1

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?

3
  • I have just realized, that I based my question on completely wrong assumption and therefore I made myself an complete idiot! :]
    – trejder
    Commented Jun 22, 2014 at 13:20
  • 1
    This isn't a bad question and may help others who start out with a similar misconception. Commented Jun 22, 2014 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.
    – user53019
    Commented Jun 22, 2014 at 14:24

2 Answers 2

4

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.

1
  • 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
    Commented Jun 22, 2014 at 13:19
-1

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.

3
  • Dear ugly downvoter. Have enough courage to express any problems in this answer, that caused you to downvote it?
    – trejder
    Commented Sep 3, 2015 at 6:43
  • Sorry, maybe I shouldn't have downvoted you, but it bugs me you reduced MVC to "if the variable is from the controller then it's MVC"... yes, functionally is correct, but conceptually this way of thinking can lead to problems in the long run, just look around for the fat-controller/fat-model patterns and how people are trying to eliminate them in the MVC frameworks. It all comes down into really thinking and knowing what you are doing and how it could impact in the future. Commented Sep 9, 2015 at 21:47
  • @Nab I understand and agree with you, that I may have gone too far with simplifying the meaning or definition of MVC. But, this is not a forum, where we can discuss MVC. This is a Q&A site, where I put an answer to my own question to notify, that I misunderstood the basics and should not ask at all.
    – trejder
    Commented Sep 10, 2015 at 6:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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