Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Multiple model variable in Volt template

Hello,

i had this problem month ago with phalcon 2 and my solution was the same as shown(with different models). But now in phalcon 3.0 it seems, that it doesn't work anymore.

Paper.php

public function initialize(){
   $this->belongsTo('usersId', __NAMESPACE__ . '\Users', 'id', [
       'alias' => 'user'
   ]);
}

User.php

$this->hasMany('id', __NAMESPACE__ . '\Papers', 'usersId', [
    'alias' => 'papers',
    'foreignKey' => [
        'message' => 'Paper cannot be deleted because he/she has activity in the system'
    ]
]);

paper/index.volt -> this is working in user/index.volt

{% for p in user.papers %}
   {{ p.diverse }}
{% endfor %}

I want in my paper/index.volt different varibales from different models, also in other templates. How can i do this?

Rgds Stefan

What exactly is a problem ? Also you should really use modelsManager :).



17.3k
edited Aug '16

Hi,

that the variable user is undefinied, normally it should work ... or not?

Yes it should work. Just check that this is actually volt by var_dump($user->papers) before setting variable to volt



17.3k

Hi,

the result is null. I am not in user controller now, i am in paper controller

http://localhost/vokuro/paper

When i set the url to

http://localhost/vokuro/user/paper

It works.

Isn't it just like the relation between the models. When i call an object i can call user -> paper -> diverse

edited Aug '16

But urls doesn't have anything to do about this. Where you set this user variable for view ?



17.3k

Maybe i have an understanding problem, but isn't this the variable? 'alias' => 'user'

I mean from where user appears in view ?



17.3k

Sorry but now i am completely be stumped.

Do i have to use setVar() to create $user variable? I understand that, when i have a relation between the models, i can simply use {{ user.papers.diverse }} in volt, because of the alias

So i have a PaperController.php with just an indexAction() and inside there is nothing more. In paper/index.volt i want to display the papers/documents of an user with id 123.

edited Aug '16

Yes, obviously you need to use setVar to have it appear in view and as well find it first men(or use model binding from 3.0.0). Like

$dispatcher->setModelBinding(true); // ind your dispatcher service

php public indexAction(User $user) { $this->view->setVar('user', $user); } ```

alias is just alias, for accessing it from model like$user->papers; or $paper->user;

It won't magically appear in view :D



17.3k
Accepted
answer

Hello Wojciech Ślawski,

now i got it and understand ;-)

my papers/index.volt

            {% for p in user.paper %}
                {{ p.diverse }}
            {% endfor %}

            {% for s in user.successLogins %}
                {{ s.userAgent }}
            {% endfor %}

PapersController.php

public function indexAction($id){
    $user = $this->view->user = Users::findFirst($id);
    $this->view->setVar('user',$user);
}

Thx for the thoughts abutting

Rgds Stefan