I have an "after_insert" observer that creates a database entry which logs the insert event. Is there any way to pass controller data to my observer? My observer definition looks like this:
No, the observer observes actions in the ORM Model, it is not aware something like a controller even exists.
The observer does have access to the current ORM object, so you could store the data in the ORM object in your controller, and retrieve it in the observer. You need to be on 1.5+ for this, earlier versions of the ORM don't support custom data.
To store custom data, just do
// assume this is your model object $ticket->my_special_user_data_property = $user_data;
I'm having trouble upgrading my project to 1.5, but that is a topic for a different post.
I have an abstract base controller which every other controller in my application extends. It handles site-wide necessities such as authentication and storage of user data. I have multiple observers which need access to data that is stored in the base controller. Rather than explicitly passing the necessary data to each model, I created a static $instance variable in my base controller, and in my base controller's before() method, I have:
if (!isset(self::$instance))
{
self::$instance = $this;
}
Now, from my observer method, I can:
$controller = \Controller\Base::$instance;
and subsequently I can access all of the data stored there from the context of the called controller. It seems to work just fine. Is there anything fundamentally wrong with this approach?
Bummer, but I see what you mean. If I were to use the model from, say, the command line interface, it would fail. Or, if I were to implement an API that didn't extend my base controller, that would also be an issue. I guess I'll fix it after I figure out how to (properly) upgrade to 1.5.
ARGH!!! It happened today! An API call that saves ticket data triggered the after_update observer, and it broke everything...damn. Stupid controller coupling.