Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Removing many_to_many relations
  • I'm a bit confused about this. So I have two models (let's say plates and forks) that are linked via a many to many relation in ORM. On the edit screen for plates, I have a selection box that allows the user to select multiple forks. When the form is saved, I need to know which forks were un-selected so I can break the linkage, if needed. So what I was thinking of doing was looping through $plates->forks and then unset($forks[$i]) with each iteration, then, in a new loop, create a linkage for each selected fork. This method pretty much works (not sure if it's the best), but the problem is that not all of the forks are "unsetting" (the first fork is always linked to the plate). (plate.php in the public function action_edit($id = null) method):
    http://scrp.at/beV I tried using 1 as the initial value for the counter $i and <= $count, but that doesn't work, either. Thanks for any insight you can give!
  • I wish things like this were documented a bit better. I just had the same issue and was trying to use ->delete() on the relationships.
    If I had realized you can just set it to an empty array that would have made much more sense. Thanks though - once again the forums were extremely helpful.
  • They aren't indexed incrementally from 0, they're indexed by their IDs (primary key). Thus your method of unsetting them will never work. If you want to clean them all out the quickest way would just be to do $plate->forks = array().
  • Thank you so much for that! It totally makes sense that they would be indexed by their primary keys.
  • Good one Jelmer. That solves my problem. Thanks!

Howdy, Stranger!

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

In this Discussion