ログインしているユーザーIDで保存する

これを保存する際に追加
[php]$this->request->data[‘Post’][‘user_id’] = $this->Auth->user(‘id’); [/php]

[php]
public function add() {
if ($this->request->is(‘post’)) {
$this->Post->create();
$this->request->data[‘Post’][‘user_id’] = $this->Auth->user(‘id’); //postテーブルのuser_idにログインしているユーザー情報を保存するよ
if ($this->Post->save($this->request->data)) {
$this->Session->setFlash(__(‘The post has been saved.’));
return $this->redirect(array(‘action’ => ‘index’));
} else {
$this->Session->setFlash(__(‘The post could not be saved. Please, try again.’));
}
}
$users = $this->Post->User->find(‘list’);
$this->set(compact(‘users’,’options’));

}
[/php]

参考:http://offsidenow.phpapps.jp/archives/1447

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です