我在Vake项目profile.ctp中有一个使用profileController的编辑个人资料页面? 我想为网址,手机等添加输入字段,但用户可以拥有更多手机,因此我创建了另一个名为users_phones的表
USERS_PHONES
id
user_id
phone
在我看来,我创建了表单
<?
echo $this->Form->create();
echo $this->Form->input('UsersPhones.phone');
echo $this->Form->end('Save');
?>
在模特中我有:
public $hasMany = array(
'Phones' => array(
'className' => 'UsersPhones',
'foreignKey' => 'user_id',
'dependent' => true
)
);
控制器:
$this->User->id = $this->Auth->user('id');
if ($this->request->is('post')) {
if ($this->User->save($this->request->data, array('validate' => false))) {
$this->Session->setFlash('Profile updated succsessefully!',
'default', array('class' => 'okmsg'));
$this->redirect($this->request->here);
} else {
$this->Session->setFlash('Profile could not be saved. Please, try again.',
'default', array('class' => 'errormsg'));
}
}
但是当我点击SAVE时它不会插入users_phones
答案 0 :(得分:2)
您需要将save()替换为支持保存多个数据的saveAll()(或saveAssociated / saveMany)。