Multiple models for the same table

Noob question: Is it possible to define which columns from the table I would like to use in the model? So I can define several models for the same database table, with different column sets?

Background: Real estate database - single table for different real estate types (apartment, house…) - some columns are shared, some type-specific and unused for different types - I would like to define different YII models for each type, so I can use Gii and have appropriate views etc for each.

Thanks!

I figured that I could create MySQL views on the main table for each type and create Yii models based on them. Would this be the best approach and are there any downsides to it? Thanks again!

NO, the best approach would be to normalize your database table(s), at least till you reach 3NF.

What you want to achieve is commonly known as single table inheritance. There’s a wiki tutorial on it here:

http://www.yiiframework.com/wiki/198/single-table-inheritance/

1 Like

Of course, that’s obvious and I agree, but, unfortunately, changing the model is currently out of the question…And, also, although I really love when things are clean and neatly modeled, I have to admit normalizing the db model would, in this case, only complicate all the involved logic - both on database and application side.