Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Custom Observer throwing error, not found
  • **edit added correct code

    I'm trying to add a custom observer to a model the observer is located in 

    /classes/observer/gcm.php

    class Observer_Gcm extends Orm\Observer
    {
          public function after_insert(Orm\Model $model){
                \Log::debug('after insert observer');
          }
    }

    and then in my Model

    protected static $_observers = array(
              'Orm\Observer_CreatedAt',
              'Orm\Observer_UpdatedAt',
              
              'Observer_Gcm'=>array(
                  'events'=>array('after_insert')
              )
          );

    But it throws the error that the class doens't exist found in /packages/orm/classes/model.php 1511

    $observer_class = \Inflector::get_namespace($observer).'Observer_'.\Inflector::denamespace($observer);
    if ( ! class_exists($observer_class))
    {
    throw new \UnexpectedValueException($observer);
    }


  • Ok i sorta got it to work but I had to manually edit the Orm bootstrap file to add the class

    'Orm\\Observer_Gcm'        => __DIR__.'/classes/observer/gcm.php',

    and put the actual observer with the others in the Orm package... this seems like a terrible way to do this
  • HarroHarro
    Accepted Answer
    Your custom observer should not be in the Orm namespace, since that namespace is reserved for the Orm package (which is a core package and should not be altered).

    Any other namespace will do, this works fine:

    protected static $_observers = array(
              'Orm\Observer_CreatedAt',
              'Orm\Observer_UpdatedAt',
              
              'My\Own\Observer_Gcm'=>array(
                  'events'=>array('after_insert')
              )
          );

Howdy, Stranger!

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

In this Discussion