Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从未使用过读取副本数据库连接,返回默认状态_Php_Symfony_Doctrine Orm_Read Replication - Fatal编程技术网

Php 从未使用过读取副本数据库连接,返回默认状态

Php 从未使用过读取副本数据库连接,返回默认状态,php,symfony,doctrine-orm,read-replication,Php,Symfony,Doctrine Orm,Read Replication,下面是关于为读取复制目的设置备用数据库连接的文档。作为参考,我使用的是AWS Aurora PostgreSQL,主要和只读的端点 我已经为我的原则设置了此选项。yaml doctrine: dbal: default_connection: default connections: default: url: '%env(resolve:DATABASE_URL)%' rea

下面是关于为读取复制目的设置备用数据库连接的文档。作为参考,我使用的是AWS Aurora PostgreSQL,主要和只读的端点

我已经为我的
原则设置了此选项。yaml

doctrine:
    dbal:
        default_connection: default
        connections:
            default:
                url: '%env(resolve:DATABASE_URL)%'
            read:
                url: '%env(resolve:READ_URL)%'
    orm:
        default_entity_manager: default
        entity_managers:
            default:
                connection: default
                naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
                auto_mapping: true
                dql:
                    datetime_functions:
                        date_trunc: App\DoctrineExtensions\DateTrunc
                mappings:
                    App:
                        is_bundle: false
                        type: annotation
                        dir: '%kernel.project_dir%/src/Entity'
                        prefix: 'App\Entity'
                        alias: App
            read:
                connection: read
                naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
                auto_mapping: false
                dql:
                    datetime_functions:
                        date_trunc: App\DoctrineExtensions\DateTrunc
                mappings:
                    App:
                        is_bundle: false
                        type: annotation
                        dir: '%kernel.project_dir%/src/Entity'
                        prefix: 'App\Entity'
                        alias: App
        auto_generate_proxy_classes: true
并将只读orm调用为:

$user = $this->getDoctrine()->getRepository(User::class, 'read')->findOneBy(['email' => $payload['email']]);
举个例子

如果我调用它,它仍然会转到默认数据库。如果我将其命名错误,Symfony会像预期的那样抛出一个异常,这样我就可以确认配置是否被正确提取和加载

我的配置或调用如何不正确,它仍然从
默认数据库连接请求,或者是否有更好的方法为数据连接定义读取副本?我还没有找到任何关于它的最新文档。

根据:

一个实体可以由多个实体管理器管理。这 但是,从扩展时会导致意外行为 自定义存储库中的ServiceEntityRepository。这个 ServiceEntityRepository始终将配置的实体管理器用于 这个实体。为了解决这种情况,请扩展EntityRepository 取而代之,不再依赖自动布线:

因此,您应该确保您的存储库扩展了
EntityRepository
,而不是
ServiceEntityRepository

$user = $this->getDoctrine()->getRepository(User::class, 'read')->findOneBy(['email' => $payload['email']]);
然后,您现在应该始终使用
ManagerRegistry::getRepository()
获取此存储库

因此基本上这是可行的,因为
$this->getDoctrine()
返回
ManagerRegistry
的实例

$user = $this->getDoctrine()->getRepository(User::class, 'read')->findOneBy(['email' => $payload['email']]);
缺点是您将无法对存储库本身使用自动连线

因此,除非您手动传递该存储库,否则这将不起作用

public function __construct(UserRepository $userRepository)
{
    $this->userRepository = $userRepository;
}

我可能遗漏了什么,但这对我来说很好。你能确认
%env(resolve:READ\u URL)%
确实解析了正确的URL吗?是的,它只是在测试中,但我基本上只是更改了不同数据库URL的密码。更改默认值会导致调用失败,更改读取值不会导致错误,因此我可以假设它使用默认值。如果它测试了默认的db,我不会感到惊讶,但是它调用只读db的尝试应该失败,因为这就是调用的原因。我认为本页末尾的警告可能就是您出现此问题的原因@朱丽叶。我想你是对的。是否有其他方法可以查询读取副本而不是主副本?对于一个常见的用例来说,这似乎太难了。没有一个是我马上想到的。谢谢,我在过去的24小时内达到了这一点,基本上结束了对我想要卸载到读取副本的2个查询使用准备好的sql查询。