I want to create a 3 table relationship User -> user_roles <- roles
User table: id, role_id
User_roles: id, user_id, role_id
Roles: id, code
I did:
Users.php
$this->hasMany(
'id',
'UserRoles',
'user_id'
);
UserRoles.php
$this->belongsTo(
'user_id',
'Users',
'id'
);
$this->belongsTo(
'role_type_id',
'Roles',
'id'
);
Roles.php
$this->hasMany(
'id',
'UserRoles',
'role_type_id'
);
And I want simply in my controller:
$users = Users::find();
foreach ($users as $user) {
echo $user->id;
echo $user->roles->code;
}
But get: Access to undefined property Users::roles
Can anyone help me solve this problem? Reference: https://docs.phalcon.io/en/3.3/db-models-relationships