cakephp:使用一种形式保存到多个模型 [英] cakephp: saving to multiple models using one form
本文介绍了cakephp:使用一种形式保存到多个模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一种形式可以保存一个具有很多部分和许多颜色的人,但是它并没有保存这些部分和颜色.只保存人(主要模型).
I have one form that saves a person with many parts and many colors but it's not saving the parts and the colors; only saving the person(main model).
在我的列表视图上,脚本的一部分:
On my list view, part of script:
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'
)
);
尝试将saveAll替换为saveAssociated,但是相同.我在这里做错了什么?任何帮助,我们将不胜感激.
Tried replacing saveAll with saveAssociated but it's the same. What am I doing wrong here? Any help is greatly appreciated.
推荐答案
您必须以格式添加别名的原始名称(别名类似于 hasMany
变量):
You must add original name of alias in form (alias like in hasMany
variable):
echo $this->Form->input('Person.person_name');
echo $this->Form->input('PersonParts.0.part_name');
echo $this->Form->input('PersonColors.0.color_name');
这篇关于cakephp:使用一种形式保存到多个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文