Welcome Guest, Not a member yet? Register   Sign In
Datamapper question
#1

[eluser]The Mask[/eluser]
Hi,
I'm having problems trying to work out how to retrieve columns from two tables that have a many-to-many relationship and also a where clause on one of the tables.
i.e. groups can have many users and users can belong to many groups. The relationships are set up in the models so anyone know how I can retrieve what the following SQL would using datamapper?

select u.username, u.email, g.groupname, g.postcode from users u inner join groups_users gu on u.id = gu.user_id inner join groups g on g.id = gu.group_id where g.admin = 1;

Thanks
#2

[eluser]The Mask[/eluser]
Also, any ideas how the following is accomplished with Datamapper?

select u.username, sum(s.points) AS total
from users u
inner join scores s on u.id = s.user_id
group by u.username

models: user, score (user can have many scores)

Thanks
#3

[eluser]BrianDHall[/eluser]
On the first one I think it would be something like:

Code:
$g = new Group();

$g->get_where('admin', 1)->user->get();

There is also the Get Advanced manual page as well which uses some cool magic functions, but I have yet to have a need for them.

As for sum, the new and improved search feature on the DMZ manual is fantastic:

http://www.overzealous.com/dmz/pages/get.html

Check on $object->select_sum();
#4

[eluser]The Mask[/eluser]
On the first one (using id < 10 for testing), this happens:

$g = new Group();
$g->get_where('id < 10');
// generates SELECT * FROM (`groups`) WHERE `groups`.`id` < 10
// So all fine and as expected (9 rows shown).

But...
$g->get_where('id < 10')->user->get();
// SELECT `users`.* FROM (`users`) LEFT OUTER JOIN `groups_users` as groups_users ON `users`.`id` = `groups_users`.`user_id` LEFT OUTER JOIN `groups` as groups ON `groups`.`id` = `groups_users`.`group_id` WHERE `members`.`id` = 1


Can anybody explain why it has `members`.`id` = 1
I was hoping to get back a collection of:
GroupID, GroupName, UserID, UserName ... for the 9 groups (several users for each group)




Theme © iAndrew 2016 - Forum software by © MyBB