Add Scopes to Parent Model

I’ll preface this with, I’m new to Yii as of last week and I’m loving it! Can’t wait to really get it figured out (what’s available, where to put code, etc).

Now to the point. Is it possible to make a base class with scopes and extend that class with other scopes in the new class. Such that the scopes from both the new class and the parent class available?




  Class Article extends CActiveRecord

  {

     ...

       public function scopes()

        {

            return array(

                'published'=>array(

                    'condition'=>'status=published',

                ),

            );

        }

     ...

  }


  Class Commentary extends Article

  {

     ...

       public function scopes()

        {

            return array(

                'recent'=>array(

                    'condition'=>'createdate >' . $daysAgo,

                    'order'=>'createdate DESC',

                ),

            );

        }

     ...

  }


// Then feed this to an ActiveDataProvider in the controller:

  $criteria = new CDbCriteria(array(

     'scopes'=>array('published','recent'),

  ));



The idea is to pack all article commonalities into the base class and then have specific classes for things like News, Commentary, Post, Blog, etc…

Please let me know if this is possible and/or if I’m even on the right track.

Thanks

Commentary::scopes() overrides Article::scopes(), so you need to use array_merge() to merge scopes.




return array_merge(parent::scopes(), array('recent' => array('...')));



Thanks phtamas!

I was thinking it was something like that but wasn’t quite sure if there was some object method or something I was supposed to use. I’m fairly new to OOP in PHP (and in general) so I’m still not certain at times how stuff is passed around. I’ve always just thought of things as variables/arrays and such. I know OOP is just the same thing at it’s core but the extra layer of abstraction gets me sometimes. That said, I can see how powerful it is!

Whew, a bit wordy on that reply… anywho, thanks.