Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How do I set a model observer from outside of the model
  • Title says it all. For example, lets say I have a plugin system, utilizing modules, and I want them to be able to subscribe to or "hook" into some of my app's models. What's the best way to do this in Fuel? Thanks!
  • Please excuse me. I wasn't real clear as to what I'm looking for. Currently, you can register an observer by adding something like this to your model:
    class Model_Comment extends \Orm\Model {
    
        protected static $_properties = array('id', 'email', 'url', 'content');
        
        protected static $_table_name = 'comments';
    
        protected static $_observers = array(
            'example', // will call Observer_Example class for all events
            'Orm\\Observer_CreatedOn' => array('before_insert'), // will only call Orm\Observer_CreatedOn at before_insert event
        );
    }
    

    But, what I want to do is, register an observer for a model OUTSIDE of the model, not inside the model like in the code shown above. Something like how the Event class works I guess. Theoretically, I would have to register the observer somewhere like so:
    Model_Comment::register_observer('Observer_Akismet', array('events' => array('before_save')));
    

    I have done some sifting through the Orm and haven't been able to find such a method. Does anyone know if something is scheduled for the future? Thanks for the help!
  • Please add a feature request for this at http://github.com/fuel/orm/issues.
  • An observer is anything that is callable, to it could be a closure, an object definition ( like array($object, 'method) ) or a static method ( "class::method" ). See http://php.net/manual/en/function.is-callable.php on what PHP considers a valid callback.

Howdy, Stranger!

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

In this Discussion