2
我有两个模型商业和用户。他们与HABTM关系有关。如何在CakePHP中将两个HABTM保存为一种形式?
一切正在与烘焙的控制器,模型和视图。
现在我试图在一种形式中结合这两种模式,以便用户可以输入一个企业名称与用户信息。
这里的形式:
Form->create('User'); ?>
Form->input('Business.name', array('label' => __('Business name')));
echo $this->Form->input('User.email');
echo $this->Form->input('User.firstname');
echo $this->Form->input('User.lastname');
echo $this->Form->input('User.password');
echo $this->Form->input('User.phone_cell', array('type' => 'text'));
echo $this->Form->input('User.phone_home', array('type' => 'text'));
echo $this->Form->input('User.phone_work', array('type' => 'text'));
?>
Form->end(__('Submit')); ?>
的唯一途径,我能够让它的工作是先保存用户,然后获取用户ID,并通过添加用户数组新后保存业务ID。
if ($this->User->save($this->request->data)) {
$this->request->data['User'] = array('User' => array(0 => $this->User->id));
if ($this->User->Business->save($this->request->data)) {
// User saved
} else {
// User not saved
}
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
我试着saveAll方法没有成功。是否可以优化这个CakePHP的方式来保存一次?
感谢
您有用户ID来关联组。用你的例子,我想同时添加一个新用户和新组。 – user1949419
我使用'$ this-> User-> Business-> invalidFields();验证了Business.name;'' – user1949419