FriendsOfSymfony / FOSElasticaBundle

Elasticsearch PHP integration for your Symfony project using Elastica.
http://friendsofsymfony.github.io
MIT License
1.25k stars 793 forks source link

Index even if i rollback transaction #1917

Open Zarlokh opened 1 year ago

Zarlokh commented 1 year ago

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

timmtim commented 11 months ago

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

Zarlokh commented 11 months ago

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 :/