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.

6a1345d8e6dd15b2c78eff0c331963b1?s=128

Denis Brumann

January 25, 2019
Tweet

Transcript

  1. OBJECT RELATIONAL WHAT? Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de

  2. DENIS BRUMANN denis.brumann@sensiolabs.de 
 @dbrumann SOFTWARE DEVELOPER BERLIN, GERMANY

  3. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !3 DATA MAPPER

  4. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !4

  5. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !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;
  6. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de /** * @ORM\Embedded(class="App\Entity\Coordinates",
 * columnPrefix=false) */

    private $coordinates; !6
  7. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de /** * @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
  8. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !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
  9. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !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
  10. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !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
  11. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !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"
  12. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !8

  13. /** * 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(); }
  14. 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) {
  15. 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);
  16. 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); } }
  17. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !10

  18. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !10

  19. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !10

  20. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !10

  21. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !11 UNIT OF WORK

  22. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !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.
  23. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !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(); }
  24. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !14

  25. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !14

  26. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !14

  27. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !15

  28. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !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(); }
  29. 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() {
  30. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !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.
  31. None
  32. { "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
  33. None
  34. None
  35. 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.
  36. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !24 $this->entityManager->flush($order); !

  37. None
  38. None
  39. $oid = spl_object_hash($entity)

  40. $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.
  41. $oid = "00000000600cd8330000000053755686"

  42. Inside the UnitOfWork entities are identified by their object hash,

    not their primary key.
  43. None
  44. $item = new OrderItem(); $item->setName('Socks'); $item->setPrice('999'); $manager->persist($item); $order = new

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

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

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

    can persist your entities in any order you like.
  48. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !31

  49. {% 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>
  50. None
  51. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !34 1 QUERY TO FETCH ALL

    ORDERS
  52. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !34 1 QUERY TO FETCH ALL

    ORDERS + 1 QUERY/ORDER
 TO FETCH ITEMS
  53. None
  54. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !39

  55. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !40

  56. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !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(); }
  57. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !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(); }
  58. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !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(); }
  59. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !42 CUSTOM OBJECT HYDRATION

  60. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !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(); }
  61. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !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
  62. <?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;
  63. 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; } }
  64. None
  65. None
  66. 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(); }
  67. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !48 FETCH MODE

  68. /** * @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" * ) */
  69. Doctrine loads associated entities lazily by default. Writing custom queries

    with JOIN, custom DTO- Results or scalar results can improve read- performance.
  70. * @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) {
  71. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !52

  72. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !53 $order = $this->em->find(Order::class, 1); dump($this->em->getUnitOfWork()->getIdentityMap());

    $sameOrder = $this->em->find(Order::class, 1); dump($order === $sameOrder);
  73. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !53 $order = $this->em->find(Order::class, 1); dump($this->em->getUnitOfWork()->getIdentityMap());

    $sameOrder = $this->em->find(Order::class, 1); dump($order === $sameOrder);
  74. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !53 $order = $this->em->find(Order::class, 1); dump($this->em->getUnitOfWork()->getIdentityMap());

    $sameOrder = $this->em->find(Order::class, 1); dump($order === $sameOrder);
  75. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !54

  76. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !54

  77. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !54

  78. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !55 $order = $this->repository->find(1); !

  79. 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) .
  80. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !57 $item = new OrderItem(); $item->setName('Coffee');

    $item->setPrice('149'); $order = new Order(); $order->setId(1); $order->addItem($item); $this->manager->flush();
  81. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !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?
  82. 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).
  83. 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
  84. 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
  85. 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
  86. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !63 STATE

  87. None
  88. None
  89. None
  90. $order = new Order(); $order->setId(1); $this->entityManager->flush();

  91. None
  92. 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. !
  93. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !68 MERGE & DETACH

  94. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !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; }
  95. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !70 API Client App fetch Order

    with ID 2 EntityManager::find(Order, 2) returns Order data as JSON
  96. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !70 API Client App fetch Order

    with ID 2 EntityManager::find(Order, 2) returns Order data as JSON
  97. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !71 API Client App patch Order

    with ID 2
 (passes Order data as JSON)
  98. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !73 $patch = $this->serializer->deserialize( $request->getContent(), Order::class,

    'json' ); $updatedOrder = $this->entityManager->merge($patch); $this->entityManager->flush();
  99. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !74 DON'T

  100. 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. !
  101. Denis Brumann
 @dbrumann
 denis.brumann@sensiolabs.de !76 FLUSH($ENTITY) ENTITYMANAGER->MERGE($ENTITY) REMOVED IN DOCTRINE

    3 ENTITYMANAGER->DETACH($ENTITY)