【问题标题】:Symfony 5 - One entity, one Repository and two DatabasesSymfony 5 - 一个实体、一个存储库和两个数据库
【发布时间】:2021-03-01 10:02:41
【问题描述】:

我在通过 Symfony 存储库查询我的数据库时遇到问题。 如果我不指定存储库

/**
 * ProductsText
 *
 * @ORM\Table(name="products_text")
 * @ORM\Entity
 */

我可以像这样使用 Doctrine 选择我的数据库之一:

->getRepository(ProductsText::class, "db1").

但如果我声明存储库:

/**
 * ProductsText
 *
 * @ORM\Table(name="products_text")
 * @ORM\Entity(repositoryClass="App\Repository\Product\ProductsTextRepository")
 */

它只选择我从中提取实体的数据库。 我不能使用两个不同的实体,因为用户在登录时选择了“数据库”。不幸的是,我也不能合并这两个数据库,因为它们不是多租户的。

我的猜测是,我可以通过将注册表从存储库中保留来解决问题。不幸的是,我之前的尝试以前端不再加载而告终。

有效,但仅适用于一个数据库:

class ProductsTextRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, ProductsText::class);
    }

没用:

class TbProductsTextRepository
{
  /**
   * @var EntityRepository
   */
  private $repository;

  public function __construct(EntityManagerInterface $entityManager)
  {
      $this->repository = $entityManager->getRepository(TbProductsText::class);
  }

很遗憾,我不了解如何重新生成存储库的机制。

我用:

  • Symfony 5.2
  • 教义 2.2
  • 学说 ORM 2.8
  • Doctrine Migrations Bundle 3.0

【问题讨论】:

标签: doctrine-orm doctrine


【解决方案1】:

我通过不“注册”存储库解决了这个问题。 通过使用 EntityRepository 并且不使用构造,我可以将存储库用于两个数据库。

use Doctrine\ORM\EntityRepository;


class TbProductsTextRepository extends EntityRepository
{

【讨论】:

  • 很高兴你能成功。当涉及到由多个实体管理器映射实体时,ServiceEntityRepository 从根本上被破坏了。只有第一个实体管理器会被使用。我怀疑您还必须从自动装配中排除您的存储库。
猜你喜欢
  • 2019-01-17
  • 1970-01-01
  • 1970-01-01
  • 2012-03-10
  • 2016-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多