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

            }
}
Rafee
  • 3,975
  • 8
  • 58
  • 88

1 Answers1

1

After a model's save()-function is called, the just added ID is available through the attribute

$this->YourModel->id;

You can pass the ID to the view or just create a flash message:

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

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

nappo
  • 594
  • 3
  • 13