大家好我正在使用Cakephp 2.x.我有一个事件模型(表)和一个用户模型。保存事件时,我需要将当前登录的用户ID保存到事件模型的user_id字段中。我试过的代码已被评论过了。有任何想法吗?提前谢谢。
public function add(){
//$this->loadModel('User');
if ($this->request->is('post')) {
$this->Event->create();
//$this->request->data['Event']['user_id'] = $this->request->data['User']['id'];
//$this->request->data['Event']['user_id'] = $this->request->data['User']['id'];
if ($this->Event->save($this->request->data)) {
$this->Session->setFlash(__('The event has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The event could not be saved. Please, try again.'));
}
}
答案 0 :(得分:2)
而不是
$this->request->data['User']['id'];
使用
$this->Auth->user('id');