0

How can i retrieve the user id of a user who has recently register.. in CakePHP 2.1 ie

right now, user with name poo entered some registeration credentials like username and password and

database structure is below

users

id  Auto_Increment
username
password

now, once the user is created , i want to display a message to user as

You have registered successfully and you id is xxx these xxx are integer number


Code for register.ctp view

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

Code for controller

 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');
                }

            }
}
4

1 回答 1

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

于 2012-06-13T10:19:08.907 回答