Skip to content

Persisting new and modified entities

sweiguny edited this page Aug 29, 2014 · 5 revisions

In this chapter you will learn

  • what the EntityManager is.
  • how persisting new and modified entities works.
    • what the behaviour regarding relations is.
  • how deleting entities works.
    • what is happening with relations.

EntityManager

The EntityManager is the Swiss Army Knife regarding persisting entities. It is needed for persisting and deleting entities. Therefor it provides two methods:

  • persist(Entity $entity)
  • remove(Entity $entity)

The EntityManager tries to act as wisely as possible, but it relies on correct configuration and a well designed database scheme. It assumes that each table, that represents an entity, has a single-column primary key.

It uses the properties and values to composes sql statements for persisting and removing entities from database.


Persisting

  • persist() takes care of primary keys and relations.

If an entity was instantiated its primary key value value will be null. On persisting such an entity, the EntityManager creates the insert statement and receives the last inserted primary key value. This value is than assigned to the entity. Indeed, provided that the primary key has a sort of auto_increment option set.

It also persists (saves to database) all relations of an entity. If new elements were added to a one-to-many or many-to-many relation, or some were modified or even deleted, such changes will be written to database. The hierarchy tiers can be endless, theoretically. If a relation is a mock, it indicates no changes, hence PPA has nothing to do here.

Note: If there is a many-to-many-relation to persist, the EntityManager treats the data like a complete set. Each time a new data association is added, the complete data set of associated rows in database is dropped and created again. So you will always need to pass the whole set for saving...

But how to do it?

Example:

$em    = \PPA\core\EntityManager::getInstance();
$query = new \PPA\core\query\TypedQuery("SELECT * FROM `user` WHERE id = 1", "\\PPA\\examples\\entity\\User");
$user  = $query->getSingleResult();

$role = new \PPA\examples\entity\Role("superman");
$user->setRole($role);

$em->persist($user);

In the example above, we select a user from the database and create a new role from scratch. After that we set the role to the user and persist the user. Hence, we can talk here from a one-to-one relation.

Internally it proceeds as follows: Because the role is created new and has therefore no primary value, PPA needs to persist the role first. After it gets the last inserted primary value, it assigns it to the role object. When the role has a primary value, PPA can also assign it to the referring property of the user object and persist it.

Removing

Removing (deleting) from database works similar.

$em    = \PPA\core\EntityManager::getInstance();
$query = new \PPA\core\query\TypedQuery("SELECT * FROM `role` WHERE id = 1", "\\PPA\\examples\\entity\\Role");
$role  = $query->getSingleResult();

$em->remove($role);
Clone this wiki locally