Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Object Relational What?

Object Relational What?

How does Doctrine talk to your database? What are Unit Of Work, Identity Map and Proxy? These are the questions I want to answer in this talk. We will look at how Doctrine ORM implements these patterns, why and most importantly why you should care about this.

Have you ever wondered why Doctrine makes queries when you didn’t expect it? Did you encounter a “A new entity was found through the relationship”-error and just randomly tried things until it works? You are not alone! Let’s look behind the curtains of Doctrine and figure these things out.

Denis Brumann

January 25, 2019
Tweet

More Decks by Denis Brumann

Other Decks in Programming

Transcript

  1. Denis Brumann
 @dbrumann
 [email protected] !5 /** * @ORM\Entity(repositoryClass="App\Repository\RentalZoneRepository") * @ORM\Table(name="carsharing_rental_zone")

    */ class RentalZone { /** * @ORM\Id() * @ORM\Column(name="hal_id", type="integer") * @ORM\GeneratedValue(strategy="NONE") */ private $id; /** * @ORM\Column(name="hal_src", type="string", length=16) */ private $source;
  2. Denis Brumann
 @dbrumann
 [email protected] /** * @ORM\Embeddable() */ class Coordinates

    { /** * @ORM\Column(name="latitude", type="decimal", precision=18, scale=15, nullable=true) */ private $latitude; /** * @ORM\Column(name="longitude", type="decimal", precision=18, scale=15, nullable=true) */ private $longitude; } !6
  3. Denis Brumann
 @dbrumann
 [email protected] !7 interface ObjectManager { public function

    find($className, $id); public function persist($object); public function remove($object); public function merge($object); public function clear($objectName = null); public function detach($object); public function refresh($object); public function flush(); public function getRepository($className); public function getClassMetadata($className); public function getMetadataFactory(); public function initializeObject($obj); public function contains($object); } QUERYING
  4. Denis Brumann
 @dbrumann
 [email protected] !7 interface ObjectManager { public function

    find($className, $id); public function persist($object); public function remove($object); public function merge($object); public function clear($objectName = null); public function detach($object); public function refresh($object); public function flush(); public function getRepository($className); public function getClassMetadata($className); public function getMetadataFactory(); public function initializeObject($obj); public function contains($object); } QUERYING OBJECT HYDRATION
  5. Denis Brumann
 @dbrumann
 [email protected] !7 interface ObjectManager { public function

    find($className, $id); public function persist($object); public function remove($object); public function merge($object); public function clear($objectName = null); public function detach($object); public function refresh($object); public function flush(); public function getRepository($className); public function getClassMetadata($className); public function getMetadataFactory(); public function initializeObject($obj); public function contains($object); } QUERYING OBJECT HYDRATION STATE
  6. Denis Brumann
 @dbrumann
 [email protected] !7 interface ObjectManager { public function

    find($className, $id); public function persist($object); public function remove($object); public function merge($object); public function clear($objectName = null); public function detach($object); public function refresh($object); public function flush(); public function getRepository($className); public function getClassMetadata($className); public function getMetadataFactory(); public function initializeObject($obj); public function contains($object); } QUERYING OBJECT HYDRATION STATE "TRANSACTIONS"
  7. /** * Finds an Entity by its identifier. * *

    @param string $entityName The class name of the entity to find. * @param mixed $id The identity of the entity to find. * @param integer|null $lockMode One of the \Doctrine\DBAL\LockMode::* constants * or NULL if no specific lock mode should be used * during the search. * @param integer|null $lockVersion The version of the entity to find when using * optimistic locking. * * @return object|null The entity instance or NULL if the entity can not be found. * * @throws OptimisticLockException * @throws ORMInvalidArgumentException * @throws TransactionRequiredException * @throws ORMException */ public function find($entityName, $id, $lockMode = null, $lockVersion = null) { $class = $this->metadataFactory->getMetadataFor(ltrim($entityName, '\\')); if ($lockMode !== null) { $this->checkLockRequirements($lockMode, $class); } if ( ! is_array($id)) { if ($class->isIdentifierComposite) { throw ORMInvalidArgumentException::invalidCompositeIdentifier(); }
  8. public function find($entityName, $id, $lockMode = null, $lockVersion = null)

    { $class = $this->metadataFactory->getMetadataFor(ltrim($entityName, '\\')); if ($lockMode !== null) { $this->checkLockRequirements($lockMode, $class); } if ( ! is_array($id)) { if ($class->isIdentifierComposite) { throw ORMInvalidArgumentException::invalidCompositeIdentifier(); } $id = [$class->identifier[0] => $id]; } foreach ($id as $i => $value) { if (is_object($value) && $this->metadataFactory- >hasMetadataFor(ClassUtils::getClass($value))) { $id[$i] = $this->unitOfWork->getSingleIdentifierValue($value); if ($id[$i] === null) { throw ORMInvalidArgumentException::invalidIdentifierBindingEntity(); } } } $sortedId = []; foreach ($class->identifier as $identifier) {
  9. throw ORMException::missingIdentifierField($class->name, $identifier); } $sortedId[$identifier] = $id[$identifier]; unset($id[$identifier]); } if

    ($id) { throw ORMException::unrecognizedIdentifierFields($class->name, array_keys($id)); } $unitOfWork = $this->getUnitOfWork(); // Check identity map first if (($entity = $unitOfWork->tryGetById($sortedId, $class->rootEntityName)) !== false) { if ( ! ($entity instanceof $class->name)) { return null; } switch (true) { case LockMode::OPTIMISTIC === $lockMode: $this->lock($entity, $lockMode, $lockVersion); break; case LockMode::NONE === $lockMode: case LockMode::PESSIMISTIC_READ === $lockMode: case LockMode::PESSIMISTIC_WRITE === $lockMode: $persister = $unitOfWork->getEntityPersister($class->name); $persister->refresh($sortedId, $entity, $lockMode);
  10. case LockMode::NONE === $lockMode: case LockMode::PESSIMISTIC_READ === $lockMode: case LockMode::PESSIMISTIC_WRITE

    === $lockMode: $persister = $unitOfWork->getEntityPersister($class->name); $persister->refresh($sortedId, $entity, $lockMode); break; } return $entity; // Hit! } $persister = $unitOfWork->getEntityPersister($class->name); switch (true) { case LockMode::OPTIMISTIC === $lockMode: $entity = $persister->load($sortedId); $unitOfWork->lock($entity, $lockMode, $lockVersion); return $entity; case LockMode::PESSIMISTIC_READ === $lockMode: case LockMode::PESSIMISTIC_WRITE === $lockMode: return $persister->load($sortedId, null, null, [], $lockMode); default: return $persister->loadById($sortedId); } }
  11. Denis Brumann
 @dbrumann
 [email protected] !12 UNIT OF WORK A Unit

    of Work keeps track of everything you do during a business transaction that can affect the database. When you're done, it figures out everything that needs to be done to alter the database as a result of your work.
  12. Denis Brumann
 @dbrumann
 [email protected] !13 public function checkout(array $cartItems): void

    { $order = new Order(); foreach ($cartItems as $cartItem) { $orderItem = new OrderItem(); $orderItem->setName($cartItem->getName()); $orderItem->setPrice($cartItem->getPrice()); $order->addItem($orderItem); } $this->manager->persist($order); $this->manager->flush(); }
  13. Denis Brumann
 @dbrumann
 [email protected] !16 public function checkout(array $cartItems): void

    { $order = new Order(); foreach ($cartItems as $cartItem) { $orderItem = new OrderItem(); $orderItem->setName($cartItem->getName()); $orderItem->setPrice($cartItem->getPrice()); $order->addItem($orderItem); $this->manager->persist($orderItem); } $this->manager->persist($order); $this->manager->flush(); }
  14. use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity(repositoryClass="App\Repository\OrderRepository") *

    @ORM\Table(name="app_order") */ class Order { /** * @ORM\Id() * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @ORM\OneToMany(targetEntity="App\Entity\OrderItem", mappedBy="order", cascade={"persist"}) */ private $items; /** * @ORM\Column(type="datetime_immutable") */ private $createdOn; public function __construct() {
  15. Denis Brumann
 @dbrumann
 [email protected] !18 CHANGE SETS Computes all the

    changes that have been done to entities and collections since the last commit and stores these changes in the $entityChangeSet map temporarily for access by the persisters, until the UoW commit is finished.
  16. { "00000000085bc321000000004502eb13": { "createdOn": [
 null,
 {
 "date": "2018-12-20 20:49:32.824391",


    "timezone_type": 3,
 "timezone": "Europe\/Berlin"
 }
 ] }, "00000000085bc37e000000004502eb13": { "status": [null, 0], "name": [null, "Chewing gums"], "price": [null, "249"], "order": [null, {App\Entity\Order}] } } Order OrderItem
  17. Doctrine ORM does not save unintentionally persisted entities. You have

    to add cascade to the association or persist each object manually. Only entities managed by the UnitOfWork will be inserted.
  18. $oid = spl_object_hash($entity) This function returns a unique identifier for

    the object. This id can be used as a hash key for storing objects, or for identifying an object, as long as the object is not destroyed. Once the object is destroyed, its hash may be reused for other objects.
  19. $item = new OrderItem(); $item->setName('Socks'); $item->setPrice('999'); $manager->persist($item); $order = new

    Order(); $order->addItem($item); $manager->persist($order); $manager->flush();
  20. $item = new OrderItem(); $item->setName('Socks'); $item->setPrice('999'); $manager->persist($item); $order = new

    Order(); $order->addItem($item); $manager->persist($order); $manager->flush();
  21. $item = new OrderItem(); $item->setName('Socks'); $item->setPrice('999'); $manager->persist($item); $order = new

    Order(); $order->addItem($item); $manager->persist($order); $manager->flush();
  22. UnitOfWork will take care of correctly ordering each commit. You

    can persist your entities in any order you like.
  23. {% extends "base.html.twig" %} {% block body %} <h1>Order list</h1>

    <div class="btn-group mb-2" role="group"> <a href="{{ path('create') }}" class="btn btn-primary">Create sample orders</a> <a href="{{ path('truncate') }}" class="btn btn-primary">Truncate orders</a> </div> <div class="list-group mb-4"> {% for order in orders %} <a href="{{ path('show', {'id': order.id}) }}" class="list-group-item list-group-item-action flex-column align-items-start"> <div class="d-flex w-100 justify-content-between"> <h5 class="mb-1">Order number #{{ order.id }}</h5> <small>{{ order.createdOn|date('d.m.Y') }} </small> </div> <p class="mb-1">contains {{ order.items|length }} items for a total of <strong>{{ order.total / 100 }}€</strong>.</p> </a> {% endfor %} </div> {% endblock %} <p class="mb-1">
 contains {{ order.items|length }} items for a total of
 <strong>{{ order.total / 100 }}€</strong>. </p>
  24. Denis Brumann
 @dbrumann
 [email protected] !41 public function findOrderWithItems(int $id) {

    return $this->createQueryBuilder('o') ->select(['o', 'i']) ->where('o.id = :order_id') ->setParameter('order_id', $id) ->join('o.items', 'i') ->getQuery() ->getSingleResult(); }
  25. Denis Brumann
 @dbrumann
 [email protected] !41 public function findOrderWithItems(int $id) {

    return $this->createQueryBuilder('o') ->select(['o', 'i']) ->where('o.id = :order_id') ->setParameter('order_id', $id) ->join('o.items', 'i') ->getQuery() ->getSingleResult(); }
  26. Denis Brumann
 @dbrumann
 [email protected] !41 public function findOrderWithItems(int $id) {

    return $this->createQueryBuilder('o') ->select(['o', 'i']) ->where('o.id = :order_id') ->setParameter('order_id', $id) ->join('o.items', 'i') ->getQuery() ->getSingleResult(); }
  27. Denis Brumann
 @dbrumann
 [email protected] !43 public function findSummarizedOrders(): array {

    $dql = 'SELECT NEW App\\DTO\\OrderSummary(
 o.id, o.createdOn, COUNT(i.order), SUM(i.price) ) FROM App\\Entity\\Order o JOIN o.items i GROUP BY i.order'; return $this->getEntityManager() ->createQuery($dql) ->getResult(); }
  28. Denis Brumann
 @dbrumann
 [email protected] !44 SELECT o.id, o.created_on, COUNT(i.order_id), SUM(i.price)

    FROM app_order AS o JOIN app_order_item i on o.id = i.order_id GROUP BY o.id ORDER BY o.created_on ASC
  29. <?php declare(strict_types = 1); namespace App\DTO; use DateTimeImmutable; final class

    OrderSummary { private $orderId; private $orderDate; private $itemCount; private $total; public function __construct(int $orderId, DateTimeImmutable $orderDate, int $itemCount, int $total) { $this->orderId = $orderId; $this->orderDate = $orderDate; $this->itemCount = $itemCount; $this->total = $total; } public function getOrderId(): int { return $this->orderId; } public function getOrderDate(): DateTimeImmutable { return $this->orderDate;
  30. public function __construct(int $orderId, DateTimeImmutable $orderDate, int $itemCount, int $total)

    { $this->orderId = $orderId; $this->orderDate = $orderDate; $this->itemCount = $itemCount; $this->total = $total; } public function getOrderId(): int { return $this->orderId; } public function getOrderDate(): DateTimeImmutable { return $this->orderDate; } public function getItemCount(): int { return $this->itemCount; } public function getTotal(): int { return $this->total; } }
  31. public function findMostExpensiveOrder(): ?Order { $sql = <<<SQL SELECT o.*,

    SUM(i.price) AS order_sum FROM app_order AS o JOIN app_order_item i ON o.id = i.order_id GROUP BY i.order_id ORDER BY order_sum DESC LIMIT 1; SQL; $resultSetMapping = new ResultSetMappingBuilder($this->getEntityManager()); $resultSetMapping->addRootEntityFromClassMetadata(Order::class, 'o'); return $this->getEntityManager() ->createNativeQuery($sql, $resultSetMapping) ->getOneOrNullResult(); }
  32. /** * @ORM\Entity(repositoryClass="App\Repository\OrderReposi ") * @ORM\Table(name="app_order") */ class Order {

    /** * @ORM\Id() * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @ORM\OneToMany( * targetEntity="App\Entity\OrderItem", * mappedBy="order", * cascade={"persist"}, * fetch="EAGER" * ) */
  33. Doctrine loads associated entities lazily by default. Writing custom queries

    with JOIN, custom DTO- Results or scalar results can improve read- performance.
  34. * @param integer|null $lockMode One of the \Doctrine\DBAL\LockMode::* constants *

    or NULL if no specific lock mode should be used * during the search. * @param integer|null $lockVersion The version of the entity to find when using * optimistic locking. * * @return object|null The entity instance or NULL if the entity can not be found. * * @throws OptimisticLockException * @throws ORMInvalidArgumentException * @throws TransactionRequiredException * @throws ORMException */ public function find($entityName, $id, $lockMode = null, $lockVersion = null) { $class = $this->metadataFactory->getMetadataFor(ltrim($entityName, '\\')); if ($lockMode !== null) { $this->checkLockRequirements($lockMode, $class); } if ( ! is_array($id)) { if ($class->isIdentifierComposite) { throw ORMInvalidArgumentException::invalidCompositeIdentifier(); } $id = [$class->identifier[0] => $id]; } foreach ($id as $i => $value) {
  35. Doctrine keeps an Identity Map referencing each known entity. When

    an entity is already known Doctrine does refrain from querying the database for it (sometimes) .
  36. Denis Brumann
 @dbrumann
 [email protected] !57 $item = new OrderItem(); $item->setName('Coffee');

    $item->setPrice('149'); $order = new Order(); $order->setId(1); $order->addItem($item); $this->manager->flush();
  37. Denis Brumann
 @dbrumann
 [email protected] !58 $item = new OrderItem(); $item->setName('Coffee');

    $item->setPrice('149'); $order = new Order(); $order->setId(1); $order->addItem($item); $this->manager->flush(); WILL THIS UPDATE THE EXISTING ORDER?
  38. ENTITY STATES A NEW entity instance has no persistent identity,

    and is not yet associated with an EntityManager and a UnitOfWork (i.e. those just created with the "new" operator).
  39. A MANAGED entity instance is an instance with a persistent

    identity that is associated with an EntityManager and whose persistence is thus managed. ENTITY STATES
  40. A DETACHED entity instance is an instance with a persistent

    identity that is not (or no longer) associated with an EntityManager and a UnitOfWork. ENTITY STATES
  41. A REMOVED entity instance is an instance with a persistent

    identity, associated with an EntityManager, that will be removed from the database upon transaction commit. ENTITY STATES
  42. When Doctrine saves a new entity when you want to

    update or vice versa, check the UnitOfWork's entityInsertions and entityUpdates. Make sure to do this during flush as this is when the associatedEntities are collected at the very latest. !
  43. Denis Brumann
 @dbrumann
 [email protected] !69 public function __construct(SessionInterface $session, EntityManagerInterface

    $entityManager) { $this->session = $session; $this->entityManager = $entityManager; } public function loadUser(): ?User { if ($this->session->has('user')) { // Loads User-entity stored in session and merges it into the UnitOfWork return $this->entityManager->merge($this->session->get('user')); } return null; }
  44. Denis Brumann
 @dbrumann
 [email protected] !70 API Client App fetch Order

    with ID 2 EntityManager::find(Order, 2) returns Order data as JSON
  45. Denis Brumann
 @dbrumann
 [email protected] !70 API Client App fetch Order

    with ID 2 EntityManager::find(Order, 2) returns Order data as JSON
  46. Denis Brumann
 @dbrumann
 [email protected] !73 $patch = $this->serializer->deserialize( $request->getContent(), Order::class,

    'json' ); $updatedOrder = $this->entityManager->merge($patch); $this->entityManager->flush();
  47. Doctrine 3 will no longer support merge and detach. Instead

    of merging through the EntityManager, do a find and then update the entity manually. Instead of detaching an entity, you can just clear the EntityManager. !