Using Relations with Parameterized Scopes

Hi,

Being new to the Yii framework I’ve been trying to work out a way to filter using a relation and its parameterized scope.

Currently I’m already filtering using a few other parameterized scopes.

E.g.




    $groups = GroupItem::model()->region($params['region'])->base($params['base'])->order('GrpItmCd ASC');


    ..


    public function region($param)

    {

        $this->getDbCriteria()->mergeWith(array(

            'condition' => 'RegionID='.$param

        ));

        return $this;

    }


    public function base($param)

    {

        $this->getDbCriteria()->mergeWith(array(

            'condition' => 'BaseID='.$param

        ));

        return $this;

    }



Now I also have another model Year which this model has a relationship too.




public function relations()

{

   return array(

     'yr' => array(self::BELONGS_TO, 'Yr', 'YrID')

   );

}



And on this relationship model it has a named scope.




    public function currentYear($param)

    {

        $this->getDbCriteria()->mergeWith(array(

            'condition' => 'YrDsc='.$param

        ));

        return $this;

    }



Any help would be much appreciated, thanks.

Hi Teng, try looking at the code I posted here:

http://www.yiiframework.com/forum/index.php?/topic/23358-parameterized-vs-named-scopes-question-using-yii-118/page__fromsearch__1

I was doing the parameterized query on a model In a sub sub relationship, but the same principal applies. Let me know if you need more help.