CActiveRecord::find() and multiple models

Hi,

I have a single table of pages. Pages can be of different types. Not all

types share the same fields or validation rules so I have a different model

for each type. What I want is to get an array of models according to the

type of page when performing CActiveRecord::find() or similar function.

Is this possible?

I don’t believe your approach is correct.

Therefore, this is how i think you should do it:

Create a single model for your table and differentiate the content types with a WHERE type=? condition in AR:




->find('type=:t',array(':t'=>'article'));



This will help you retrieve the records based on their types.

For saving/updating data in the model use scenarios.

If a content type requires certain fields, just create a scenario for those fields.

Take a look at the scenarios usage in the documentation for more info.

You can use this method…

http://www.yiiframework.com/doc/api/1.1/CActiveRecord/#instantiate-detail

Thank you, that’s exactly what I was searching for. I have considered

using twisted1919’s approach, but the model becomes vary complex when

there are many types of pages and it’s a lot harder to add new types.