Yii Framework Forum: How Does Has_Many Works? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How Does Has_Many Works? Rate Topic: -----

#1 User is offline   Bogdanas 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 18-July 13

Posted 18 July 2013 - 06:33 AM

Hello everyone, I can't understand how HAS_MANY relation works. I have model Place with relation:
'movies' => array(self::HAS_MANY, 'Movies', 'id_place')

And I can't figure out how to filter Movies model, it always selects all related Movies, I tried to describe condition with:
$this->loadModel($id)->with(array('movies' => 'column=value'))

but it selects all movies, it always selects all movies doesn't matter if I use "with" statement or not, tried to add together=>false to relation, doesn't matter. What am I doing wrong?

P. S. Sorry for my bad English
0

#2 User is offline   Pep 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 22
  • Joined: 06-August 11

Posted 20 July 2013 - 05:54 PM

Here is a description: http://www.yiiframew...ord#with-detail

You can pass an array as a parameter to the with() method.

In this case the key of the array should be the related model and the value of the array should be another array. This second array should be a criteria compatible with CDbCriteria.

In your case I am gessing something like this would work:
with(array(
    'movies'=>array(
        'on'=>'column = :value',
        'params'=>array(
            ':value'=>$value,
        ),
    ),
))


You could also put the condition in a condition statement for the relation like this:
with(array(
    'movies'=>array(
        'condition'=>'column = :value',
        'params'=>array(
            ':value'=>$value,
        ),
    ),
))


I believe that is you put your condition into the condition statement of the relation the join will act like an inner join. If you want to keep the left join behavior that is the default of relations then put your condition into the 'on' statement.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users