FOSElasticaBundle icon indicating copy to clipboard operation
FOSElasticaBundle copied to clipboard

Index even if i rollback transaction

Open Zarlokh opened this issue 1 year ago • 2 comments

Package Version : v6.2.0 PHP version : 8.2.5 doctrine/dbal version : 3.6.2 Symfony version : 5.4.22 Elastica conf :

fos_elastica:
    clients:
        default: { host: "%env(ELASTICSEARCH_HOST)%", port: '%env(ELASTICSEARCH_PORT)%' }
    indexes:
        bubbles:
            ...
        unavailabilities:
            ...
        orders:
            properties:
               ...
            persistence:
                driver: orm
                model: App\Entity\Order\Order
                listener:
                    defer: true
                provider:
                    query_builder_method: getQueryBuilderForElasticaPopulate

With this conf, if i run this code

        $entityManager->beginTransaction();
        $order = new Order();
        $order->setCode('test123456789');
        $entityManager->persist($order);
        $entityManager->flush();
        $entityManager->rollback();

My order is not create in my database but if i check my elasticsearch, i can find this order image

Now, if i go to my order list page, i have an error because Elastica cannot find this order in my database So, how can i index my entity only if a transaction is committed and not rollbacked ? (i see nothing about that in closed or opened issues and nothing about it in any site like stackoverflow. Did i miss something ?)

Thanks for helping

Zarlokh avatar Jul 05 '23 13:07 Zarlokh

@Zarlokh did you find a solution for this issue. I've encountered the exact same problem.

timmtim avatar Oct 03 '23 09:10 timmtim

Hi @timmtim I duplicate the Listener used by FOSElastica to fix it. I create an array with entities in case of a transaction is running I create a decorator on EntityManager to dispatch a custom event on rollback method and commit to do something on Listener (remove entities from array or push it on elastic if it is not defer etc...) Be careful about nested transaction

I'm not very happy about my fix but I need it to avoid any error on my app I'm waiting for an official fix :/

Zarlokh avatar Oct 03 '23 11:10 Zarlokh