Yii Framework Forum: Using Parameterized Scopes From A Related Model? - Yii Framework Forum

Jump to content

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

Using Parameterized Scopes From A Related Model? Rate Topic: -----

#1 User is offline   girIafraid 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 09-May 13

Posted 09 May 2013 - 08:22 AM

I have a question regarding parameterized scopes that take an array parameter:

If I have the scope:

public function myScope($arrayParam) {
  ...
}


I can apply this successfully using:

MyModel::model()->myScope(array(4, 5, 6))->findAll();



However, if MyModel appears in a relation (e.g. 'myRelation') within a different model (e.g. 'AnotherModel'), and I attempt to apply the scope using:

AnotherModel::model() -> findAll(array(
  'with' => array(
      'myRelation' => array(
         'scopes' => array('myScope' => array(4, 5, 6))
       ),
   )
));

…then this does not work. Specifically because only the first value in the array parameter (in this case, 4) reaches myScope.

My relation is written correctly and as far as I understand the construction of the query is correct, so what am I doing wrong here?
0

#2 User is offline   redguy 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 815
  • Joined: 02-July 10
  • Location:Central Poland

Posted 09 May 2013 - 08:49 AM

you must pass ARRAY of parameters to scope function, and your param is another array - so it should be array with another array as first element:
AnotherModel::model() -> findAll(array(
  'with' => array(
      'myRelation' => array(
         'scopes' => array('myScope' => array(array(4, 5, 6)))
       ),
   )
));

red
0

#3 User is offline   girIafraid 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 09-May 13

Posted 09 May 2013 - 09:29 AM

View Postredguy, on 09 May 2013 - 08:49 AM, said:

you must pass ARRAY of parameters to scope function, and your param is another array - so it should be array with another array as first element:
AnotherModel::model() -> findAll(array(
  'with' => array(
      'myRelation' => array(
         'scopes' => array('myScope' => array(array(4, 5, 6)))
       ),
   )
));



This worked - thanks!
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