我有一种形式可以保存一个拥有很多零件和多种颜色的人,但是它并没有保存零件和多种颜色。仅保存人(主模型)。
在我的列表视图中,脚本的一部分:
echo $this->Form->input('Person.person_name', array('required' => true, 'placeholder' => 'Name Your Person', 'label' => false) );
echo $this->Form->input('Part.0.part_name', array('required' => true, 'placeholder' => 'Add Part', 'label' => false) );
echo $this->Form->input('Color.0.color_name', array('required' => true, 'placeholder' => 'Enter a Color', 'label' => false) );
在我的列表控制器上:
if ($this->request->is('post')) {
$created = $this->Person->saveAll($this->request->data, array('validate'=>'first'));
if (!empty($created)) {
$this->Session->setFlash(__('The person and his/her parts and colors have been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The person could not be saved. Please, try again.'));
}
}
在我的人模型上:
public $hasMany = array(
'PersonParts' => array(
'className' => 'Part',
'foreignKey' => 'part_person_id'
),
'PersonColors' => array(
'className' => 'Color',
'foreignKey' => 'color_person_id'
)
);
尝试用saveAssociated替换saveAll,但是是一样的。我在这里做错了什么?任何帮助是极大的赞赏。
您必须以格式添加别名的原始名称(别名类似于hasMany
变量):
echo $this->Form->input('Person.person_name');
echo $this->Form->input('PersonParts.0.part_name');
echo $this->Form->input('PersonColors.0.color_name');
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句