我有一种形式可以保存一个具有许多部件和多种颜色的人,但它不保存部件和颜色;只救人(主模型)。
在我的列表中 View ,脚本的一部分:
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) );
在我的列表 Controller 上:
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'
)
);
尝试将 saveAll 替换为 saveAssociated 但结果是一样的。 我在这里做错了什么?非常感谢任何帮助。
最佳答案
您必须在表单中添加别名的原始名称(如 hasMany
变量中的别名):
echo $this->Form->input('Person.person_name');
echo $this->Form->input('PersonParts.0.part_name');
echo $this->Form->input('PersonColors.0.color_name');
关于php - cakephp:使用一种形式保存到多个模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20741415/