[eluser]imohammad[/eluser]
I had no idea that you can use where_related multiple times.
You have been a huge help for me during this learning process and I'm so thankful Simon!
Let me make the same previous example but a bit harder.
Let's say we want the same result in the previous example and in addition, we want to filter them by their rates.
Code:
So I have the following tabe (in addition to the previous tables):
Users_rates
Users_Rates Columns:
- ID
- User_id
- rating
And we want to calculate the average of the rating for each user, and we filter the retrieved users based on the average rate.
Code:
Users_Rates dummy data:
1
1
4
2
1
5
3
2
3
4
2
5
And I want to include the average column with the result. I don't think I can use Include_related()? because this is a one to many relation.
I'm thinking of all possible examples, and I'm thankful for your help a lot!
- Mohammed
[quote author="Maglok" date="1406210862"]Yeah I know how ORM needs a bit of a different approach in thinking.
So you want users that have a certain skill and/or a certain group. You can do that like this:
Code:
// Instance your objects
$group = new Group();
$user = new User();
$skill = new Skill();
// Get a bunch of groups
$group->where('title', 'dev')->get();
// Combine the get for the skills and get them
$skill->where_in('title', array('php', 'java'))->get();
// Get the set that has the related $group and the related $skill
$this->where_related($group)->where_related($skill)->get();
This is just a example, I didn't test it.
And important thing is to remember that you cannot just do another get() on a instance of a datamapper object and expect it to remember the previous result.[/quote]