New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Repository as service #11155
Comments
Why do you need the entity manager as a prop in the 2. example? |
@kunicmarko20 usually for persistence operations: public function store(MyAggregate $aggregate) : void
{
$this->entityManager->persist($aggregate);
$this->entityManager->flush();
} |
Ah, thanks for the explanation @Ocramius. What would you suggest here? |
Presenting my findings below. I won't bother sharing the memory usage and response speed data but in the case of TEST REPO 1 use App\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Symfony\Bridge\Doctrine\RegistryInterface;
class UserRepository extends ServiceEntityRepository
{
public function __construct(RegistryInterface $registry)
{
parent::__construct($registry, User::class);
}
public function findAllCustom(): array
{
return $this->createQueryBuilder('u')
->getQuery()
->getResult();
}
public function persistCustom(): void
{
$this->getEntityManager()->persist(new User());
$this->getEntityManager()->flush();
}
} TEST REPO 2 :( use App\Entity\User;
use Doctrine\Common\Persistence\ManagerRegistry;
class UserRepository
{
private $managerRegistry;
public function __construct(ManagerRegistry $managerRegistry)
{
$this->managerRegistry = $managerRegistry;
}
public function findAllCustom(): array
{
return $this->managerRegistry->getRepository(User::class)->createQueryBuilder('u')
->getQuery()
->getResult();
}
public function persistCustom(): void
{
$this->managerRegistry->getManager()->persist(new User());
$this->managerRegistry->getManager()->flush();
}
} TEST REPO 3 use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
class UserRepository
{
private $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
public function findAllCustom(): array
{
return $this->entityManager->getRepository(User::class)->createQueryBuilder('u')
->getQuery()
->getResult();
}
public function persistCustom(): void
{
$this->entityManager->persist(new User());
$this->entityManager->flush();
}
} |
Thank you for this issue. |
Friendly reminder that this issue exists. If I don't hear anything I'll close this. |
Hey, I didn't hear anything so I'm going to close it. Feel free to comment if this is still relevant, I can always reopen! |
Hi,
As in usage of repository classes the Internet is full of blog posts that often say "Do not dependency inject Entity Manager anywhere", "Entity Manager closed", "This happens if your entity uses multiple Entity Managers" so on. There are many examples and many people are confused as a result. All people want is to create a repository (but how exactly) and inject it to wherever they need it - e.g. a Service class.
I think it is worth putting some kind of example (the best practise) in the Doc and explain why repository should be done that particular way. To me the example should encourage people from decoupling themselves from the Doctrine and composition is more ideal than the inheritance etc.
If no action will be taken in the Doc, could please someone show us an example here? Or maybe vote on one the examples below please. I believe @Ocramius 's input here would add a great value!
Thanks
Examples I've seen -
1
2
3
4
The text was updated successfully, but these errors were encountered: