Closed Gabriel-Arthur closed 3 years ago
Hi Gabriel,
I think what is most important here, is to understant that includeRelated
is the same as doing a join. So in your first example, you get 2 users returned. One for each related role.
This code should explain.
Data::debug(get_class($this), "Joining user table with roles table. Resulting in one row for each role");
$userModel = new UserModel();
$user = $userModel
->includeRelated(RoleModel::class)
->where('id', 1)
->find();
Data::debug($user->allToArray());
Data::debug(get_class($this), "Fetching roles user afterwards will put all roles on the user object");
$userModel = new UserModel();
$user = $userModel->find(1);
$user->roles->find();
Data::debug($user->toArray());
Resulting in
Here is the complete project I used to test this issue. CI4Playground.zip
Omg, this is so simple!!
Thank you !!
Following the documentation I have the database structure below
how can i get all the roles of a user, because the code below
returns
I can even return correctly in an array like this
doing something like that
but, the instance of User don't have all the Roles and I can't set.
This is a problem?
Thanks a lot!