drmonkeyninja
drmonkeyninja

Reputation: 8540

CakePHP $this->Auth->user('id') returns incorrect user in controller

I've got a really bizarre bug occurring in a CakePHP app I've been working on.

I've got the following method in my users_controller.php that reads the currently signed in user and sends the data to a view and sets the title_for_layout to the user's name:-

function account() {
    $this->User->id = $this->Auth->user('id');
    $this->set('User', $this->User->read());
    $this->set('title_for_layout', 'Welcome '.$this->User->Contact->field('name'));
}

In my view I've got (among other things):-

<?php echo $User['Contact']['name'] ?>

Everything in the view looks fine. It is outputting the fields for the correct user (the one I am currently signed in as). However the title_for_layout is using a completely different user's details. So $this->User->Contact->field('name') is not the same as $User['Contact']['name']!

I can't spot what is going wrong here so hoping someone out there can point out my mistake.

Upvotes: 0

Views: 969

Answers (1)

ori
ori

Reputation: 7847

Model read() simply does a find('first'), sets the result as the Model $data, and returns it. It doesn't set $ids of associated models.

So in your case, $this->User->id is set, but $this->User->Contact->id isn't.

Upvotes: 2

Related Questions