Basic Mvc Logic When Using Custom Cform/controller


I got 3 tables: Users, Flags and Answers. Answers gots FlagID and UserID (many to many). On top of these models I made a CForm GameForm which defines HTML Form data. Then a GameController takes this user input (FlagID, FlagValue and, somewhere in the back, UserID) and compares with FlagValue in Flag table. If match, a record is added in the table Answers (flagID, UserID, time).

My questions: is it good practice

  1. if the GameController does all the compare and logic by communicating with the three Models only? Or should it interact with their Controllers?

  2. to call a method in Flag model (to interact with DB), I have to create a Flag Model instance in my GameController ?

this is my first PHP application. I’ve been reading a lot these days on Yii (so many good things!), so hadn’t courage to screen all topics, sorry…


Hi mattima,

welcome to Yii!

a)Controllers could use a variety of models (not only the main model).

b)Controllers should avoid call methods of another controller (if it is possible).

So the common code could be in a Component class as static methods (reusability)

So you could make methods in a Component that be called both of two(or more) controllers.

  1. It depends what you want to do.

For example:

If you want to save/update/delete one record then may you should make an instance.

If you want to check multiple records may should be a static method and call like that


Thanks KonApaz.

This clarifies how I can proceed.



Call the other models in the one controller.

Depends on the method. If you are just finding out if the ‘flag_id’ exists then


No need for an instance. I would lean toward the only reason to create an instance is is you are CRUDing. Just getting info, i’d go with Flag::model()->.

I quite often set up a public function getDataList() in my models for use with dropDownLists. I would call it from what ever controller by:


Something I’ve been doing on my sites is generating controllers for each of the main items in the mainmenu, then actions within for each of the ‘sub-items’. In one site, there is a User Profile that can be modified by only the logged in user. I have User and Profile models, but they are accessed through the SiteController actionViewProfile(), actionUpdateProfile().

Thanks a lot Jkofsky.