简体   繁体   English

Symfony 5 中 Repository 的循环参考

[英]Circular reference for Repository in Symfony 5

I try to follow this tutorial : https://www.thinktocode.com/2018/03/05/repository-pattern-symfony/ .我尝试遵循本教程: https : //www.thinktocode.com/2018/03/05/repository-pattern-symfony/

It's suppose to help structure your Repository.它应该有助于构建您的存储库。

But when i get to this point :但是当我到达这一点时:

final class ProductRepository
{
    /**
     * @var EntityManagerInterface
     */
    private $entityManager;

    /**
     * @var ObjectRepository
     */
    private $objectRepository;

    public function __construct(EntityManagerInterface $entityManager)
    {
        $this->entityManager = $entityManager;
        $this->objectRepository = $this->entityManager->getRepository(Product::class);
    }
    
    public function find(int $productId): Product
    {
        $product = $this->objectRepository->find($productId);
        return $product;
    }

    public function findOneByTitle(string $title): Product
    {
        $product = $this->objectRepository
            ->findOneBy(['title' => $title]);
        return $product;
    }

    public function save(Product $product): void
    {
        $this->entityManager->persist($product);
        $this->entityManager->flush();
    }
}

And testing my Repository with this test case :并使用此测试用例测试我的存储库:

<?php

namespace App\Tests\Repository;

use App\Repository\ProductRepository;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

class ProductRepository_KernelTest extends KernelTestCase
{

    private ?ProductRepository $_productRepository;

    protected function setUp(): void
    {
        $kernel = self::bootKernel();
        $this->_productRepository = self::$container->get(ProductRepository::class);
    }

    public function test_findAllProductNatByLabelForLabelEmptyReturnTenProduct()
    {
        dump($this->_productRepository->findAllProductsByLabel('AACIFEMINE'));
        die();
    }
}

It loop endlessly.它无休止地循环。

I think it's due to this code :我认为这是由于此代码:

public function __construct(EntityManagerInterface $entityManager)
{
    $this->entityManager = $entityManager;
    $this->objectRepository = $this->entityManager->getRepository(Product::class); // <-----
}

As it call the ProductRepository constructor inside of this same constructor... So i guess it's why that loop因为它在同一个构造函数中调用 ProductRepository 构造函数......所以我想这就是为什么这个循环

So I don't know.所以我不知道。 Is this tutorial just wrong or not up to date ?这个教程是错误的还是不是最新的?

https://www.thinktocode.com/2018/03/05/repository-pattern-symfony/#comment-4155200782 https://www.thinktocode.com/2018/03/05/repository-pattern-symfony/#comment-4155200782

Maciej,马切伊,

You are correct that in these example we are using 2 repositories.您是对的,在这些示例中我们使用了 2 个存储库。 The object repository from doctrine inside our own custom repository.来自我们自己的自定义存储库中的学说的对象存储库。 This allows use to be decoupled from doctrine's repository and still change this in the future.这允许使用与学说的存储库分离,并且在未来仍然可以改变这一点。 This means to not set your custom repository as the default repository in your entity.这意味着不要将您的自定义存储库设置为实体中的默认存储库。

You can get rid of inject the object repository, and in so only be using 1 repository by implementing a BaseRepository class in which you create the basic findBy, findOneBy, createQueryBuilder yourself.您可以摆脱注入对象存储库,因此仅通过实现 BaseRepository 类来使用 1 个存储库,您可以在其中创建基本的 findBy、findOneBy、createQueryBuilder。 Take a look at the EntityRepository in Doctrine/ORM.查看 Doctrine/ORM 中的 EntityRepository。 This might be a good follow up topic to go over in a future article to create a better solution then I suggested in here.这可能是一个很好的后续主题,可以在以后的文章中讨论以创建更好的解决方案,然后我在这里建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM