How do I use Doctrine in a service container?
The Code just causes an error message \"Fatal error: Call to undefined method ...::get()\".
Since 2017 and Symfony 3.3 you can register Repository as service, with all its advantages it has.
Your code would change like this.
# app/config/services.yml
services:
_defaults:
autowire: true
...\Service\:
resource: ...\Service
use Doctrine\ORM\EntityManagerInterface;
class YourRepository
{
private $repository;
public function __construct(EntityManagerInterface $entityManager)
{
$this->repository = $entityManager->getRepository(YourEntity::class);
}
public function find($id)
{
return $this->repository->find($id);
}
}
class dsdsf
{
private $yourRepository;
public function __construct(YourRepository $yourRepository)
{
$this->yourRepository = $yourRepository;
}
public function create()
{
$id = 10;
$this->yourRepository->find($id);
}
}
Do you want to see more code and pros/cons lists?
Check my post How to use Repository with Doctrine as Service in Symfony.