CakePHP 2.x将用户模型中的用户ID保存到登录用户的事件模型中的事件user_id字段中

时间:2012-12-02 20:02:52

标签: cakephp cakephp-1.3 cakephp-2.0 cakephp-2.1 cakephp-appmodel

大家好我正在使用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.'));
            }
        }

1 个答案:

答案 0 :(得分:2)

而不是

$this->request->data['User']['id'];

使用

$this->Auth->user('id');