2

在将教义/持久性从 1.0 升级到 1.3 时,我遇到了静态代码分析问题。

Repository (Doctrine\ORM\EntityRepository) does not accept                    
         Doctrine\Persistence\ObjectRepository.     

问题在于这个

<?php
declare(strict_types=1);

namespace Appbundle\Repository\Company;

class CompanyRepository
{
    /**
     * @var EntityManagerInterface
     */
    private $entityManager;

    /**
     * @var EntityRepository
     */
    private $entityRepository;

    /**
     * @var ProfileRepository
     */
    private $profileRepository;

    public function __construct(
        EntityManagerInterface $entityManager,
    ) {
        $this->entityManager = $entityManager;
        $this->entityRepository = $entityManager->getRepository(Company::class);
    }

该代码作为 getRepository reuturns EntityRepository 但 getReposiry 的返回类型是 ObjectRepository 并且不兼容。使用 1.0 版。有人知道它可能是什么吗?

4

2 回答 2

0

This error is correct. EntityManagerInterface inherits getRepository method from ObjectManager interface, where ObjectRepository is typehinted.

So you can't rely on EntityRepository to be returned from the method as the implementation can choose to return just ObjectRepository.

You should probably typehint for something else, like EntityManager instead.

于 2020-03-16T18:44:01.100 回答
-2

好的,所以我发现这是由于 Doctrine 更改了名称空间。这是一个报告的错误。

https://github.com/doctrine/orm/pull/7997 https://github.com/doctrine/orm/pull/7953

于 2020-03-18T08:39:04.580 回答