![]() |
Datamapper many to many relationship - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22) +--- Thread: Datamapper many to many relationship (/showthread.php?tid=58475) |
Datamapper many to many relationship - El Forum - 06-14-2013 [eluser]Noraj[/eluser] Hi! 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: Code: $users = new User(); And in my view : Code: <?php 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 ![]() I'm sure this must be a simple issue, but I really can't understand what's going on... ![]() Any help would be really appreciated. ^o^ Thanks! Datamapper many to many relationship - El Forum - 06-14-2013 [eluser]WanWizard[/eluser] You could try Code: $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 [eluser]Noraj[/eluser] Thanks for your help, now I understand the problem and know what I must do. Trust me, it's quite a progress ![]() 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 [eluser]WanWizard[/eluser] 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 Code: $user->role->get(); Datamapper many to many relationship - El Forum - 06-17-2013 [eluser]Noraj[/eluser] Great ! Thanks a lot, I manage to get all of this working by following your advice ![]() Thanks again for taking the time to explain this to me and answer my questions; that means a lot for a newbie like me ![]() And by the way, great job on the DataMapper librairy! |