Lots of queries to pull up user photos and their comments

View: New views
4 Messages — Rating Filter:   Alert me  

Lots of queries to pull up user photos and their comments

by centr0 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


my models:

user hasmany comment
profile hasmany comment
user hasone profile
user hasone photo

comment belongsto user
profile belongsto user
photo belongsto user

what im trying to do is pull up profile information and the profiles
comments.

here is my index() and its find():
        function index($url = null) {
                $this->Profile->recursive = -1;

                /*
                *
                */
                $profile = $this->Profile->find('first', array(
                        'conditions' => array('Profile.url' => $url),
                        'contain' => array(
                                'User' => array(
                                        'fields' => array(
                                                'User.id','User.name'
                                        ),
                                        'Photo' => array(
                                                'fields' => array(
                                                        'Photo.thumbnail','Photo.image'
                                                )
                                        )
                                ),
                                'Comment' => array(
                    'User' => array(
                        'fields' => array(
                            'User.name'
                        ),
                        'Photo' => array(
                            'fields' => array(
                                'Photo.thumbnail'
                            )
                        )
                    )
                )
                        )
                ));
                $this->set('profile', $profile);
        }

this function takes its function arg and finds the appropriate profile
ie. /profiles/index/centroscape

Everything returns correctly without error i get exactly what i want,
but once the query gets to the Photo part, it queries multiple times
for photos with different ids.  if there is a way to avoid 1 query per
photo please let me know.  I dont think there is.

imagine a profile with 50-80 comments.  its kind of scary.  also, this
is my first time implementing containable behavior. any tips other
than my question are welcome!


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@...
To unsubscribe from this group, send email to cake-php+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Lots of queries to pull up user photos and their comments

by centr0 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Update:

This query is what i typically want run for the comments part of the
contain:

SELECT Comment.content, Comment.created, Comment.modified,
Photo.thumbnail, User.name
                                                                FROM comments AS Comment
                                                                LEFT JOIN profiles AS i ON Comment.profile_id = i.id
                                                                LEFT JOIN users AS User ON Comment.user_id = User.id
                                                                LEFT JOIN photos AS Photo ON User.id = Photo.user_id
                                                                WHERE Comment.profile_id =1

anyway to portray this the cake way?  id like to stay away from custom
SQL.  Thanks.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@...
To unsubscribe from this group, send email to cake-php+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Lots of queries to pull up user photos and their comments

by Miles J :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Use the "join" parameter within find. However, theres not really any
documentation on it, so you would have to read the source code
manually.

On Nov 4, 10:07 am, centr0 <centrosc...@...> wrote:

> Update:
>
> This query is what i typically want run for the comments part of the
> contain:
>
> SELECT Comment.content, Comment.created, Comment.modified,
> Photo.thumbnail, User.name
>                                                                 FROM comments AS Comment
>                                                                 LEFT JOIN profiles AS i ON Comment.profile_id = i.id
>                                                                 LEFT JOIN users AS User ON Comment.user_id = User.id
>                                                                 LEFT JOIN photos AS Photo ON User.id = Photo.user_id
>                                                                 WHERE Comment.profile_id =1
>
> anyway to portray this the cake way?  id like to stay away from custom
> SQL.  Thanks.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@...
To unsubscribe from this group, send email to cake-php+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Lots of queries to pull up user photos and their comments

by Dr. Loboto :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


But there is very helpful article:
http://bakery.cakephp.org/articles/view/quick-tip-doing-ad-hoc-joins-in-model-find

On Nov 5, 1:44 am, Miles J <mileswjohn...@...> wrote:

> Use the "join" parameter within find. However, theres not really any
> documentation on it, so you would have to read the source code
> manually.
>
> On Nov 4, 10:07 am, centr0 <centrosc...@...> wrote:
>
>
>
> > Update:
>
> > This query is what i typically want run for the comments part of the
> > contain:
>
> > SELECT Comment.content, Comment.created, Comment.modified,
> > Photo.thumbnail, User.name
> >                                                                 FROM comments AS Comment
> >                                                                 LEFT JOIN profiles AS i ON Comment.profile_id = i.id
> >                                                                 LEFT JOIN users AS User ON Comment.user_id = User.id
> >                                                                 LEFT JOIN photos AS Photo ON User.id = Photo.user_id
> >                                                                 WHERE Comment.profile_id =1
>
> > anyway to portray this the cake way?  id like to stay away from custom
> > SQL.  Thanks.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@...
To unsubscribe from this group, send email to cake-php+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---