This is a mystery.. in my controller Add function i want to enter the user’s id into the record being created. I put the $this->Session->read(‘Auth.User.id’) after the create statement. But it was always blank.. i found out i could get username and role but id was always empty.
I am following cake naming convention for the table and field names. Users table has:
id
username
role
Calling the Session and Auth I can get the user’s role, username, any other value from the table but not the id… very weird
$this->Session->read(‘Auth.User.role’) —-> this returns role
$this->Session->read(‘Auth.User.username’) —-> this returns username
$this->Session->read(‘Auth.User.id’) —-> this is blank!
Any ideas what might be happening?
So the answer to my question (which i found out after a while on my own) is that after adding a new record the autonumber id isn’t put into the auth Session for current user. Cake does this at login, so you’d have to log out and log back in to get new id in Auth Session. However you could get the newly inserted user autonumber by using getInsertId() and then use that to update the Auth Session by using $this->Session->write(‘Auth.Model.id’, $this->Model->getInsertId()); – genoki 49 secs ago edit