Wednesday, June 29, 2011

Re: Limiting Find Queries by Fields in the Associated Table

I think I might have said this wrong. What I'm meaning is when I do a
findByAll query I can specify which fields that query should select
from and return. For instance I want my query to only select and
return the User.username, Message.subject, Message.id fields from
those respective tables and this works fine. I should also be able to
say Avatar.avatar to return the associated users avatar as well from
the message. But this is not working even with a recursion of 2. How
can I specify in my findByAll query that it should select and return
the Avatar spot in the array below, this is whats returned without
anything in the fields options of findByall using the debug() method.

I have no problem using in the view $message['User']['Avatar']
['avatar']; but I don't need all of the other fields that are brought
back by the query, I want to limit it to only return the 4 fields from
above but it doesn't work.

Thanks.

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

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

Am I not able to select that deep into the association via a
findByAll?

On Jun 28, 4:59 pm, andrewperk <andrewp...@gmail.com> wrote:
> 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] => addr...@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: