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.

Volt and many to many - output

Hello again,

I begin to fall in love with Phalcon O:)

But I've got a little problem. I cannot get the idea of how to access fields from a many to many relationship in Volt-output.

I've got two tables: 'persons' and 'names'. I've got a third table named 'personslastnames' (yes, and another one 'personsfirsnames').

Now I wanted to show a table of all stored persons - I created one with phpmyadmin for the test. The models were all created by Phalcon-Dev-Tools (phalcon all-models --relations).

PersonsController: public function indexAction() { $this->view->title = 'LIST'; $this->view->persons = Persons::find(); }

index.volt: ... <tbody> {% for person in persons %} <tr> <td>{{person.id}}</td> <td>{{person.persons_lastnames.name}}</td> ...

The fields from Person are shown, but for the many-to-many-fields I got errors: Notice: Access to undefined property Persons::personslastnames in C:\xampp\htdocs\slipbox\app\cache\cxampphtdocsslipboxappviewspersons_index.volt.php on line 23

So next error is clear: Notice: Trying to get property of non-object in C:\xampp\htdocs\slipbox\app\cache\c_xampphtdocsslipboxappviewspersons_index.volt.php on line 23

In the documentation I cannot find the clue how to access the variable. Do I have to make another Query in the model? Thaught Phalcon does the needed things automatically?!

Thanks a lot - again Aljoscha

By doing this in index.volt I get the ID of the name, but I want the name O:) ... <td>{{person.id}}</td> <td> {%for lastname in person.PersonsLastnames%} {{lastname.names_id}} {% endfor %} </td> ...



5.1k
Accepted
answer
edited Jun '14

All should work if you got your Model relations properly set up.

There's a discrepancy in the snippets (which are a bit hard to read btw.. :P ;)) you're giving; persons_lastnames vs PersonsLastnames

Deleted all models and let Phalcon-Dev-Tools create them again. Now everything works fine, using:

{% for lastname in person.PersonsLastnames %} {{lastname.Names.name}} {% endfor %}

Thanks alot!

edited Jun '14

@AljoschaPeters

When you post code in your coments, try to always use this sintax:

 ```phalcon
    #your code here
 ```

Example:

 ```phalcon
 $this->session->get("username");
 ```

Becomes:

$this->session->get("username");

This way your code get automatically highlighted and becames easier for everyone to understand it. :)