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:
( 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:
That will give you access to the repository and all the queries you do from that will give you Entity classes.