Wednesday, March 2, 2011

Re: How to properly use "conditions" in model associations

i agree with gremlin
foreign_key + model is an example for this

everything else i would set right inside the querying model method


On 2 Mrz., 23:06, gremlin <abba.bry...@gmail.com> wrote:
> I tend to define conditions in associations on very few occasions. One
> good use is for polymorphic models (you need to define the field =>
> modelName mapping) and for models that have a enum field (or similar)
> that determines some behavior of the model. IE log models and for
> defining self referencing tables where I tend to use an relationship
> alias (ParentNode, ChildNode)
>
> This isn't the only time you might define conditions in the
> relationship but these are the only ones that pop to mind.
>
> On Mar 2, 11:43 am, "Krissy Masters" <naked.cake.ba...@gmail.com>
> wrote:
>
>
>
>
>
>
>
> > Personaly I never set any conditions in the relations (hasOne, hasMany ....)
>
> > all conditions are defined in the function since depending on why I am
> > getting the data and for who conditions constantly change.
>
> > You can say conditions Posts => 1 //active so you only by default display
> > all active Post.
>
> > but then when its time to get all users posts so they can edit their data
> > you need to set condition where Post.id => $user_id. If you kept the regular
> > condition you would only pull that users active posts not all.
>
> > But I just developed from day 1 like this so also would be curious to see
> > what others are doing in regards to the other params available for
> > relations.
>
> > Good question :)
>
> > K
>
> > -----Original Message-----
> > From: cake-php@googlegroups.com [mailto:cake-php@googlegroups.com] On Behalf
>
> > Of Eric Anderson
> > Sent: Wednesday, March 02, 2011 3:57 PM
> > To: CakePHP
> > Subject: How to properly use "conditions" in model associations
>
> > Hi everyone,
>
> > I'm wondering what is the 'proper' or 'suggested' way of using
> > "conditions" in model associations. I'm using an Activity model to log
> > activities performed throughout my app. I then use this table to
> > output activity feed / notifications style sections to users. The
> > problem I'm having is that Cake throws a "column not found" error (I
> > guess technically it's MySQL throwing the error). When running a
> > query, Cake for some reason formulates the query so that MySQL is
> > looking for columns from my Activity table inside of other tables. Do
> > I need to specify "conditions" in both my User and Activity model, as
> > shown below? When I remove the conditions from the Activity model,
> > everything runs fine.
>
> > <?php
>
> > class Activity extends AppModel {
>
> >     var $name = 'Activity';
> >     var $actsAs = array('Containable');
>
> >         var $belongsTo = array(
>
> >                 // Doers
> >                 'User' => array(
> >                 'className' => 'User',
> >                 'foreignKey' => 'doer_id',
> >                         'conditions' => array('Activity.doer_table' =>
> > 'users')
> >         )
> > }
> > ?>
>
> > <?php
>
> > class User extends AppModel {
>
> >     var $name = 'User';
> >     var $actsAs = array('Containable');
>
> >     var $hasMany = array(
> >                 'Activity' => array(
> >                 'className' => 'Activity',
> >                 'foreignKey' => 'doer_id',
> >                         'conditions' => array(
> >                                 'Activity.doer_table' => 'users'
> >                         ),
> >                 'dependent' => true
> >         );
> > }
> > ?>
>
> > --
> > Our newest site for the community: CakePHP Video Tutorialshttp://tv.cakephp.org
> > Check out the new CakePHP Questions sitehttp://ask.cakephp.organdhelp
> > 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 athttp://groups.google.com/group/cake-php

--
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: