tags:

views:

53

answers:

2

I have three Models with the associated properties:

Links Model:

var $belongsTo = array('LinkCategory');

LinkCategory Model:

var $hasMany = array('Link');
var $hasAndBelongsToMany = array('User');

User Model

var $hasAndBelongsToMany = array('LinkCategory');

With that, I would expect the following to get me all the link categories for the user.

$user_link_categories = $this->LinkCategory->find('all', array('conditions' => array('User.id' => 1)));

However, it dies with: Unknown column 'User.id' in 'where clause' Query: SELECT LinkCategory.id, LinkCategory.title, LinkCategory.created, LinkCategory.modified FROM link_categories AS LinkCategory WHERE User.id = 1

I am new to CakePHP, but not MVC. In my mind, this should work given my associations. Have I missed something or does this just not work in CakePHP for some reason.

A: 

LinkCategory doesn't have a field User.id... as a matter of fact, the way User.id has a period in the middle suggests id is a field which belongs to User.

Since LinkCategory belongsTo a User it probably has a field user_id, so theoretically this should work:

$user_link_categories = $this->LinkCategory->find('all', array('conditions' => array('user_id' => 1)));

If this still doesn't work it means your DB tables don't have columns to support these relationships and you'll need to read more about associations to get to the bottom of that.

Personally, though, I think you're going backwards. You're trying to find a LinkCategory based on their associated User, but you can't use a field from another model (User) like this in a simple find() (on LinkCategory).

Instead you should find the User with an id of '1', then - since a User belongsTo a LinkCategory - read that's User's LinkCategory. Again, I'm rusty, but I'm thinking it would look a little like this...

$user = $this->User->find('first', array('conditions' => array('Article.id' => 1)));
pr($user);

... make that recursive (see here) and $user should load your LinkCategory as well.

LeguRi
Also, if you're looking to filter on a field that's in a related model, you should use the Containable property. You can't tell linkCategory to filter on a property of the Users table, for example, without using Containable. It's well documented in the manual.
Travis Leleu
@Travis Leleu - does that mean what bancer suggests (using `'recursive' => 2`) won't work without `Containable`?
LeguRi
No, the recursive parameter determines the level of relationships that will be retrieved on a find. For finer granularity, you can use Containable. Also, if you want to filter (the conditions parameter) based on fields in the related model, you'll need to use Containable.
Travis Leleu
Your code above assumes I have a LinkCategory belongsTo User relationship, and conversely a User hasMany LinkCategory. If you looked at original post, these are HABTM relationships. Thanks anyway.
Jason McCreary
You're right! My bad :(
LeguRi
A: 
bancer
Really!? Making the query recursive would allow one to filter on `User.id`? Nifty.
LeguRi
@Richard JP Le Guen: I'm not sure. That was a guess.
bancer
Yes, this was a misprint. Link is the Model. I also have a join table of link_categories_users. Unforunately, none of the sample code you provided works. Even the last one, which was exceptionally baffling to me.
Jason McCreary
I have the similar structure in my application and the last code sample works for me. I've verified it. What is the query log you got?
bancer
@bancer, I ended up using the `join` parameter as the last code sample does work when the query is left fully recursive. However, this seems overkill as my User model has many additional associations. Thanks.
Jason McCreary