You are viewing revision #2 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.
Where to put what ¶
Many time, writing widgets or editing Gii code, there can be the doubt if the code is following the MVC patter or not.
A few suggestion can help to detected the most evident error of style.
Controller ¶
In the controller there should be the code for collect user input, retrive models from database, rendering views.
In a controller there should never be:
- html code (it should be in the view)
- sql code (there should never be, if is needed it should stay in a model)
- field name they should be in the view.
Avoiding embedding field names allow you to change the database with minimal effort.
Views ¶
In the views (and only in the views) there should be the html code.
In the view there should not be:
- user input ($_GET, $_POST) input should be collected in models in the controllers, never in views.
- sql code: if is neede, better to crete functions in the models
The less php operation there are in the views, the best it is. Instead of concatnate 2 fields, is better to write a getter method, so you can reuse in other views.
Models ¶
The models are used for collect user inputs and access to the database.
In the models there should not be:
- user input ($_GET, $_POST)you should write a function that will be called in the controller.
- html code (it should be in the view)
Widgets ¶
Widgets has a behavior similar to controllers. If a widget is supposed to create lot of html, better to create a view file for it.
re junxiong
This part of field name is the most weak of the article.
What I mean, is that the field name (property of model) shold appear in the model, for example in the specification of rules, attribute label, search and so on.
Should appear in the view, in $form->textField($model, 'fieldName').
About the controller, I think that is better to avoid to hardcode the field names.
The default Gii code creates the function search() and setAttributes, in order to delegate to the model the deals with the actual database structure.
If you have to set some default value, I think that the doing in the controller is correct too.
For example, if you save 2 models in relation belongs to, you will save the main model first, and on the related you will do something like:
$model->foreignKey_field=$model->primary_key;
In this case, you are writing in the controller the name of the foreign key field, and I think that this is an absolutely stylish exception to my rules.
If you want to be more strict, as Yii provide an alias for primary key, you can add a function in the related model like:
public function setMainModelId($id)
{
$this->foreignKey_field=$id;
}
This will alias the field name for the foreign key field, and will allow you to change the database implementation without touching the controller.
As the change of the name of a foreign key field is an even more than rare in real life, I 100% quote the first option.
Definitive guide
This article has been reviewed, expanded and included in the definitive guide by samdark.
Read also this chapther of the guide.
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.