我有一种形式可以保存具有许多零件和许多颜色的人,但不会保存零件和颜色;只保存该人(主要模型)。脚本的cakephp:使用一种形式保存到多个模型
在我的名单视图,部分:
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,但它是相同的。 我在这里做错了什么?任何帮助是极大的赞赏。
你有三种型号对不对?并且值应该存储在不同的表中 –
将模型别名保持为单独的PersonParts - PersonPart是一个好主意 - 否则应用程序的某些方面会令人困惑(例如需要包含名为PersonParts的表单字段) – AD7six