Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm using Yii for a few years and only right now I realized, that their demo application (empowered by CRUD auto-generation tool -- Gii) seems to be breaking key MVC architecture pattern. I'd like to discuss this here.

As long as I've been using MVC (about four years), I was always told, that view should not acquire data directly from model. Every piece of text about MVC, I've read so far, was always underlining, that both view and models should be completely separate and the controller is the only element, that spins them together. If I need any model data in view (most often used mechanism) I should let my controller to retrieve it and feed view with it.

Correct me, if I'm wrong, but finding this question and an answer to it makes me sure, that I'm right: while it is technically possible, it should be avoided.

So, in terms of Yii, that would be:

$probes = Probes::model()->getAllProbesForCurrentUser;

$this->render('index', array
(
    'probes'=>$probes
))

(quite obvious, I think -- controller retrieves data from model and renders view, feeding it with that model)

If all above is right, then how should I explain this:

<?php $this->widget('CGridView', array
(
    'id'=>'users-grid',
    'dataProvider'=>$model->search(),
    'columns'=>array
    (
        'name',
        'email',
        'level'
    )
)); ?>

(a widget placed in view acquires data directly from model)

What am I missing?

share|improve this question
    
I have just realized, that I based my question on completely wrong assumption and therefore I made myself an complete idiot! :] –  trejder Jun 22 at 13:20
1  
This isn't a bad question and may help others who start out with a similar misconception. –  Bart van Ingen Schenau Jun 22 at 13:41
    
@trejder - You're not the first to be tripped up by the many variants of MVC which includes MVP and MVVM. I voted to leave your question open as others are likely to make the same mistake in reviewing a sample. Running across this question may provide value to them so they can learn the nomenclature involved. –  GlenH7 Jun 22 at 14:24

2 Answers 2

up vote 4 down vote accepted

The array can also called a ViewModel, it is different from the Model because it carries the data the view needs while the Model represent the application business entities. For simple CRUD form they are likely to be identical. But for complex form, a ViewModel can be combined from many models.

share|improve this answer
    
Thank you for your effort in trying to answer my question, however I have just realized, that I based my question on completely wrong assumption and therefore I made myself an complete idiot! :] Voting to close/delete my own question. –  trejder Jun 22 at 13:19

This question is based on a completely wrong assumption. Of course, $model variable, used in widget, is retrieved by a controller and fed to view in as natural way as it can be in MVC world.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.