Universal model for similar tables


My project has a lot of table which having almost the same column.

For now I created many models normally according to those tables but I found out that each model seems to have the same functions and properties. Is it possible to create universal model to serve many tables that have similar column?

Personally I think it’s possible but not yet trying anything since I have some approaches which are:-

  1. Create 1 model and use it entirely for all similar tables. While using constructor to help create different model for each table (such as set table name).

  2. Create 1 model as based model to group same functions together and create another sub-model to extend this based model. Each sub-model contain property specifically for each tables (table name, things like that)

Those “similar” tables are dynamic tables that generated from another system (but will not be deleted or altered after generated) and they share some columns. And more of them will be generated in the future. I don’t want to create new model every time new table comes (I’ll do it if it is only way). And I don’t want to repeat myself for every new create model (that also mean repeat unit test file!).

Any suggestion?

Thanks in advance.

You could go with either option 2 - or option 3 which is a behavior.

A behavior is probably a cleaner way.

it’s called abstract class…

you can create base model…

and the sub model of course extends the abstract class/based model


Thanks, gonna try out now!

Hi there, that’s a promising topic for the newbie that I am :rolleyes:

but more guidance is needed …

I’m working on a webapp with different content types: Post, File, Gallery, Category and so on

They all share some common columns names: title, description, category_id, status …

I understand the way 2. : Post, Gallery File models can extend a ContentType model where I can define some common validation rules, relations rules, scopes …

Q1: But how to use behaviors as jacmoe says ?

I need also some common actions like publish/unpublish, category filtering.

I imagine that my PostController, FileController … can extend a ContentTypeController.

Q2: Where to put this ContentTypeController, in components or in controller directory ?

Q3: Will, saying PostController, inherit the access rules for the actions publish/unpublish, category filtering as defined in the parent ContentTypeController ?

Thanks for help.