Hi all:
As I understand the MVC concept of programming,
-
the (barebone) view should only handle the presentation,
-
the (thin) controller is the intermediary between the view and,
-
the (fat) model, where the business logic should reside.
I realize that a strict implementation of the above is not always practical nor feasible so the rules can be broken. However, I’ve noticed that in many of the code samples posted here it’s pretty common practice to have the view interact directly with the model, especially when using a database. As a MVC/PHP/Yii newbie myself I know I’ve done it a lot. For example, to load a dropDownList from the database I see code in the view similar to the following:
<div class="row">
<?php echo $form->labelEx($model,'mymodel_country_code'); ?>
<?php
$modelCountries = Countries::model()->findAll();
$listCountries = CHtml::listData($modelCountries, 'country_code', 'country_name');
echo CHtml::activeDropDownList($mymodel, 'mymodel_country_code', $listCountries, array('empty' => '(Select country)'));
?>
<?php echo $form->error($mymodel,'mymodel_country_code'); ?>
</div>
The above tells me that the view:
[list=1]
[*]is querying the model directly,
[*]formatting the retrieved data,
[*]rendering the dropDownList (which is also active therefore directly updating the model), and
[*] doing error processing.
[/list]
I think that even combining the statements into one long php expression makes no difference. Are these conclusions correct?
To me this approach is not scalable and will incur in additional bandwidth costs from the hosting provider as the website usage grows, plus it’s not ‘elegant’. I would prefer to have the view just accept input data and render the page, the controller handling the message traffic (with perhaps a minimal amount of processing of its own) and finally the model doing the heavy work of processing business logic and database interaction.
My question is, do y’all have any suggestions on how to improve on this scenario (using Ajax perhaps)? Specific scenarios and code samples would be greatly appreciated. Thanks.