Monday, August 31, 2009

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

Yay! The query works! :)

Thank you! I really appreciate your patience. :)

Thanks for your time and knowledge!

~Sarah

On Aug 30, 11:07 pm, delocalizer <conrad.leon...@hotmail.com> wrote:
> now I understand, you want associated user & animal info for all
> specified Userpages...
> so as I suggested before, do the find on Userpage model...
> but use contain to provide the associated stuff:
>
> $pages = $this->Userpage->find('all',array(
>     'conditions'=>array(
>          'or'=>array(
>              'approved'=>1,
>              'and'=>array(
>                  'Userpage.approved' => -1,
>                  'user_id' => $id
>              )
>          )
>      ),
>     'contain'=>array(
>         User=>array(
>             Animal=>array(
>                 'fields'=>array(
>                     'frontfilename'
>                  )
>             )
>         )
>     )
> ))
>
> cheers,
> C.
>
> On Aug 31, 10:42 am, Sarah <sarah.e.p.jo...@gmail.com> wrote:
>
> > I would like the results from the query to include all userpages that
> > either have an approved value of 1 or belong to the current user
> > (whose id is stored in the $id var) and have an approved value of -1.
> > Basically I want a list of all userpages that are approved for public
> > viewing or that are not approved but belong to the logged in user.  So
> > a user can see all of their userpages and only the approved userpages
> > of the other users.
>
> > I'm sorry that by use of "the user".  I did not mean specifically the
> > user stored in $id (which is not a variable used for a loop, but holds
> > the user stored in the session), I mean each author of the the
> > userpages.  So for each userpage I would like its id, its author's id,
> > and its author's frontfilename.
>
> > I hope this clarified the relationship between Userpages and Users.
>
> > Perhaps it is, sorry.  I am relatively new to both cake and mysql, so
> > I'm still trying to figure out and understand the capabilities of
> > both.
>
> > Again, thanks.  :)
> > ~Sarah
>
> > On Aug 30, 4:16 pm, delocalizer <conrad.leon...@hotmail.com> wrote:
>
> > > ok... but I'm still not sure exactly what your intent is. When you say
> > > you need information on THE user's animal - do you mean there is a
> > > particular user that you are trying to get information on, like the
> > > current requesting user? $id is a unique value in your method (ie. not
> > > a loop iterator or something)? If so, why have find('all') and
> > > 'order'=>array('User.id'=>'asc')?  Part of my confusion is that you
> > > don't have a well-defined belongsTo relationship between Userpages and
> > > Users - which user does a page with approved = 1 belongTo? But if you
> > > need only info on the user with 'id'=>$id, set 'conditions' at the
> > > User model level as well:
>
> > >                  $array = $this->User->find('first', array(
> > >                         'conditions'=>array('id'=>$id),
> > >                         'fields'=>array('id', 'animal_id'),
> > >                          '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')
> > >                                         )
> > >                                 )
> > >                         )
> > >                 );
>
> > > I think this thread might be straying outside the scope of cakephp
> > > into program design details :)
>
> > > On Aug 31, 7:14 am, Sarah <sarah.e.p.jo...@gmail.com> wrote:
>
> > > > 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')
> > > > > > >          )
>
> ...
>
> read more »
--~--~---------~--~----~------------~-------~--~----~
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: