在CakePHP中获取刚注册用户的用户ID

时间:2012-06-13 10:03:34

标签: cakephp cakephp-2.1

如何检索最近在CakePHP 2.1中注册的用户的用户ID,即

现在,名为poo的用户输入了一些注册凭据 例如usernamepassword以及

数据库结构如下

用户

id  Auto_Increment
username
password

现在,一旦创建了用户,我想以

的形式向用户显示消息

您已成功注册,且您的ID为xxx 这些xxx是整数


register.ctp视图的代码

echo $this->Form->create('User');
echo $this->Form->input('first_name', array('label'=>'First Name'));
echo $this->Form->end('Register');

控制器代码

 public function register(){
            if ($this->request->is('post')){

                if ($this->User->save($this->request->data)){

                   // $this->Session->setFlash('User is created' . $this->YourModel->id);
                    $this->Session->setFlash(__('You have registered successfully and your ID is %s', $this->YourModel->id));
                    $this->redirect(array('action'=>'index'));
                } else {
                    $this->Session->setFlash('Cannot register a user');
                }

            }
}

1 个答案:

答案 0 :(得分:1)

在模型的save() - 函数被调用之后,刚添加的ID可通过属性

获得

$this->YourModel->id;

您可以将ID传递给视图或只创建一条Flash消息:

$this->Session->setFlash(__('You have registered successfully and your ID is %s', $this->YourModel->id));`

请参阅http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-save-array-data-null-boolean-validate-true-array-fieldlist-array

相关问题