Let's say I have model "Question".
Every question is created by user (current user).
How to "auto" update createdBy to current.user
In Doctrine2 I should have event listener with dependency on security.context.
And event will subscribe for preSave(), setting $question->setCreatedBy( $context->getToken()->getUser());
How to achieve this with Propel2 ? I could set createdBy in controller, but this is ugly :(
I could write custom behavior, but how to access security.context from the behavior ?
After ~ half year I found working solution :)
Idea: Model will have setter injection for
Event Dispatcher. Onpre-savemodel will fire event (validation/user injection etc..). This way I require ED for save. I can select objects from the DB without injected ED. Dependency manager will manage "repository". Repo will be able to inject all required dependencies on the model and then call save.$depepndanciesManager->getModelRepo->save($model). Witch will do:$model->setEventDispacher($this->getEventDispacher); $model->save();Example of Model:
Example of Repository:
Example usage from controller:
Example config:
Config is based on symfony framework. Real implementation:
Links may not work, project is in active development!