Hello, im a beginner with DataMapper ORM, so i have question about complex querying.
First, here is simplified data objects:
class User
property :id, Serial
property :login, String
has n, :actions
end
class Item
property :id, Serial
property :title
has n, :actions
has n, :users, :through => :actions
end
class Action
property :user_id, Integer
property :item_id, Integer
belongs_to :item
belongs_to :user
end
This is how data in db looks like:
+ ------- + + ------- + + ------- +
| Users | | Items | | Actions |
+ ------- + + ------- + + ------- +
| 1 | u1 | | 3 | i1 | | 1 | 4 |
| 2 | u2 | | 4 | i2 | | 1 | 3 |
| ....... | | 5 | i3 | | 1 | 4 |
+ ------- + | ....... | | 1 | 5 |
+ ------- + | 1 | 6 |
| 1 | 3 |
| ....... |
+ ------- +
So, for example User 1 has viewed some items N time. And what i cant figure out, how to select items and their action amount relating to user.
For example, the result for user 1 should be like this:
+ -------------------- |
| Items (item_id, num) |
+ -------------------- |
| 3, 2 |
| 4, 2 |
| 5, 1 |
| 6, 1 |
+ -------------------- +
P.S. regular SQL query that matches my needs:
SELECT i.id, i.title, COUNT(*) as 'num'
FROM actions a
JOIN items i on i.id = a.item_id
WHERE a.user_id = {USERID}
GROUP by a.id
ORDER BY num DESC
LIMIT 10;
So, how to do this and is there are any docs about complex datamapper queries?