Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] Need example of set_join_fields to update multiple rows
#1

[eluser]Genki1[/eluser]
Hi,

I have a many-to-many join table and want to update every row that is related to $object.

As the manual says, I've set $model to a "model name" or "relationship key" but I just can't get the multiple rows to update.

Please give an example using "model name" and an example using "relationship key".

Thank you

Reference: http://datamapper.wanwizard.eu/pages/joinfields.html
#2

[eluser]WanWizard[/eluser]
Code:
// mark this alarm as fired
$alarm->set_join_field($user, 'wasfired', TRUE);

// mark all alarms related to users as fired
$alarm->set_join_field('user', 'wasfired', TRUE);

You only need the "relationship key" instead of the model name if you have the defined the relationship as advanced, and used a custom name for the relation.

If this doesn't work, what is the result of $alarm->check_last_query()?
#3

[eluser]Genki1[/eluser]
Hi WanWizard, check_last_query() gave me the info I needed. Thank you for the prompt response.

BTW, the example for set_join_fields() still refers to "$user" where it should be "$u".

Thanks for taking over the project!
#4

[eluser]WanWizard[/eluser]
I know, it's cosmetic, and I haven't had a lot of time lately to work on low-prio stuff. Got some other projects to finish...
#5

[eluser]Genki1[/eluser]
I understand. But, it's not just cosmetic (such as formatting), it's an error which is confusing because that parameter allows different values and that makes it confusing, so especially to new users of the program. That's why different folks are mentioning it. Thanks again for your efforts.
#6

[eluser]WanWizard[/eluser]
Wrong choice of words, I meant is isn't affecting the functionality of the library...
#7

[eluser]Genki1[/eluser]
As to my original question about set_join_field, I'm not getting the result I expect. In the following example, only one row in the join table gets updated:

Code:
$u = new User();
$u->where('first_name', 'john')->get();  // $u now contains exactly one row

$alarm = new Alarm();
$alarm->where_in('alarm_type', 'urgent')->get();   // $alarm now contains multiple rows

// DESIRED RESULT: in the join table,
//                 for user JOHN and each of his alarms which are URGENT,
//                 set REQUIRED = TRUE
$u->set_join_field($alarm, 'required', TRUE);

// ACTUAL RESULT: in the join table, only one record gets updated.

I checked in datamapper.php and the parameter $related_fields does correctly contain the MULTIPLE records I want to change. However, after line 5278, only ONE record is available:

Code:
// $object correctly contains multiple records
$object = array($object) // line 5278
// $object now contains only ONE record

In set_join_field, should I be passing something other than $alarm? Any advice you can give is greatly appreciated.
#8

[eluser]WanWizard[/eluser]
I don't have a development environment at the moment (thanks to ^%#&!@$ Dell ), so without testing, could you try changing that to
Code:
if ( $object instanceOf Datamapper )
{
    $object = $object->all;
}

if ( ! is_array($object))
{
    $object = array($object);
}

Alternatively, you could also
Code:
$u->set_join_field($alarm->all, 'required', TRUE);
that would probably work without code changes. Maybe this syntax should be added to the docs.
#9

[eluser]Genki1[/eluser]
Adding those lines to DataMapper.php worked. Much joy!

Since great minds think alike, I had already tried
Code:
$alarm->all
but received:

Quote:An Error Was Encountered
Unable to relate iif_field with 0.
#10

[eluser]WanWizard[/eluser]
Never seen that before, I'll have to look at that, because I prefer to use $object->all...




Theme © iAndrew 2016 - Forum software by © MyBB