M
M
Mike Evstropov2016-07-11 02:00:39
symfony
Mike Evstropov, 2016-07-11 02:00:39

How to access repository in entity?

Tried to follow this example:

use Doctrine\ORM\EntityManager;

class Someclass {

    private $entityManager;

    public function __construct(EntityManager $entityManager)
    {
        $this->entityManager = $entityManager;
    }

    public function someMethod()
    {
        $customer = $this->entityManager
            ->getRepository('Acme\CustomerBundle\Entity\Customer')
            ->findOneBy(...);
    }
 
 }

But I am getting an error
Error: Call to a member function getRepository() on null

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
index0h, 2016-07-11
@Aroused

How to access repository in entity?

You don't have to want it. This contradicts the very concept of Repository-Entity.
Repository - works only with the database and only with etiti.
Entity - only stores data and nothing more.
Don't try to turn the Repository into an ActiveRecord, it won't end well.
If you need dependent entities - then write them in annotations .
If you need to call exactly the repository method: this is done through DI in services, or through $this->getDoctrine()->getManager()->getRepository('AppBundle:MyEntityName') in controllers.
Specifically in your example: you must declare a service into which the EntityManager will be installed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question