beforeSave method common to all AR classes

Hi,

I have a few functions that I share across all of the beforeSave methods of my AR-based models. Is there a way to define these functions in only one beforeSave method?

I thought about using behaviors but I’m not sure that is the right way. Besides, in this article I read that a behavior cannot override a base class method: http://www.yiiframework.com/wiki/44/behaviors-events

I also tried using an intermediary class, e.g. MyActiveRecord which extends CActiveRecord, and extending all my AR classes from that - but MyActiveRecord needs a table.

Which is the simplest way to do this?

Thanks

gm

Creating a base ActiveRecord is exactly how you should do it…

What do you mean by "MyActiveRecord needs a table"?

Yii complains about not finding a table named MyActiveRecord in the app database, even if I don’t use the class directly but extend it with other classes which have a related table. If I set the method MyActiveRecord::tableName() to return false, Yii complains about a missing table “”.

Also, if I define MyActiveRecord as an abstract class, I get a fatal error saying "Cannot instantiate abstract class MyActiveRecord".

Solved: I HAD to define MyActiveRecord as an abstract class. The fatal error was caused by defining the static method model() only in MyActiveRecord instead of in each child class.

Also, the tableName() method does not seem to be required in the base class.

Thanks!

You don’t actually need to define it as abstarct class, but you must define the model() method for each ActiveRecord class you directly use. tableName() is not required for the parent class in this case.