Reputation: 31
I'm trying to display the actual value as opposed to the ID Number using cakePHP.
These are my Tables Roles Table Users Table l
This is my UsersTable code
class UsersTable extends Table
{
public function initialize(array $config)
{
$this->hasOne('Roles');
}
This is my UsersController code
public function list()
{
$usersTable = $this->Users->find('all')->contain(['Roles']);
$allUsers = $usersTable->toArray();
$this->set("allUsers", $allUsers);
}
This is how im trying to print it
$user->role->name
However I am getting the following error Error
What am I doing wrong and how do I fix this issues?
Upvotes: 0
Views: 31
Reputation: 644
In your UsersTable, the relation order is wrong, it's saying that user has one role, so this will try to join the roles table using a key on the roles table. It should actually be belongsTo, since User belongs to a role. Try this:
class UsersTable extends Table
{
public function initialize(array $config)
{
$this->belongsTo('Roles');
}
}
Upvotes: 1