Answer the question
In order to leave comments, you need to log in
How to call a repository in symfony 2?
I do an example from docks. There is an entity and a repository, everything is generated through the console.
calling
$org = $this->getDoctrine()
->getRepository('MyOrgBundle:Organization')
->find(21);
Fatal error: Call to undefined method My\OrgBundle\Entity\Organization::find()
public function indexAction()
{
$org = $this->getDoctrine()
->getRepository(User::class)
->find(24);
return array('name' => 'hdf');
}
public function indexAction()
{
$org = $this->getDoctrine()
->getRepository(Organization::class)
->find(24);
return array('name' => 'hdf');
}
Answer the question
In order to leave comments, you need to log in
1) Something tells me that it crashes on something like $product->find() and not on what you brought.
2) It's better to do this
3) It's even better to register repositories as services
4) Better yet, don't inherit from Doctrine repositories and use your own, which you can pass the entity manager to the constructor and do whatever you want there. Example (as it should be, it can be simplified in real projects)
class DoctrineOrganizationRepository implements OrganizationRepository
{
private $em;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
public function getOrganization(int $id) : Organization
{
$organization = $this->em->find(Organization::class, $id);
if (!$organization) {
throw new OrganizationNotFoundException();
}
return $organization;
}
}
services:
organization_repository:
class: MyApp\Service\Doctrine\DoctrineOrganizationRepository
autowire: true
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question