Entity manager is the primary access point to ORM functionality provided by Doctrine.
EntityManageris a Doctrine class that lives in
Doctrine\ORMnamespace and used to retrieve entities from their repositories using search criteria and save entities back to database.
EntityManager is registered as a service in the Zend Framework 3 service manager.
In your factory class, you retrieve the
EntityManager from service manager as
follows (if you need a different connection than
orm_default, just replace the
the required connection name):
// Get Doctrine entity manager $entityManager = $container->get('doctrine.entitymanager.orm_default');
The most used methods provided by the
EntityManager class are listed in table 12.4 below.
||Places new entity into entity manager (makes it managed).|
||Removes an entity from database.|
||Flushes all changes to objects that have been queued up to now to the database.|
||Creates a new Query object.|
||Gets the repository for an entity class.|
Let's review the methods from table 12.4.
To add a newly created entity to entity manager (to make the entity "managed"), you use
To remove an entity from database, you use entity manager's
When you call
EntityManager remembers your changes in memory, but doesn't
apply changes to database automatically (by performance reasons). To apply changes to database in a
single transaction, you use the
For example, look at the code example below that shows how to create an instance of the
and save it to database:
// Create new Post entity. $post = new Post(); $post->setTitle('Top 10+ Books about Zend Framework 3'); $post->setContent('Post body goes here'); $post->setStatus(Post::STATUS_PUBLISHED); $currentDate = date('Y-m-d H:i:s'); $post->setDateCreated($currentDate); // Add the entity to entity manager. $entityManager->persist($post); // Apply changes to database. $entityManager->flush();
createQuery() method of the entity manager is designed for creating a query from a DQL string. It returns the
object. You then execute the query and get results (an array of entities matching search conditions).
getRepository() method of the entity manager is designed to get repository by entity class name. Please look
below for example where we get the repository for our
$repository = $entityManager->getRepository(Post::class);
Conceptually, each entity class has its own repository. The repository provides methods for retrieving
entities from database. The repository can be considered as a collection of all
available entities of certain class. For example, there are repositories for our
To load data from the database, you retrieve an entity from its repository. When you request the repository for an entity, it loads the data from the table mapped to the entity, and assigns entity's fields with the data.
Doctrine\ORM\EntityRepositoryclass implements the default repository. If needed, you can, by extending the
EntityRepository, create your own repository for certain entity class. We will show how to do that later.
The most used methods provided by the
EntityRepository class are listed in table 12.5.
||Finds all entities in the repository.|
||Finds an entity by its identifier.|
||Finds entities by a set of criteria.|
||Finds a single entity by a set of criteria.|
||Creates a new QueryBuilder instance that is prepopulated for this entity name.|
findAll() method gets all entities from repository. For simple example of its usage, look below:
// Find all posts from repository $posts = $entityManager->getRepository(Post::class)->findAll();
find() method is the simplest method of searching for an entity. It retrieves an entity
by its ID (primary key).
In the example below, we select post with ID = 1.
// Find post by primary key (ID) $post = $entityManager->getRepository(Post::class)->find(1);
findBy() takes a search criteria (and optional sorting order and limit)
arguments and returns a collection of entities matching criteria. The
findOneBy() method is very
findBy(), but it returns the first entity matching the criteria.
In the code example below, we use the
findBy() method for selecting 50 most recent published posts:
// Find 50 most recent published posts $posts = $entityManager->getRepository(Post::class)->findBy( ['status'=>Post::STATUS_PUBLISHED], ['dateCreated'=>'DESC'], 50);
For your convenience, the
EntityRepository class also provides magic methods allowing you to query entities by attribute name
findOneByX methods, as follows (just substitute the X placeholder with an attribute name):
// Query a single post by ID attribute $post = $entityManager->getRepository(Post::class)->findOneById(1); // Query posts by status attribute $posts = $entityManager->getRepository(Post::class) ->findByStatus(Post::STATUS_PUBLISHED);
And the most complex search method is the
createQueryBuilder(). That method allows to create
complex DQL queries.
If standard find methods are not sufficient (or if you have complex search criteria and DQL queries),
you can create your own repository by extending the standard
and encapsulate the search logic there. We will show how to do that later when implementing tag cloud feature
for our Blog sample.