Extend CActiveRecord

I have added the below function to yii\framework\db\ar\CActiveRecord.php because I want this functionality available to all my AR classes. However I receive this exception when the method is called. “Fatal error: Cannot instantiate abstract class CActiveRecord in \yii\framework\db\ar\CActiveRecord.php on line 361”. My guess is that it has to do with the self::model() call, but I don’t know what to change that to.


	public function findRecentActive($limit=10) {

		$conditions = array('condition'=>'active=:active',

							'params'=>array(':active'=>true),

							'order'=>'t.modified DESC','limit'=>$limit);

		$result = self::model()->findAll($conditions);

		return $result;

	}

I am calling the method in a widget like so:


       $model = new Hero;

       $results =  $model->findRecentActive(5);

it’s better to create a new class extending CActiveRecord instead of changing it.

… or a behavior

CActiveRecord class is abstract. With "self::model()" you are trying to create an instance of this class, what is impossible. As mentioned above, you can create an own class ActiveRecord extending CActiveRecord, put it into components directory and use it wherever you want.

The proper way to do this would be to create a base class for all of your models. In this base class; call it BaseModel for instance; you should define a named scope. The scope could then be used as needed on any AR find() method, not just findAll().

I do not think this type of filter should be in a behavior.

I just want to add my two cents about modifying the framework. Don’t do it. You will get burned.

Yii is fairly extensible in almost all areas, especially in AR. Changing framework code is unneccessary.