• Edit
  • Delete

symfony event dispatcher cheat sheet

http://www.symfony-project.org/book/1_2/17-Extending-Symfony

 

Create a notifier

notify

The registered methods of the listeners are executed

    $this->dispatcher->notify(new sfEvent($this, 'rest_request.fetch_prepare', array(
      'uri'        => $uri,
      'parameters' => $parameters
    )));

notifyUntil

The first registered method that returns true of the listeners is executed

    $this->dispatcher->notifyUntil(new sfEvent($this, 'rest_request.fetch_prepare', array(
      'uri'        => $uri,
      'parameters' => $parameters
    )));

 

filter

The listener can change values

    $values = sfContext::getInstance()->getEventDispatcher()->filter(
        new sfEvent($this, 'form.update_object'), $values
    )->getReturnValue();

(getting dispatcher from the symfony context)

 

 


Connect a listener to the notifier

In class ullMetaWidgetPassword:

$this->dispatcher->connect('form.update_object', array('ullMetaWidgetPassword', 'listenToUpdateObjectEvent'));

(getting dispatcher from within a objects attribute)

 

The method to be called by the notifier in case of filter

 

  public static function listenToUpdateObjectEvent(sfEvent $event, $values)
  {
    if ($values['password'] == '')
    {
      unset($values['password']);
    }

    return $values;
  }

 

In case of a notify:

  public static function listenToUpdateSingleColumnEvent(sfEvent $event)
  {
    $params = $event->getParameters();
   
    $column = $params['column'];
    $booking = $params['object'];
   
    // to something  
  }

 


Use symfony event dispatcher in doctrine models

sfProjectConfiguration::getActive()->getEventDispatcher()->notify(new sfEvent($this, 'foo.bar'))