Extending Cactiverecord Problems

Hi there,

I would like to provide to all my model classes an easy function to load a model which would be defined in my ownd ActiveRecord class like this:

class ActiveRecord extends CActiveRecord{

public static function load($id) {


    $model = self::model()->findByPk($id);


    if ($model === null)


        throw new CHttpException(404, 'The requested page does not exist.');


    return $model;


}

}

all my models would extend that class:

class MyModel extends ActiveRecord{

public static function model($className = __CLASS__) {


    return parent::model($className);


}

}

The problem is that when I call MyModel::load($id) the code fails all the time in the model() method of CActiveRecord. The wrong class name is passed:

"Fatal error: Cannot instantiate abstract class CActiveRecord in C:\xampp\htdocs\yii\framework\db\ar\CActiveRecord.php on line 386"

This is because "self::model()" in load() applies to ActiveRecord and not the MyModel class. Basically the model() function of MyModel is never called.

It seems an easy task but I can’t find a work around. Any idea or alternative solution?

Thanks

Renaud

it’s a static method so here self refers to the class where it has been implementd not where it’s been called you may send the class name to it like what you did in model method or if you use php 5.3+ you can use static instead of self which refers to the current class.

Thanks Reza that’s exactly what I was looking for, it works!

Cheers

Renaud