Hello! It’s been a while since my last post but I ran into a rather frustrating issue yesterday and didn’t want anyone else to have to repeat it. According to the official Doctrine 2 docs you should try to flush a limited number of times per request.
Note Do not invoke flush after every change to an entity or every single invocation of persist/remove/merge/… This is an anti-pattern and unnecessarily reduces the performance of your application. Instead, form units of work that operate on your objects and call flush when you are done. While serving a single HTTP request there should be usually no need for invoking flush more than 0-2 times.
Source
While debating how to go about this I remembered controller plugins and how handy they can be for controlling very stages of the request. For anyone who hasn’t read it I’d suggest checking out the post by Matthew O’Phinney. A quick summary of hooks can be found below:
-
routeStartup()
: prior to routing the request -
routeShutdown()
: after routing the request -
dispatchLoopStartup()
: prior to entering the dispatch loop -
preDispatch()
: prior to dispatching an individual action -
postDispatch()
: after dispatching an individual action -
dispatchLoopShutdown()
: after completing the dispatch loop
The majority of the work from the EntityManager will come from the controller action’s so the hook we’re interested in is the postDispatch() hook. A few minutes later and we have our plugin.
// File located at My/Controller/Plugin/EntityManager.php namespace My\Controller\Plugin; use Zend_Controller_Plugin_Abstract, Zend_Controller_Front; class EntityManager extends Zend_Controller_Plugin_Abstract { /** * Flush the EntityManager. * * (non-PHPdoc) * @see Zend_Controller_Plugin_Abstract::dispatchLoopShutdown() */ public function postDispatch($request) { $bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap'); $em = $bootstrap->getResource('EntityManager'); $em->flush(); } }
// APPLICATION_PATH/configs/application.ini resources.frontController.plugins[] = "My\Controller\Plugin\EntityManager"
Once that’s setup your ready to go with one small caveat. If you’re a fan of $this->_helper->redirector() in the controller’s you going to have an issue because the redirector forces an exit after redirecting the response. In order for this to work properly you will need to change those to $this->_helper->redirector->setGotoSimple(); which doesn’t force an exit. You do not need to change all the redirector’s. Only ones where a flush is required. For example,
use Services\Game, Services\Profile; class EditProfileController extends Zend_Controller_Action { public $gameService; public $profileService; public function preDispatch() { $this->profileService = new Profile(); $profile = $this->profileService->findProfile($this->_request->id); if (!$profile || !$profile->getGame() || !$this->profileService->isAllowed('update')) { // postDispatch isn't needed so we save a cycle or two and just call redirector per normal return $this->_helper->redirector('index', 'profiles'); } $form = $profile->getProfileForm( array( 'game' => $profile->getGame(), 'method' => 'post', 'action' => '/edit-profile/process?id=' . $this->_request->id)); $form->populateFromModel($profile); $this->view->profileForm = $form; $this->view->profileModel = $profile; $this->view->gameModel = $profile->getGame(); } public function indexAction() {} public function processAction() { $request = $this->getRequest(); if (!$request->isPost()) { // postDispatch isn't needed so we save a cycle or two and just call redirector per normal return $this->_helper->redirector('index', 'profiles'); } if (!$this->profileService->updateProfile($request->getPost())) { return $this->render('index'); } // The update profile call was successful so we need to make sure the postDispatch() gets ran. $this->_helper->redirector->setGotoSimple('index', 'profiles'); } }
Welp, that about sums it up! I hope I saved someone else the hassle I went through trying to understand why Doctrine 2 wasn’t flushing (quick! someone jiggle the handle). Cheers.