Welcome Guest, Not a member yet? Register   Sign In
DataMapper ORM v1.8.2

[eluser]WanWizard[/eluser]
[quote author="matyhaty" date="1348565422"]Is there a way of doing an AND, or some sort of merge on two seperate result sets (even if that result set is just a load of ID's that would do!)[/quote]
Only by lopping over the result, and adding a where() for every record found.

[eluser]WanWizard[/eluser]
@Maglok,

Datamapper supports deep queries, like $parent->where_related('child/grandchild', 'column', 'value'), but afaik that isn't implemented for has_many relations, as potentionally you'll get an N * M resultset.

[eluser]WanWizard[/eluser]
@rick20,

The correct syntax is:
Code:
$user = new User(1);
$item = new Item();
$item->where_join_field($user, 'status', 'active')->get();
which will give all items related to user 1 with a status = 'active'.

[eluser]rick20[/eluser]
@WanWizard,
I already tried that one, but it doesn't work, I guess because I change the relationshiop key name in my models.
I have to change the relationship key name because there are 2 different relationships between User and Item, one is 'favorites', and the other one is 'orders'.
Is there any alternative way around?
Thank you

[eluser]WanWizard[/eluser]
A quick look in the code reveals that you can either pass an object (in which $this->model is used to find the relation), or a string (which should be the relation name).

In the first case you have the requirement that the relation must have the same name as the class, in the second you can't set a filter on a specific object.

Perhaps you can work around it using
Code:
$user = new User(1);
$item = new Item();
$item->where_join_field('orders', 'status', 'active')->where('user_id', $user->id)->get(); // you might have to prefix "user_id"

[eluser]rick20[/eluser]
@WanWizard,

I just made the relationship key name identically same between User and Item (the relation name is 'orders' in $has_many variables of User and Item model).
And then I tried to run the code but it outputs an error :

Code:
Unknown column 'mc_orders.status' in 'where clause'

SELECT * FROM (`mc_item`) WHERE `mc_orders`.`status` = 'active' AND `user_id` = 1

It is weird that the query doesn't include table mc_orders using this code.
What am I doing wrong here?

[eluser]WanWizard[/eluser]
If you do
Code:
$parent->where_join_field()->...
there is no technical need to include the child table in the query. That would mean an extra join, a slower query, and more CPU and memory usage. So Datamapper doesn't include it.

If you do need the child, use
Code:
$parent->child->where_join_field()->...

In your example I assumed that 'status' was a column in your join table. If it isn't, you should not be using where_join_field(), but just where().

[eluser]rick20[/eluser]
@WanWizard,
Sorry, I think I find myself not clear enough about the usage of where_join_field()
If
Code:
$parent->where_join_field()->...
does not include the join-table, then how does the query recognize the join-field name?

If I take a look back on the documentation http://datamapper.wanwizard.eu/pages/get...join_field
I'm sure I'm quite understand with this query
Code:
$user->alarm->where_join_field($user, 'wasfired', FALSE)->get();

However, I think it only works if model user has_many alarm, and model alarm has_many user, with join-table user_alarm. I mean, both models have basic and minimum many-to-many relationship setup.

But in my case, where I change the has_many relationship to 'orders', the where_join_field() seems doesn't want to work, even though I'm definitely sure that I follow exactly the example code.

As for the field 'status', you are correct, the field 'status' is in my join-table, and I want to filter the main table based on it.

Really appreciate your help to solve this issue.
Thanks

[eluser]Andy78[/eluser]
Not used datamapper in a while set everything up as you should (I think!) Trying to do a simple get but getting the following error:

Cannot access empty property in......datamapper.php on line 2370

Is there a simple cause for this?

[eluser]WanWizard[/eluser]
@rick20,

What I mean is the following:

Say you have the tables "parents" -> 'children_parents" -> "children". Two tables in a many-to-many relation, and a join table relating the two. Say you have a column in the join table called "field".

Code:
$parent->where_join_field('child', 'field', 'value')->get();
will select all parent records which have a join table record in which the column 'field' has the value 'value'. Note that for this query, the 'child' table doesn't need to be joined.

So, if you do
Code:
$parent->where_join_field('child', 'child.field', 'value')->get();
you would get your error, since there's no table in the query called 'child'.

The first parameter of *_related() is the relation name the query is on (and not the model or table on the other side).




Theme © iAndrew 2016 - Forum software by © MyBB