最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

doctrine - Symfony 6.4 multiple connections, update not always working - Stack Overflow

programmeradmin1浏览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:[email protected]:3306/P2223?serverVersion=8.0.39&charset=utf8mb4"
P2324="mysql://root:[email protected]:3306/P2324?serverVersion=8.0.39&charset=utf8mb4"

This is my doctrine.yaml:

# config/packages/doctrine.yaml
doctrine:
    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.yaml
doctrine:
    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.

发布评论

评论列表(0)

  1. 暂无评论