I have been trying to use events feature. It realy helps to do many things easily.
I just wonder why Yii events feature does not allowing send parameters directly. There even exists ‘params’ field at CEvent class.
actualy task was to make payment handler which will raise onSuccess event, and system will execute needed actions. But payment manager should work with various of payment methods. Some payment methods do not completing imediatelly, they are completing after some time by payment provider and payment provider sending request to our side (paypal, 2co etc).
So, to handle this i needed to serialize and save event handlers to the database for next use.
But i noticed that the anonymous function will not be generated properly from saved serialized string because it saves only link to it. And link will exists while current script runing.
To have saving event abiltiy i needed to bypass using anonymous functions. So i needed to create function under some class and use following code:
$obj->onSuccess = array($user,'doSomeActions');
this code allows save events to db:
public function beforeSave(){
parent::beforeSave();
foreach (array('onSuccess','onError') as $name){
$events[$name] = $this->getEventHandlers($name);
}
$this->events = serialize($events);
return true;
}
So here we came to base problem of this topic - event parameters.
Finally, i found way to save event handlers into database for next use, but still cant save event parameters.
It would be realy nice if event function under CComponent class will allow to pass paramters into it. for example by this way:
$obj->onSuccess = array($user,'doSomeActions',$params); // thrid paramter will be used as parameter
and following change can be made at raiseEvent function:
list($object,$method,$params)=$handler;
........
else if(method_exists($object,$method))
$object->$method($event,$params);
In any case thank you for best framework.