Symfony2 & Doctrine2: EntityManager
Just a quick reference post ( im using Symfony2 BETA1 ):
Ref:
Doctrine2 API Symfony2 API–Get The Entity Manager
To get the EntityManager inside a Controller use the Dependency Injector to get it:
$em = $this->get("doctrine.orm.entity_manager");
That object is basically the manager that you will use to talk with Doctrine2. You will use this object mostly to persist and flush Entity object to the database ( this is done using an UnitOfWork pattern )
Each entity represents a Model/A table in your database, so it should not have SQL inside it, it’s just a class. The place where you put your methods to extract entities from the database is in a Repository class, which contains methods like… (imaginary) getAllMembersNotActive(), getMembersWithAccessTo($comething), … etc. So, this means, that your Controller should be clean from any database query.
–Custom Repository for your Entities
To set a custom Repository for your Entity, you will use annotations in your class like this…
[preserved_text 66770fa0feae92c9ffb85fc6f83fd1df /]
So once you have that, use the console to auto-generate the Repository classes:
php app/console doctrine:generate:entities YourAppYourBundle
After that you will have the new empty repository classes in the path you said on the repositoryClass attribute above:
src/YourApp/YourBundle/Entity/UserRepository.php
( which will extend from EntityRepository ) Inside thouse classes (once per Entity will be generated) you will put the code related to each entity.
–Access your Repository
Then, to have access to that Repository class, from your controller call this:
$em->getRepository("YourAppYourBundle:User")
That will give you access to the repository and all the queries you do from that will give you Entity classes.
–Custom Queries
If you need to execute custom SQL or custom DQLs, you will have to use the EntityManager directly to generate DQL or SQL queries:
$em->createQuery(...);
Roberto 4:35 AM on 18 April 2012 Permalink |
Ok, this is already available from the Symfony book on the official website.
What about retrieving the EntityManager from inside a generic PHP object in a Symfony2 project?
p48l0 8:40 AM on 18 April 2012 Permalink |
that’s easy, just register your class as a service, and use the dependency injector to instanciate it. It will take care of all the injections of dependencies.
PS: u declare the dependencies on the services.xml of your proyect.
Roberto 8:42 AM on 18 April 2012 Permalink
Or simply pass the EntityManager to the class constructor from inside a Controller.
p48l0 8:46 AM on 18 April 2012 Permalink |
Using the Dependency Injector would be the better choice i think… because this way, you wont have to reference your class, allowing you in the futuro to swap it for another, or to change the instanciation’s parameters, without afecting the rest of your code.
If you do: new SomeClass( EnityManagerRef ) you will be bounding your class with your code…
Roberto 8:49 AM on 18 April 2012 Permalink
I do agree with you… it is just a fast shortcut! Larger applications would need several services that are usually difficult to remember…