Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 Use Doctrine in Service Container

How do I use Doctrine in a service container?

The Code just causes an error message "Fatal error: Call to undefined method ...::get()".

<?php  namespace ...\Service;  use Doctrine\ORM\EntityManager; use ...\Entity\Header;  class dsdsf {      protected $em;      public function __construct(EntityManager $em)     {         $this->em = $em;     }      public function create()     {         $id = 10;         $em = $this->get('doctrine')->getEntityManager();         $em->getRepository('...')->find($id);     } } 

services.yml

service:     site:         class: ...\Service\Site 
like image 458
user1075510 Avatar asked Dec 01 '11 13:12

user1075510


People also ask

Does Symfony use doctrine?

Symfony provides all the tools you need to use databases in your applications thanks to Doctrine, the best set of PHP libraries to work with databases. These tools support relational databases like MySQL and PostgreSQL and also NoSQL databases like MongoDB.

What is Symfony Service Container?

In Symfony, these useful objects are called services and each service lives inside a very special object called the service container. The container allows you to centralize the way objects are constructed. It makes your life easier, promotes a strong architecture and is super fast!

How should be the process to add a new entity to the app in Symfony?

With the doctrine:database:create command we create a new database from the provided URL. With the make entity command, we create a new entity called City . The command creates two files: src/Entity/City. php and src/Repository/CityRepository.


2 Answers

According to your code, you already have an EntityManager injected. You don't need to call $em = $this->get('doctrine')->getEntityManager() — just use $this->em.

If you don't inject an EntityManager already, read this.

UPDATE:

You need to make the container inject an EntityManager into your service. Here's an example of doing it in config.yml:

services:     your.service:         class: YourVendor\YourBundle\Service\YourService         arguments: [ @doctrine.orm.entity_manager ] 

I prefer to define bundles' services in their own services.yml files, but that's a bit more advanced, so using config.yml is good enough to get started.

like image 174
Elnur Abdurrakhimov Avatar answered Sep 30 '22 02:09

Elnur Abdurrakhimov


For easily accessing the Entitymanager use the following one:

//services.yml   your service here:     class: yourclasshere     arguments: [@doctrine.orm.entity_manager] 

And in the class itself:

class foo {   protected $em;      public function __construct(\Doctrine\ORM\EntityManager $em)   {     $this->em = $em;   }    public function bar()   {       //Do the Database stuff       $query = $this->em->createQueryBuilder();        //Your Query goes here        $result = $query->getResult();   } } 

This is my first answer so any comments are appreciated :)

like image 20
MaXxer90 Avatar answered Sep 30 '22 02:09

MaXxer90