Ben Davis
Ben Davis

Reputation: 1

CakePHP find returns same value every time

Hey guys, been having this problem for a while now, and can't for the life of me seem to track down anything remotely helpful.

I'm trying to, once a user logs into the application (using the built-in Auth component), use the school_id field to find the name of the school that they are associated with (and display this name in the header of the view). I also figure that I will need to call up various other pieces of school information in other actions down the road.

I've tried both of the following, but neither seems to work. No matter whether I School->find() based on the user's "school_id" or by a number that I have included manually. It simply returns the information of the same school every time (the school with the ID of 1).

Here's what I've tried:

$this->set('school_name', $this->School->find('first', array('conditions' => array('School.id' == 2))));

$this->set('school_info', $this->School->find('first', array('conditions' => array('School.id' == $this->Auth->User('school_id')))));

$this->set('school_info', $this->School->find($this->Auth->User('school_id');

Once again, not a problem with the code not returning anything. It just returns the same school every time (where ID = 1).

As you can imagine, this has been fairly frustrating, and I would love any help that you could provide.

Thanks, Ben

Upvotes: 0

Views: 352

Answers (1)

RabidFire
RabidFire

Reputation: 6330

You are using the == sign instead of => in your condition.

It should be:

$this->School->find('first', array('conditions' => array('School.id' => 2))));

Remember: == is a conditional operator. => is the arrow notation used to create array key-value pairs.

A short way to do it would be:

$this->School->findById($this->Auth->user('school_id'));

If you're supplying the id, supply it to the findById and not the all-encompassing find method. That said, you should still take care to see that you're using the right operators. :)

Quick Note: 'School.id' == 2 evaluates to false and array(false) is an array with one element false which is why you didn't get any errors.

Upvotes: 2

Related Questions