Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Updating Many-to-Many relationships
  • So I have got my relationships working, got it creating them. But am struggling to actually update the relationship. Thats saving new elements, or deleting elements. foreach($genres as $key => $genre)
    {
    $show->genres[] = Model_Genre::find($genre);
    }
    foreach($users as $key => $user)
    {
    $show->users[] = Model_User::find($user);
    } Am doing the above to set the elements. But when ever I try to update them I get various errors and have various issues. Also thought I should mention my information am trying to save is sent to my controller in the format of 1,2,3,... I then explode that into a array, so what ever I do I need to loop that exploded array. It would also be nice if I can check too see if its already in the database as I don't want too do lots of database requests. I spoke to berrymedia on IRC and he said later he would post some notes on it but also thought if anyone else has some advise/tutorials on how to do this would appreciate it. I've tried various methods on saving new/updated information.
  • Moved the post to the ORM forum. Without telling us what the errors are, or what issues you have, we can only guess. In case of update, are some related objects already present? Shouldn't you check if $show->genres[$genre] exists before adding it again? Deleting a related object is as simple as "unset($show->genre[$genre]);". They will deleted when you save the object.
  • Harro Verton wrote on Friday 23rd of December 2011:
    Moved the post to the ORM forum. Without telling us what the errors are, or what issues you have, we can only guess. In case of update, are some related objects already present? Shouldn't you check if $show->genres[$genre] exists before adding it again? Deleting a related object is as simple as "unset($show->genre[$genre]);". They will deleted when you save the object.
    Weird thing is I tried the unset method and that didn't work either, when $show->save() was executed and the page refreshed the elements were still there.
  • Cascade_save disabled on that relation? If so, $show->save() will only save the show object, and not the changes to the related objects.
  • Harro Verton wrote on Friday 23rd of December 2011:
    Cascade_save disabled on that relation? If so, $show->save() will only save the show object, and not the changes to the related objects.

    That fixed adding, just now need to sort deleting objects. Edit: I just thought cascade_delete? But I don't actually want it too delete the genre out of the database. only the link.
  • This should be enough to delete them:
    // unlinked all related genres not present in $genres
    foreach ($show->genres as $key => $unused)
    {
        in_array($key, $genres) or unset($show->genres[$key]);
    }
    $show->save();
    
  • Harro Verton wrote on Friday 23rd of December 2011:
    This should be enough to delete them:
    // unlinked all related genres not present in $genres
    foreach ($show->genres as $key => $unused)
    {
        in_array($key, $genres) or unset($show->genres[$key]);
    }
    $show->save();
    

    I get a unexpected unset error with that code. Edit: Sorted it cause unset returns a void its not valid php statement. Have to use if statement Edit: Edit: This no longer works for editing? The cascade save sorted it originally but this new code no longer works for it now... Edit: Edit: Edit: Got it all going with crazy nested foreach loops and in_array checks. If I get some time I'll post up a explanation for others.
  • Yeah, sorry, unset() is a language construct, and doesn't work like that. You basically need two loops. One like this, that deletes all relations for genres that are not selected, and one that adds genres that where selected, but are not related yet. You call save() once after these two loops.
  • hello,
    $school = Model_School::find(1001); $schoolStaff = Model_SchoolStaffinfo::find(1);
    unset($school->staff_infos[$schoolStaff->id]); $school->save(); here many-to-many relationship exist between
    Model_School and Model_SchoolStaffinfo with('cascade_save' => true,'cascade_delete' => true,) .
    this code is running sccessfully without error but when i refresh page then schoolStaff still remains unchanged.means staff Related with school not deleted . also i have checked unset working properly.
    can u please help me to resolve this issue.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion