を保存する前に、私はこのようになりますコントローラで機能を持っています。私は新しい部屋を追加して実行すると、あなたは誰が部屋の中にいるのかを選択できるようになりますが、それは保存されていますが、最初のユーザーを$ room-> users = [$ user_field] ;そのbeeing保存されません。私は$ roomオブジェクトをファイルに記録しています。両方のオブジェクトは同じです(フォームを使用してユーザーを追加した後、コードを使用してユーザーを追加した後)。私を助け:(CakePHPの3つのデータ関連
は、たぶん私はモデルにbeforeSave()またはafterSave()を使用する必要がありますか?
SOLUTION 私は助けをCode.Working と解決策を見つけた。 代わりの
$this->loadModel('Users');
を追加私はちょうど私のcreateRoom()関数に入れinitializie()メソッドに 。
最終作業コードはリクを探しますE本:
//Create room
public function createRoom(){
$this->loadModel('Users');
$room = $this->Rooms->newEntity();
if($this->request->is('post')){
$room = $this->Rooms->patchEntity($room, $this->request->data);
$user_field = $this->Users->get($this->Auth->user('id'));
$room->users = [$user_field];
$room->current = 1;
$room->owner_id = $this->Auth->user('id');
Log::debug($room);
if($this->Rooms->save($room)){
$this->Flash->success('You have created a new room.');
return $this->redirect(['action' => 'index']);
}
else{
$this->Flash->error('Error creating new room.');
}
}
$games = $this->Rooms->Games->find('list', ['limit' => 200, 'keyField' => 'id', 'valueField' => 'name']);
$this->set(compact('room', 'games', 'users'));
$this->set('_serialize', ['room']);
}
お返事ありがとうございます。私はあなたのソリューションを私のコントローラに入れています。iveは次のエラーを受け取りました:ブール値でメンバ関数user()を呼び出します。これでどうしたらいいですか? – Barteo
Auth Pluginを読み込みましたか? Authを変数に、print_r()をビューに保存すると役立ちます。 $ this-> set( 'auth'、$ this-> Auth)と同様です。 ビュー内:print_r($ auth); –