添加数据库行按钮 Cakephp
控制器:
function add(){
if (!empty($this->data)) {
$qnote = $this->Qnote->save($this->data);
if (!empty($qnote)) {
$this->data['Step']['qnote_id'] = $this->Qnote->id;
$this->Qnote->Step->save($this->data);
}
$this->Session->setFlash('Your note has been saved.');
$this->redirect(array('action' => 'index'));
}
}
表单。
<?php
$userID = Authsome::get('id');
echo $form->create('Qnote', array('action'=>'add'));
echo $form->input('Qnote.id', array('type' => 'hidden'));
echo $form->input('Qnote.user_id', array('value' => $userID, 'type' => 'hidden'));
echo $form->input('Qnote.subject');
echo $form->input('Qnote.body', array('rows' => '3'));
echo $form->input('Step.id', array('type' => 'hidden'));
echo $form->input('Step.user_id', array('value' => $userID, 'type' => 'hidden'));
echo $form->input('Step.body', array('rows' => '3'));
echo $form->end('Save Notes');
?>
此表单在 2 个模型中添加数据。 模型1=Qnote; 模型 2 = 步骤; 我能够将数据添加到模型中。
我想知道我可以在表单中添加一个按钮 该按钮允许用户将多个 Step.data 添加到 Step 模型中。 有些喜欢 +1 按钮。
基本上我想为每个 Qnote 添加多个步骤。
有人能指出我如何实现这一目标的正确方向吗?
The Controller:
function add(){
if (!empty($this->data)) {
$qnote = $this->Qnote->save($this->data);
if (!empty($qnote)) {
$this->data['Step']['qnote_id'] = $this->Qnote->id;
$this->Qnote->Step->save($this->data);
}
$this->Session->setFlash('Your note has been saved.');
$this->redirect(array('action' => 'index'));
}
}
The Form.
<?php
$userID = Authsome::get('id');
echo $form->create('Qnote', array('action'=>'add'));
echo $form->input('Qnote.id', array('type' => 'hidden'));
echo $form->input('Qnote.user_id', array('value' => $userID, 'type' => 'hidden'));
echo $form->input('Qnote.subject');
echo $form->input('Qnote.body', array('rows' => '3'));
echo $form->input('Step.id', array('type' => 'hidden'));
echo $form->input('Step.user_id', array('value' => $userID, 'type' => 'hidden'));
echo $form->input('Step.body', array('rows' => '3'));
echo $form->end('Save Notes');
?>
This Form Adds Data in 2 Models.
Model 1 = Qnote;
Model 2 = Step;
I am able to add Data to the Models.
I was wondering I could add a button to the form
The Button would allow users to add multiple Step.data to the Step model.
Some like a +1 Button.
Basically I want to add multiple steps Per Qnote.
Could someone point me in the right direction how i can achieve this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我用 jQuery 做的事情。基本上,您需要做的就是使用 jQuery 在 CakePHP 约定中动态添加更多输入:例如
Step.0.user_id
。您现在需要对 +1 执行的操作:您需要将零向上计数,这样您将获得
Step.1.user_id
等等。第一个选项:使用 jQuery 脚本来执行此操作
在本例中,您将使用
step
类克隆一个 div,该类保存模型Step< /代码>。然后,您可以替换名称属性,以将零替换为变量
count
的新值。count++
使您能够添加任意数量的步骤。这是仅 jQuery 的解决方案,可能需要针对您的环境进行额外的工作。
第二个选项:将 AJAX 与元素结合使用
您还可以在
StepsController
中编写一个函数,该函数呈现一个保存表单并处理计数器的元素。第三个选项:使用 URL 参数来决定需要多少个选项
如果您有一个类似
/qnote/add/3
的 URL,您可以使用 3 作为参数用于迭代这些表单输入的 for 循环。您需要注意,在添加另一个步骤时,最终已经输入的值会随表单一起发送,这样它们就不会丢失。
希望这有助于走上正确的道路。
This is something I would do with jQuery. Basically all you need to do is using jQuery to dynamically add more inputs in the CakePHPs conventions:
Step.0.user_id
for example.What you need to do now on a +1: you need to count the zero up, so you will get
Step.1.user_id
and so on.First option: Use a jQuery-Script for doing this
In this case you're cloning a div with the class
step
which holds your inputs for the modelStep
. You then replace the name-attribute to replace the zeros through the new value of the variablecount
.count++
enables you to add as many steps as you want.This is an jQuery only solution and may require additional work for your environment.
Second option: Use AJAX with an element
You could also write a function in your
StepsController
which renders an element which holds your form and takes care of the counter.Third option: Use a URL-parameter to decide how many options you want
If you have an URL like
/qnote/add/3
you could use the 3 as a parameter in a for-loop to iterate through these form-inputs.You need to take care, that eventually already typed in values are sent with the form when adding another Step so that these don't get lost.
Hope this helps to get on the right way.