ビジネスとユーザーの2つのモデルがあります。それらはHABTM関係によって関連付けられています。
すべてがベイク処理されたコントローラー、モデル、およびビューで機能しています。
現在、2つのモデルを1つのフォームに組み合わせて、ユーザーが会社名を入力できるようにしています。
フォームは次のとおりです。
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の方法で1回の保存に最適化することは可能ですか?
ありがとう