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.