Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Where do observers go?
  • I have created a custom observer and placed the file in:

    fuel/app/classes/observers/structure.php

    It looks like this:

    <?php

    namespace Orm;

    class Observer_Structure extends Observer
    {
    public function after_insert()
    {
    \Log::info('Successfully called observer');
    }
    }

    and I have modified one of my models with the following $_observers:

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

    When the model tries to trigger the observer, I get this error:

    UnexpectedValueException [ Error ]: Orm\Observer_Structure
    PKGPATH/orm/classes/model.php @ line 1356

    I have also tried:

    protected static $_observers = array(
    'Orm\Observer\Structure' => array(
    'events' => array('after_insert'),
    ),
    );

    I then get this error:

    UnexpectedValueException [ Error ]: Orm\Observer\Structure
    PKGPATH/orm/classes/model.php @ line 1356

    and also:

    protected static $_observers = array(
    'Orm\Observer\Observer_Structure' => array(
    'events' => array('after_insert'),
    ),
    );

    that gives me this error:

    UnexpectedValueException [ Error ]: Orm\Observer\Observer_Structure
    PKGPATH/orm/classes/model.php @ line 1356

    I have also tried locating the observer file at:

    fuel/app/classes/orm/observers/structure.php

    but I get the same three errors as above.

    My questions are:

    - Where do I put observers for my application?
    - What do I name the observer file?
    - How do I call the observer?

  • bharoldbharold
    Accepted Answer
    Custom observers belong in:

    /fuel/app/classes/orm/observer

    The file should be named after the observer. For example, if the observer is Observer_Structure, the file name should be structure.php

    The correct syntax for the $_observers in the model for this example is:

    'Orm\Observer_Structure' => array(
    'events' => array('after_insert'),
    ),

  • If somebody else wants to comment on this thread I will mark it as answered. Apparently you cannot accept your own response as an answer.
  • I can do that for you. :-)

    btw, there is no set location, you can put observers anywhere you want. I have mine for example in fuel/app/classes/model/observer, and I use

    'Model\Observer\Structure' => 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