Symfony 3 custom repository factory



  • I'm building a container in service.php

    $container->setDefinition('repository.access_token', new Definition(\Ftob\OauthServerApp\Repositories\AccessTokenRepository::class)) ->setFactory([new Reference('doctrine'), 'getRepository']) ->setArguments([\Ftob\OauthServerApp\Entity\AccessToken::class]);

    I'm trying to get him on the test--

    class AccessTokenRepositoryTest extends KernelTestCase
    {
    

    protected $repository;

    public function setUp()
    {
    $this->bootKernel();
    $this->repository = self::$kernel->getContainer()->get('repository.access_token');
    }

    public function testDi()
    {

    $this->assertInstanceOf( AccessTokenRepository::class, $this->repository);
    

    }

    }

    Got a mistake.

    AccessTokenRepositoryTest::testDi
    Failed asserting that Doctrine\ORM\EntityRepository Object (...) is an
    instance of class "Ftob\OauthServerApp\Repositories\AccessTokenRepository".

    /var/www/tests/Repositories/AccessTokenRepositoryTest.php:23
    FAILURES!
    Tests: 1, Assertions: 1, Failures: 1.

    Actually, the question... Why is it? Doctrine\ORM\EntityRepositoryNot AccessTokenRepository?

    Thank you.



  • And in the annex (not in tests) if you ask $entityManager->getRepository(\Ftob\OauthServerApp\Entity\AccessToken::class)What's coming back? It is likely that the problem is not in the container or in the service instructions.

    Doctrine Can't see, AccessTokenRepository as custom repository for AccessToken and returns default EntityRepository

    Like and when you say Doctrine Use AccessTokenRepository as repositories for their essences AccessToken?

    Is there anything like this in your class announcement? Announcement:

    /**
    * @ORM\Entity(repositoryClass="Ftob\OauthServerApp\Repositories\AccessTokenRepository")
    */
    class AccessToken
    {
        //...
    }
    

    (sighs)Or do you indicate the metadata in YAML/XML? In general, it doesn't matter what kind of metadata you chose. It is important that at the time of appeal EntityManager::getRepository() They could have been read.)

    If, yes, and in the working annex, the challenge $entityManager->getRepository(\Ftob\OauthServerApp\Entity\AccessToken::class) He returns your caste repository,, which means you have to do with the characteristics of the test environment (I really don't have much to imagine yet), then watch that you're overwhelmed in the test environment. Check through. X-Debug the same as the collection of class metadata in the annex and in the tests.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2