Tuesday, June 28, 2011

Limiting Find Queries by Fields in the Associated Table

Hello, I'm trying to figure out how to limit my find query to a field
that is in the related table. My associations are like so:

User hasMany Message
Message belongsTo User
User hasOne Avatar
Avatar belongsTo User

In my Message model, I'm finding all messages that belong to a user.
I'm using a recursive of 2 so that I can pull in the associated Avatar
model as well.

Using debug on the query without limiting fields in the view I get the
following output:

Array
(
[0] => Array
(
[Message] => Array
(
[id] => 11
[user_id] => 1
[friend_id] => 6
[subject] => Message Subject
[body] => Message Body
)

[User] => Array
(
[id] => 1
[email] => address@email.com
[username] => user1
[password] =>
9e05f17f51310726878dcf822f8682dcb0051b79
[firstname] => User
[lastname] => Name
[Avatar] => Array
(
[id] => 1
[user_id] => 1
[avatar] => 1309297124.jpg
)
)


How can I access that Avatar spot in the array in my fields array for
doing the find query?


My find query limiting fields looks like this:

findAllByFriendId($user_id, array('Message.subject', 'Message.id',
'User.username', 'Avatar.avatar'), array('Message.id'=>'desc'));

This query works fine up until the Avatar.avatar spot as it says there
is no Avatar.avatar in fields. So I thought maybe I should try using
'User.Avatar.avatar'. But that doesn't work either.

Does anyone have any helpful tips?

Thanks.

Andrew

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: