Quantcast
Channel: Active questions tagged symfony4 - Stack Overflow
Viewing all articles
Browse latest Browse all 3925

Symfony 6.4 multiple connections, update not always working

$
0
0

I'm using Symfony 6.4 with multiple connections.I have 2 databases (P2223 and P2324) with the same datatables struct, but with different data.

My .env:

DATABASE_URL="mysql://root:secret@127.0.0.1:3306/P2223?serverVersion=8.0.39&charset=utf8mb4"P2324="mysql://root:secret@127.0.0.1:3306/P2324?serverVersion=8.0.39&charset=utf8mb4"

This is my doctrine.yaml:

# config/packages/doctrine.yamldoctrine:    dbal:        connections:            P2324:                url: '%env(resolve:P2324)%'                logging: true            default:                url: '%env(resolve:DATABASE_URL)%'                logging: true        default_connection: P2324    orm:        default_entity_manager: P2324        entity_managers:            P2324:                connection: P2324                mappings:                    Main:                        is_bundle: false                        dir: '%kernel.project_dir%/src/Entity'                        prefix: 'App\Entity'            default:                connection: default                mappings:                    Main:                        is_bundle: false                        dir: '%kernel.project_dir%/src/Entity'                        prefix: 'App\Entity'

This is my controller:

        $entityManager = $doctrine->getManager('P2324');        $item=$entityManager->getRepository(Aules::class)->findOneBy(array('id' => 1));        $item->setNom("newName");        print_r($item);        try{$entityManager->flush();}        catch(\Exception $e){            $errorMessage = $e->getMessage();            echo $errorMessage;        }        return new JsonResponse([], Response::HTTP_OK, [], false);

It works ok, but if I change the connection:$entityManager = $doctrine->getManager('default');It doesn't works.But, surprisingly, if I change doctrine.yaml, and I change the order of connections, using $entityManager = $doctrine->getManager('default');, it works ok.Like that:

# config/packages/doctrine.yamldoctrine:    dbal:        connections:            default:                url: '%env(resolve:DATABASE_URL)%'                logging: true            P2324:                url: '%env(resolve:P2324)%'                logging: true        default_connection: P2324    orm:        default_entity_manager: P2324        entity_managers:            default:                connection: default                mappings:                    Main:                        is_bundle: false                        dir: '%kernel.project_dir%/src/Entity'                        prefix: 'App\Entity'            P2324:                connection: P2324                mappings:                    Main:                        is_bundle: false                        dir: '%kernel.project_dir%/src/Entity'                        prefix: 'App\Entity'

So, Doctrine only detects if the object has changed if I use the first connection, but don't detect any changes if I use the second connection. Weird.How can I doctrine will update the database.


Viewing all articles
Browse latest Browse all 3925

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>