Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
_many_many Observer
  • Is there an easy way to have an observer related to _many_many relationships?  Ideally a way to monitor creation/deletion.  Since _many_many tables have no model of their own this seems a bit difficult (unless I switch to a full model with _has_many on either side, which would not be ideal).  An option would be to check in the before_save of the related models, but this seems inefficient given that almost all save requests will not have to deal with the _many_many relationships in question.  Is there another way than the two mentioned above?

  • What exactly do you want to accomplish? Reading this you mean the relations through table?

  • Yes, the through table.  The simplest example would be stats tracking on when entries are added to the through table - computed values in other tables etc.  Been playing with it all morning, currently I'm using a get_diff() check on the before_save() event of either table to accomplish the functionality, was wondering if there's a better way.
  • HarroHarro
    Accepted Answer
    No, the through table wasn't designed to contain anything other then the two FK's.

    For fancy stuff, creating a model for it, with two has_many/belong_to's, is the best option. You can use that alongside the existing many_many, so you can use one to access the through table, one to access the related objects.

    It will still require manual updates to the through table when you assign new relations though, I'm afraid the observer is your best bet for that.
  • Thanks for confirming that Harro, appreciate it!

Howdy, Stranger!

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

In this Discussion