DDC-3222: PostUpdate event destroying collectionUpdates #4009

Closed
doctrinebot opened this Issue Jul 21, 2014 · 4 comments

2 participants

@doctrinebot

Jira issue originally created by user jspizziri:

I have an entity that contains a Many-To-Many Unidirectional association. When the association is updated and the entity is flushed the changes are not being persisted to the database.

This is because the postUpdate event is being fired on executeUpdates, which is being called before the collection updates are being processed here:

            // Collection updates (deleteRows, updateRows, insertRows)
            foreach ($this->collectionUpdates as $collectionToUpdate) {
                $this->getCollectionPersister($collectionToUpdate->getMapping())->update($collectionToUpdate);
            }

This is occurring in UnitOfWork->commit() lines 333 through 366.

Apparently the postUpdate listener I wrote was causing the collectionUpdates property to be erased.

@doctrinebot

Comment created by @ocramius:

What does the listener actually do? Doesn't look like a bug to me...

@doctrinebot

Comment created by jspizziri:

I'm using FOSElasticaBundle/ElasticSearch. On the postUpdate event I'm calling fos:elastica:populate on the entity indexes. Heres the code

<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

namespace SRC\Bundle\SearchBundle\EventListener;

use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
use Doctrine\ORM\Event\LifecycleEventArgs;
use SRC\Bundle\ProductBundle\Entity\Product;
use SRC\Bundle\UserBundle\Entity\User;

/****
 * Description of SearchIndexer
 *
 * @author jacobspizziri
 */
class SearchIndexer
{   
    protected $container;
    protected $logger;

    public function **construct($container, $logger) {
        $this->container = $container;
        $this->logger = $logger;
    }


    public function postUpdate(LifecycleEventArgs $args){
        $this->index($args);
    }

    protected function index($args){
        $entity = $args->getEntity();

        $arguments = false;
        $command = new \FOS\ElasticaBundle\Command\PopulateCommand();
        $command->setContainer($this->container);

        //TODO: probably need to specify the --env=prod/dev
        // update Elastica on Product update
        if ($entity instanceof Product) {
            $this->logger->info('Updating Product Search Indexes');

            $arguments = array(
                    '--index'=>'website',
                    '--type' => 'product'
                    );
        } else if ($entity instanceof User) {
            $this->logger->info('Updating User Search Indexes');

            $arguments = array(
                    '--index'=>'website',
                    '--type' => 'user'
                    );
        }


        if($command && $arguments){
            $input = new ArrayInput($arguments);
            $output = new NullOutput();

            $result = $command->run($input, $output);
            $this->logger->info('Finished Updating Search Indexes with result: '. strval($result));
        }
    }
}
?>

Am I using this event incorrectly? Should I only be using postPersist?

@doctrinebot

Comment created by @ocramius:

After checking again, I think this issue is invalid. Please use the onFlush event to log changes (instead of post-* events

@doctrinebot

Issue was closed with resolution "Invalid"

@Ocramius Ocramius was assigned by doctrinebot Dec 6, 2015
@doctrinebot doctrinebot closed this Dec 6, 2015
@doctrinebot doctrinebot added the Bug label Dec 7, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment