ViewModel concept in Yii?

(Oleg) #1

In ASP.NET MVC there is such thing like ViewModel.
It works like this: To view we could bound only one model and this model called ViewModel. Access to data entities goes through this model.

Why it is good?

  1. It helps with separation of concerns.
    Whan application growing it starts happening a lot in view or in controller.
  2. IDE friendly.

Why it is bad?

  1. We need to create and maintain additional class
  2. In ViewModel should be only model related code, needed for view, not models itself. Unexperienced user can put general code for model processing in ViewModel.

I suppose this pattern could exist in the form of concept, for example, this approach cloud be described in manual and ViewModel cloud be optionally generated by Gii

(demonking) #2

it’s not the same like a ModelForm?

Why not write an own generator for gii?

(Alexander Makarov) #3

It is form model.

(Oleg) #4

This is a render method from one of my projects. Only $model is bound to form.

        $result = $this->render($view, [
                'model' => $model,
                'source' => $source,
                'searchType' => $searchType,
                'dataProvider' => $dataProvider,
                'soundex' => $soundex,
                'similarDataProvider' => $similarDataProvider,
                'messages' => $messages,
                'allpersonsResult' => $allpersonsResult

(Oleg) #5

What bad it leads to a mess in a view file.
Of course we could say it is a bad design, but you start from 1 model, and keep adding functionaly during years and it grows to a monster. Symfony for example force you to use EntityRepositories to avoid such problem.

(Oleg) #6

The real problem is, in my opinion, that there is no place for code for advanced application screen. We could have few forms (interconnected or not) plus output area with customisation features and advanced hierarchical structure. And this concept does not fit Form Model because it not about form it about View in general.


This is application area. If you don’t like to pass multiple variables to view, you can create entity to group them and passs it to view:

        $result = $this->render($view, new MyViewEntity([
                'model' => $model,
                'source' => $source,
                'searchType' => $searchType,
                'dataProvider' => $dataProvider,
                'soundex' => $soundex,
                'similarDataProvider' => $similarDataProvider,
                'messages' => $messages,
                'allpersonsResult' => $allpersonsResult

And then use $data->model in view. You don’t need any special tools from framework for that.

(Oleg) #8

Yes I can and I do, but this a point with framework - to provide solutions and learn good practices.

(Oleg) #9

Moreover I believe you did not catch idea completely, the idea is not to group models

(Oleg) #10

(Alexander Makarov) #11

View model, additionally to holding data, contains view logic such as formatting values or checking for permissions to render menu item. That is entirely possible with current Yii 1, Yii 2 or Yii 3.

The missing part is, indeed, docs that this is possible and why/when it’s a good idea.

@olegl84 would be great to get a wiki article for 2.0 explaining the concept. We could probably make it part of the 3.0 extra guide if we’ll decide to do something like “patterns book”.

(Alexander Makarov) #12

Created an issue not to forget about it:

(Oleg) #13

Good, I will contribute about View Model.

(Jasco) #14