3

I am looking for a way for modules to interact with ORM models in other modules using FuelPHP.

For example if I had a module entitled products, it'll come with some default behavior whereby products are selected from the products table etc. That's fine.

The problem I'm trying to solve is allowing modules/packages which may or may not always exist, to interact with the products module.

For example, if I enabled an inventory module, it would need to add a relationship in the products model on the fly to allow Model_Product::find()->get() to be extended to retrieve inventory information where necessary. So the above code could change to

Model_Product::find()->related('inventory')->get()

This demonstrates a further issue whereby calls to related('inventory') in code would throw an exception when the inventory module hasn't been loaded (and in turn created the relationship).

Conceptually I'm struggling to find a way to have the modules interact.

I believe I could solve the first issue by creating a method similar to the below in the model

public static function _init()
{
     $data = array(
          '_has_many' => static::$_has_many,
          [...]
     );

     $additional_relationships = \Event::trigger('event_name', $data, 'array');

     foreach($additional_relationships as $key => $value)
     {
          static::$$key = $value;
     }
}

But this seems less than ideal. It also doesn't solve the second problem.

halfer
  • 19,824
  • 17
  • 99
  • 186
Ben Swinburne
  • 25,669
  • 10
  • 69
  • 108

1 Answers1

0

You've got a bit of a chicken and egg situation here. You can design a pull system (like you suggest) or a push system (like I would do it), but both will only capture the state of that specific moment. This challenge requires constant updates as models get loaded.

Perhaps you can solve it with an intermediate class. Have this class define a notifier event, which is triggered by every model on-load, and gets passed the relationship information of that model. Have every model register an update event callback, so that the notifier can update the models when new information comes in. The notifier should also be able to pass all data it has collected before the model is loaded. It's going to be quite a challenge to keep everything in sync.

All this will not prevent an exception in case you run a query with a related model but that model isn't accessible (i.e. the relation is not defined).

WanWizard
  • 2,574
  • 15
  • 13