Yii's Mvc Implementation

I’m still trying to fully gasp the proper MVC design. When I generate models and controllers with Gii, certain code placement doesn’t make sense to me. For instance, generating a Controller will create accessRules method


	public function accessRules()

	{

		return array(

			array('allow',  // allow all users to perform 'index' and 'view' actions

				'actions'=>array('index','view'),

				'users'=>array('*'),

			),

			array('allow', // allow authenticated user to perform 'create' and 'update' actions

				'actions'=>array('create','update'),

				'users'=>array('@'),

			),

			array('allow', // allow admin user to perform 'admin' and 'delete' actions

				'actions'=>array('admin','delete'),

				'users'=>array('admin'),

			),

			array('deny',  // deny all users

				'users'=>array('*'),

			),

		);

	}

This makes sense, we have a controller and it directs views which information is appropriate to whom. However, glancing at the models, I see attributeLabels


	public function attributeLabels()

	{

		return array(

			'id' => 'ID',

			'name' => 'Name',

			'description' => 'Description',

			'create_time' => 'Create Time',

			'create_user_id' => 'Create User',

			'update_time' => 'Update Time',

			'update_user_id' => 'Update User',

		);

	}

why is this inside a model and not a part of a controller? I thought models are for calculations and database interaction and controllers are all about what and how things should be displayed.

I would say that attributeLabels() are more appropriate in the view and not in the controller.

Controller is a glue between views and models and should only check permissions, work with GET/POST data, trigger models loading / saving and render views.

But Yii does not have separate "View" object and views are just php templates (you can put some logic inside it, but it is better to avoid this), so by default this goes into the model.

Also standard widgets / helpers like CForm rely on this and by default show filed labels using data configured in attributeLabels().

So you right and this logic does not belong to the model and this is just a Yii default, but you are free to remove this from your models and configure labels explicitly.

I think the label of a model attribute should be defined in model class, otherwise we would have a lot of duplicated code. Think about the fact that a model can be used everywhere. Many views of different kinds will display the same model. It is also true with the controllers. Where is the most suitable place to store the labels in a centralized fashion? IMO it’s model.

Model is a database record at its bottom layer. And it’s also a data entity in the middle and a business logic at the top. And on the surface of the business logic are the attribute labels. Isn’t it a kind of business logic how people call those data entities?