So far I created 3 tables :
users, roles and users_roles and created phalcon project by using Phalcon tools as well as generated models, controllers and views (php) by scaffolding tables with Phalcon tools. I have established working many-to-many relationship between tables users and roles via intermediate table users_roles.
My intention is to show search results in 'user' search view in this manner - generated table shold be like this
TABLE:
USER ROLES
user1 role1,role2,role3
user2 role2
user3 role1,role3
Many-to-many relationship in Users model is :
public function initialize()
{
$this->hasManyToMany('id',
'UsersRoles',
'user_id','role_id',
'Roles',
'id',
['alias' => 'UserRoles']); <--- alias is a must. it wil be used later as 'userRoles' property in foreach...
}
THE ANSWER
The code above should be in view , here is the code :
<table class="table table-bordered">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>User roles</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<?php foreach ($page->items as $user): ?>
<tr>
<td><?php echo $user->id ?></td>
<td><?php echo $user->name ?></td>
<td><?php $roles = $user->userRoles; <------- code starts here
$tmp=[];
foreach ($roles as $role)
{
$tmp[] = $role->name;
}
echo join(',',$tmp);
?></td> <------ until here
<td><?php echo $this->tag->linkTo(["users/edit/" . $user->id, "Edit"]); ?></td>
<td><?php echo $this->tag->linkTo(["users/delete/" . $user->id, "Delete"]); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
I hope this will help someone.. Regards.