MVC Yii Fundementals

My questions revolves around the ‘best practice’ on creating controllers and models in the Yii framework.

My database is currently;

Billing (one-many) Site (one-many) Job (one-many) JobEntries (one-many) JobTime/JobItem/JobNote

Billing: is our client’s billing details, contains billing address, terms etc

Site: is our client’s physical site details and there can be multiple site addresses for a billable client

Job: contains the job header details for scheduled work we do for a client

JobEntries: is a table being used to tie multiple work entries we do to a particular job and ties time/item/notes to it.

The 3 tables JobTime, JobItem, JobNote each contain data for time, hardware sold, and notes per entry.

The database structure is designed so we can have multiple time, item, note entries per job entry, and multiple job entries for the job.

I currently have a controller called ClientController that manipulates the Billing and Site models and it works nicely. I read somewhere controller model was a 1:1. Is this correct? or am I doing aok in making a controller handle multiple models?

Reason I ask is I am about to code the JobController which will handle the Job, JobEntries, JobTime, JobItem, JobNote models.

Many thanks for your clarification guys!


It all depends on database design and table relations…

For example if you have two non related models like Post and Car… then for creating a new record you would need to have two methods like createPost() and createCar()… in this case it’s better to have separate controllers so that each one has a create() method…

On the other hand if the tables are related and especially if the data for related tables can be entered together with the main table… than it makes sense to have one controller that will handle data to different models…

Many thanks for replying and yes, they are related. For example, I have a action called NewClient in the Client controller which creates a new billing and new site record. I will also have a NewSite action which adds additional sites to the main billing record.

The JobController will manage quite a few more models so was eager to make sure I wasn’t on the wrong track with my thought process! so thank you!

(The database is innodb and all tables contrained by FK.)

You need to think about this taking into account all your need and dependencies…

As you say…

you will have two actions newClient() and newSite()… so in this case those two models use separate create actions (even as they are related) and maybe it would be better if they had separate controllers…

You might find that your controller will become very large, very quickly since you’ll have to duplicate all CRUD operations 4 times for each entity. I vote for separating your controllers: cleaner URLs & easy to read/debug.

http:/domain/client/123/createSite // Create

http:/domain/client/updateSite/456 // Update

http:/domain/client/deleteSite/456 // Delete

http:/domain/client/site/456 // Read

http:/domain/client/123/createBilling // Create

http:/domain/client/updateBilling/456 // Update

http:/domain/client/deleteBilling/456 // Delete

http:/domain/client/billing/456 // Read



Ok I understand where you are heading but what if the views are going to be mainly multi-model forms?


NewClient adds a new billing and new site record in the one form.

NewJobEntry will be adding a jobentry, 1 or more jobtime, 1 or more jobitem and/or 1 or more jobnote records in the one form…

The idea is that when a technican updates an existing job for a client, he/she will be adding in multiple time/item/note entries to the update.

ps. One a side note, this structure is very much like a forum, except each post can have multiple components (ie time/item/note)


If one view (form) enters data for two models… then you just need one create action not two…

Check this wiki article for some ideas -

Yes, that is a very good article you linked, I have read that and it is very good. In fact I used it already as the basis for my NewClient action/view :)

With respect to one controller per multi-model Vs one:one controller/model I’m guessing, in my case, the one controller for mutiple models is the best approach.