We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Three table relationship problem

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

edited Jul '18

Users.php

$this->hasMany(
  'id',
  'UserRoles',
  'user_id'
);

You can access UserRoles like this:

foreach($users->UserRoles as $userRole) {
    // logic
    $userRole->code;
}

Users.php

$this->hasOne(
  'id',
  'UserRoles',
  'user_id'
);

You can access UserRoles like this:

$users->UserRole->code;

Notice that hasMany returns an array of models, while hasOne returns a single model.

With your model layout, you should be able to access roles like this:

$users = Users::find();

 foreach ($users as $user) {
            echo 'user #', $user->id, PHP_EOL;
            foreach($user->UserRoles as $userRole) {
                echo $userRole->code, PHP_EOL;
            }
}


931
edited Jul '18

Thanks, I get this thing, I got an array, I need to loop trough it. But the problem is, when I try to access $user->UserRoles in

foreach($user->UserRoles as $userRole) {
                echo $userRole->code, PHP_EOL;
            }

phalcon says: Access to undefined property Users::userRoles

but it is defined, I have defined it by

$this->hasMany(
  'id',
  'UserRoles',
  'user_id'
);

Or haven' t I?

btw. hasMany is what I want, I want to get an array, thanks for that.



931
Accepted
answer

I got it, I just needed to set aliases, idk why, but somehow phalcon didn't know what UserRoles is until I have set an alias for this relation table like so:

Users.php

$this->hasMany(
            'id',
            'B2a\Backend\UsersRoles',
            'user_id',
            array('alias' => 'usersRoles')
        );

UsersRoles.php

$this->belongsTo(
            'user_id',
            'B2a\Backend\Users',
            'id'
        );

        $this->belongsTo(
            'role_type_id',
            'B2a\Backend\Roles',
            'id',
            array('alias' => 'roles')
        );

Should work without aliases if you use the correct table name.

PHP reported Access to undefined property Users::userRoles, but your table name is UserRoles, with capital first letter.

But aliases are fine too ;]



931

This is what bothered me at first too, error said: undefined userRoles, but I had NONE line of code with userRoles, just UserRoles. Strange behaviour.

Should work without aliases if you use the correct table name.

PHP reported Access to undefined property Users::userRoles, but your table name is UserRoles, with capital first letter.

But aliases are fine too ;]