2009-01-20

pre- , post- or instead- :: domain object processing

If you need to run somth action -before, -after or instead of object saving you could use "Behavior" Propel feature.

for example - generated code contains such block:
  1. public function save(PropelPDO $con = null)  
  2. {  
  3.    foreach (sfMixer::getCallables('BaseSampleModel:save:pre'as $callable)  
  4.    {  
  5.      $affectedRows = call_user_func($callable$this$con);  
  6.      if (is_int($affectedRows))  
  7.      {  
  8.        return $affectedRows;  
  9.      }  
  10.    }  
  11. ...  
  12. }  


on callable getting framework searches for 'BaseSampleModel:save:pre'

It's internal rule creation for behavior events:
  1. sfMixer::register('Base'.$class.$hook$callable);  


For calling somth. before model saving:
1. add to propel.ini
propel.builder.addBehaviors = true

2. Register handlers (you can add code to the end of domain object)
  1. sfPropelBehavior::registerHooks('before_instance_creation'array(':save:post' => array('SampleModel''createNewParamInstance')));  
  2. sfPropelBehavior::add('SampleModel'array('before_instance_creation'));  

after that - hook will be registered. so - on hook handling SampleModel::createNewParamInstance will be executed.
3. Add callable
  1. <!--php<br /-->  
  2. class SampleModel extends BaseSampleModel  
  3. {  
  4.  ...  
  5.  public static function createNewParamInstance($obj) {  
  6.   // GO !  
  7.  }  
  8. }  

No comments: