Sunday, August 30, 2009

Re: Using belongsTo and hasMany relationships: Is it possible to access tables through a chain of relationships?

I'm performing this in the Userpages controller. I was calling find
on Users because I need information on the user's animal.

User hasMany Userpages
User belongsTo Animal
The foreign keys are set up appropriately.

I need to get a list of all userpages in which: (Userpages.approved =
1) OR (Userpages.approved = -1 AND User.id/user_id = $id)

But I want the following information: the Userpage id, the userpage
user_id, and the frontfilename from the user's animal.

The concept is similar to a list of comments:
I want the id and contents of the comment, I want the information on
the user, including the user's picture (in this case frontfilename
which is a column in Animal).

I suppose the catch is that more than one user can be the same animal
(users are in groups).

I've been searching everything that I can think of and searching based
off what I find. Do you think the kind of join matters?

I appreciate your following up and your help.

Thank you,
~Sarah

On Aug 30, 1:28 am, delocalizer <conrad.leon...@hotmail.com> wrote:
> Ah I hadn't realised you just wanted Userpages - in which case why not
> call find on Userpage?
> If you're in Users controller:
> $pages = $this->User->Userpage->find('all',array(
>     'conditions'=>array(
>          'or'=>array(
>              'approved'=>1,
>              'and'=>array(
>                  'Userpage.approved' => -1,
>                  'user_id' => $id
>              )
>          )
>      )
> ))
>
> On Aug 30, 3:21 pm, Sarah <sarah.e.p.jo...@gmail.com> wrote:
>
> > Wow, this was very cool.  Thank you for the advice.
>
> > I realized I had forgotten a condition.  So this is where I am now:
>
> >                  $array = $this->User->find('all', array(
> >                         'fields'=>array('id', 'animal_id'),
> >                         'order'=>array('User.id'=>'asc'),
> >                         'contain'=>array(
> >                                 'Animal'=>array(
> >                                         'fields'=>array
> > ('frontfilename')
> >                                 ),
> >                                 'Userpage'=>array(
> >                                         'conditions'=>array(
> >                                                 'or'=>array(
> >                                                         'approved'=>1,
> >                                                         'and'=>array(
>
> > 'Userpage.approved' => -1,
>
> > 'user_id' => $id
> >                                                         )
> >                                                 )
> >                                         ),
> >                                         'order'=>array(
> >                                                 'id'=>'desc')
> >                                         )
> >                                 )
> >                         )
> >                 );
>
> > This does not cause any errors.  :)  However, I'm not quite sure that
> > this is what I need.  To my understand based of the reading on
> > containable (and the results that I received from the query), this
> > query will return all users and only return their userpage information
> > if it matches the conditions.  I suppose I could just check each user
> > from the query to see if that user had any userpage info returned by
> > the query, but I imagine this would slow things down?
>
> > I thank you very much for your time and help.  I would appreciate any
> > advice you can give.  :)
>
> > Thanks,
> > ~Sarah
>
> > On Aug 29, 1:08 am, delocalizer <conrad.leon...@hotmail.com> wrote:
>
> > > Hi Sarah,
> > > If you have a reasonably complicated find query with associated models
> > > and conditions, the core 'Containable' behaviour is very handy:http://book.cakephp.org/view/474/Containable
> > > so in your AppModel put this (and all your models will have access to
> > > the behaviour):
> > > var $actsAs = array('Containable');
> > > then you would have for your query something like this:
> > > $array = $this->User->find('all',array(
> > >     'fields'=>array('id','animal_id'),
> > >     'order'=>array('id'=>'asc'),
> > >     'contain'=>array(
> > >          Animal=>array(
> > >             fields=>array('frontfilename')
> > >          ),
> > >          Userpage=>array(
> > >             'conditions'=>array(
> > >                 'approved' => '1'
> > >             ),
> > >             'order'=>array(
> > >                 'id'=>'desc')
> > >          )
> > >     )
> > > ));
> > > Note that conditions, fields and order options are all specified on a
> > > per-model basis  - you get back only what you want from each model.
> > > Containable is definitely worth the effort to learn.
>
> > > cheers,
> > > C.
>
> > > On Aug 29, 7:40 am, Sarah <sarah.e.p.jo...@gmail.com> wrote:
>
> > > > I fixed my syntax,
>
> > > > $conditions = array('or' => array( 'Userpage.approved' => 1, 'User.id'
> > > > => $id) );
> > > > $order = array('User.id ASC', 'Userpage.id DESC');
> > > > $array = $this->User->find('all', array('fields'=>array('id',
> > > > 'Userpage.id', 'User.animal_id', 'Animal.frontfilename'),
> > > > 'conditions'=>$conditions, 'order'=>$order));
>
> > > >  but I'm still getting the same errors as before.
>
> > > > I verified that a User has many userpage and that a userpage belongs
> > > > to a user.
> > > > The SELECTing of the Animal picture is not a problem...I don't
> > > > understand why it won't SELECT the userpage information...
>
> > > > My recursive level is set to 1, I tried setting it to 2, but it didn't
> > > > make a difference.
>
> > > > Thanks for your time and help,
> > > > ~Sarah
>
> > > > On Aug 28, 12:23 pm, WebbedIT <p...@webbedit.co.uk> wrote:
>
> > > > > Looks like you very nearly had it.  Just got your find syntax slightly
> > > > > wrong, try:
>
> > > > > $array = $this->User->find('all', array(
> > > > >   'fields'=>array('id, 'Userpage.id', 'User.animal_id',
> > > > > 'Animal.frontfilename'),
> > > > >   'conditions'=>$conditions,
> > > > >   'order'=>$order
> > > > > );
>
> > > > > Then go have a look at the cookbook and study the syntax of the find
> > > > > command a bit more.  The first parameter should be the type of call,
> > > > > in this instance 'all'.
>
> > > > > The second parameter needs to be an array with your fields,
> > > > > conditions, order etc.
>
> > > > > Hope this helps
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
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?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: