Using Doctrine's Entity Manager
The principle of an ORM is to manage data stored in a relational database through an object-oriented API. We learned about its underlying concepts earlier in this chapter.
Each entity class is mapped to the related database table. Properties of the entity class are mapped to the table's columns.
So, the rows of a database table are represented in the PHP app by a collection of entities.
Doctrine ORM is able to retrieve data from the database and to populate entities with them. This process is called hydration.
Note
Instead of entities, Doctrine can populate PHP arrays in different manners (with the object graph, with a rectangular result set, and so on). It is also possible to create custom hydrators by referring to the following link: http://docs.doctrine-project.org/en/latest/reference/dql-doctrine-query-language.html#hydration-modes
As we have learned with the Data Mapper design pattern, it also does the inverse job: it persists data held by entities to database.
We will play a lot with entities later.
Doctrine comes with the following files to map entities to tables:
Annotations in comment blocks that embed directly in the entities
XML configuration files
YAML configuration files
Plain PHP files
Annotations are fairly recent in the PHP world (they are popular in Java) but they are already widely used by Doctrine and Symfony communities. The advantages of this method are great readability and maintenance facility because mapping information is next to the PHP code. Putting mapping information directly in the code can also be a drawback in some contexts, especially for big projects that use several persistence systems.
We will use the annotation method in this book, but other methods are described in the Doctrine documentation. We will return to them in Chapter 2, Entities and Mapping Information.
In the next chapter, Chapter 2, Entities and Mapping Information, we will discover that Doctrine is smart enough to use mapping information to automatically create the related database schema.
For now, we will focus on retrieving an Entity Manager. As entities are retrieved, persisted, updated, and removed through it, this is the entry point of Doctrine ORM.
Edit the src/bootstrap.php
file to retrieve a Doctrine's Entity Manager. Add the following code at the end of this file:
$entitiesPath = array(__DIR__.'/Blog/Entity'); $config = Setup::createAnnotationMetadataConfiguration ($entitiesPath, $dev); $entityManager = EntityManager::create($dbParams, $config);
Tip
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
The $entitiesPath
property contains the list of paths to directories storing entity classes. We already mentioned that our app will follow the PSR-0
namespacing convention. The \Blog
folder will be the root namespace and entity classes will be in the \Blog\Entity
folder.
A Doctrine configuration is created to use annotations for mapping information and to be able to locate the blog's entities that we'll create.
A new EntityManager
is created and configured to use our database and Doctrine settings.
For simplicity, we create a unique Entity Manager that will be used across the application. For real-world apps, you should take a look at the Dependency Injection design pattern.
Note
Find more on Dependency Injection pattern at the following link: http://en.wikipedia.org/wiki/Dependency_injection