Note that there are some explanatory texts on larger screens.

plurals
  1. POPersist an Object on Event in Doctrine 2
    primarykey
    data
    text
    <p>When an entity is saved in my app, I want to add its <code>slug</code> field to a lookup table. To make this as least intrusive as possible, I wrote up an event subscriber. According to <a href="https://stackoverflow.com/questions/10374155/doctrine-inserting-in-postpersist-event">this post</a>, I cannot <code>INSERT</code> on <code>postUpdate</code> events, so I tried <code>postFlush</code>:</p> <pre><code>class SlugsSubscriber implements EventSubscriber { public function getSubscribedEvents() { return array('postFlush'); } public function postFlush(PostFlushEventArgs $args) { $em = $args-&gt;getEntityManager(); $needsFlush = false; error_log("postFlush"); foreach ($em-&gt;getUnitOfWork()-&gt;getScheduledEntityInsertions() as $entity) { // To prevent short-circuit $tmp = $this-&gt;registerSlugs($entity, $entityManager); if($tmp) $needsFlush = true; } foreach ($em-&gt;getUnitOfWork()-&gt;getScheduledEntityUpdates() as $entity) { // To prevent short-circuit $tmp = $this-&gt;registerSlugs($entity, $entityManager); if($tmp) $needsFlush = true; } if($needsFlush) { error_log("Flushing"); $em-&gt;flush(); } } protected function registerSlugs($entity, EntityManager $entityManager) { error_log("register slugs"); if($entity instanceof Product) { $this-&gt;registerProductSlugs($entity, $entityManager); return true; } elseif($entity instanceof Category) { $this-&gt;registerCategorySlugs($entity, $entityManager); return true; } return false; } } </code></pre> <p>However, when I try to save an entity now, the method <code>registerSlugs</code> is never called, neither on an update nor a new insertions.</p> <p>Where did I go wrong?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload