0

大家好,我使用的是 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.'));
            }
        }
4

1 回答 1

2

代替

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

采用

$this->Auth->user('id');
于 2012-12-02T20:26:02.997 回答