Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
passing the function called to observer
  • I'm pretty sure this isn't possible, but just wanted to double check. Is there any way to let an observer know which model function triggered the event? For instance, I have a Model_User_Token, with a function earn() that inserts a record into the user_tokens table. Is there any way I can let an observer watching for insert events know that the earn function is what inserted the record? Thanks in advance!
  • I realized a more general form of my question above is whether it is possible to pass variables to observers. So for instance, here are my observers :
    protected static $_observers = array(
      'Orm\\Observer_CreatedAt' => array(
        'events' => array('before_insert'),
        'mysql_timestamp' => false,
      ),
      'Activity' => array(
        'events' => array('after_insert'),
        'activity' => $_activity
      )
    );
    

    With this I get a parse error - unexpected variable $_activity. Maybe I'm doing something completely dumb (always possible!), or is there some reason why we can't have variables in the observer configs? Thanks for any help!
  • This is how any observer is called: https://github.com/fuel/orm/blob/1.1/develop/classes/model.php#L1187 It gets passed just the instance and the event that triggered it. If you want to give it more information the proper way to do it would be to introduce your own event and call $obj->observe('my_event') when it happens. You can however add properties to the observer that can be used as config for that particular model, you can see an example of that here: https://github.com/fuel/orm/blob/1.1/develop/classes/observer/updatedat.php#L30 But what you are trying to do is impossible, you are adding a non-existant variable which PHP will never accept as you found out by the error message that exactly describes your problem.
  • Thanks Jelmer! That's what I figured. Also, good to know you can make custom events! I didn't realize that. Thanks for the help. :)

Howdy, Stranger!

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

In this Discussion