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:

public function save(PropelPDO $con = null)
{
foreach (sfMixer::getCallables('BaseSampleModel:save:pre') as $callable)
{
$affectedRows = call_user_func($callable, $this, $con);
if (is_int($affectedRows))
{
return $affectedRows;
}
}
...
}


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

It's internal rule creation for behavior events:

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)

sfPropelBehavior::registerHooks('before_instance_creation', array(':save:post' => array('SampleModel', 'createNewParamInstance')));
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


class SampleModel extends BaseSampleModel
{
...
public static function createNewParamInstance($obj) {
// GO !
}
}

No comments: