【发布时间】:2013-12-04 07:44:48
【问题描述】:
我想在 cakephp 中将多条记录添加到单个模型中。我一直在关注他的教程http://bit.ly/1jjR1U5,它就像魅力一样。但我需要对他的代码进行微调。我需要将表单中的一个字段设置为所有多条记录的通用字段并保存模型。我已经在下面清楚地解释了他的代码以及我需要什么。
下面是他将多条记录添加到单个模型的代码。 在 add.ctp 中:
echo $this->Form->create('Model');
for($i = 0; $i < $count; $i++){
echo $this->Form->input("Model.$i.field1", array());
echo $this->Form->input("Model.$i.field2", array());
echo $this->Form->input("Model.$i.field3", array());
echo $this->Form->input("Model.$i.field4", array());
}
echo $this->Form->end('add');
在 Controller 中,即在 add() 操作中的 ModelController.php,
function add($count = 1){
if($this->request->is('post')){
if($this->Model->saveAll($this->request->data['Model'])){
$this->Session->setFlash(__('The Model has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The Model could not be saved. Please, try again.'));
}
$this->redirect(array('action' => 'index'));
}
$this->set('count', $count);
}
以上代码将在表中创建多个插入。我想要的是add.ctp表单,
echo $this->Form->create('Model');
echo $this->Form->input("field1", array());
for($i = 0; $i < $count; $i++){
echo $this->Form->input("Model.$i.field2", array());
echo $this->Form->input("Model.$i.field3", array());
echo $this->Form->input("Model.$i.field4", array());
}
echo $this->Form->end('add');
在这里,我希望字段 1 与要插入表中的记录共用。我不想循环它,因为客户端必须在每次迭代中为字段 1 选择相同的选项。 如果有人知道如何使用 cakephp 控制器或表单来实现这一点,请分享您的建议。
【问题讨论】:
标签: php mysql cakephp cakephp-2.0 cakephp-2.3