CodeIgniter Forums
Datamapper many to many relationship - Printable Version

+- CodeIgniter Forums (
+-- Forum: Archived Discussions (
+--- Forum: Archived Libraries & Helpers (
+--- Thread: Datamapper many to many relationship (/showthread.php?tid=58475)

Datamapper many to many relationship - El Forum - 06-14-2013


I'm using the Datamapper ORM (and that's great :o), but I'm facing an issue when managing a many to many relationship.
I've got a user table, and a role table. Each user can have several roles. Each roles is related to several users.
The problem is, I don't know how to retrieve the role from all users. I'm trying to develop an admin interface in which there's a table showing all the user main information : usename, email, status and role.

I tried this in my controller:

$users = new User();
$users->include_related('role', 'name')->get_iterated();
$this->load->view('admin/users/index',array('users' => $users));

And in my view :
      foreach($users as $u):
...//other code to retrieve user information
<?php echo $u->role_name; ?>

When doing this, the result is a table where I actually see all my users. However, if a user has for example 3 roles, There's 3 rows showing his information, and each row has one of his role. For exemple, if the user has Test name and the roles of admin and customer, I end with this table :
Name Role
Test customer
Test admin

I don't understand... Maybe I don't do good in my controller, but I have no idea how to do it Sad
I'm sure this must be a simple issue, but I really can't understand what's going on... Sad

Any help would be really appreciated. ^o^

Datamapper many to many relationship - El Forum - 06-14-2013

You could try

$users->include_related('role', 'name', TRUE, TRUE)->get_iterated();

but I doubt it works in a many-to-many relationship.

Datamapper doesn't hydrate related objects (in most cases). It was designed many years ago (around CI 1.4), when instantiating objects was a very expensive business. Which is why include_related() returns the joined result. I haven't used CI in years, so it's very unlikely that I will find the time and the will to address this, since it requires quite a bit of redesign.

The alternative is to use only get the users, and get the roles in your loop, but that means N+1 queries, which isn't really an option.

Datamapper many to many relationship - El Forum - 06-14-2013

Thanks for your help, now I understand the problem and know what I must do. Trust me, it's quite a progress Smile

May I ask you a question ? How would I retrieve a single user's roles ? Would I face the same issues ?

Thanks again for your help.

Datamapper many to many relationship - El Forum - 06-14-2013

From a code's point of view, there is no difference between getting one and getting all.

If you have a single (existing) user object, you can do
which will get all roles of that user (assuming 'role' is the name of your relation).

Datamapper many to many relationship - El Forum - 06-17-2013

Great !
Thanks a lot, I manage to get all of this working by following your advice Smile

Thanks again for taking the time to explain this to me and answer my questions; that means a lot for a newbie like me Wink
And by the way, great job on the DataMapper librairy!