How to extend an ActiveRecord Model


I’m trying to create a form in Yii that is paged. Each page is for a group of fields. The best way to do this, I figured, would be to extend the ActiveRecord model and add a parameter for the current page. This also allows me to override the rules() methods with my own rules for the form itself.

My problem with this is, Yii doesn’t allow us to natively extend models. I had to override the getMetaData() method because Yii runs self::model(get_class($this)) on itself, which ends up breaking everything.

Not only that, but I can’t use findByPk because Yii relies heavily on the class name.

I know of tons of work arounds to make this work, but I’m wondering if I’m missing something.



I think the way to go would be to just use scenarios (one for each page).

Hi Kyle

Maybe there’s a less imaginative solution: why don’t you handle each group of fields into a fieldset, and handle their visibility with jQuery depending on form page?

If it’s a wizard and you want to validate each step before allowing the next one, you’ll have a little more work in order to validate just the visible attributes.

But if your form is tabbed, you would change nothing to the validation.

I had both use cases, and I handled it like I said above.

Cheers :)

You can also read here on SO: Yii multi page form wizard best practice

I am. However, there’s a lot of rules to my form for each page, and that doesn’t solve the fact that I have a “page” attribute on my form model, but not my AR model. I wanted to keep the ideas separate.