Advertisement

Orm hero

Software Engineer at AdEspresso
Oct. 28, 2016
Advertisement

More Related Content

Advertisement

Orm hero

  1. ORM Hero
  2. Hello! I am Simone Di Maulo Software Engineer @ AdEspresso OOP FP BEER @toretto460
  3. Why do you need an ORM ?
  4. IMPEDANCE MISMATCH
  5. RBDMS Objects Object reference Integer Float Double String DateTime … Table Row ForeignKey Integer Float Double String Date … PHP
  6. Author Book Author Book Book
  7. Common mistake
  8. Magic
  9. Magic
  10. > Mapping to a relational database involves lots of repetitive, boiler-plate code. A framework that allows me to avoid 80% of that is worthwhile even if it is only 80%. The problem is in me for pretending it's 100%. http://martinfowler.com/bliki/OrmHate.html - Martin Fowler -
  11. HOW DOES IT WORK ¿
  12. METADATA
  13. /** * @ORMTable(name=“author") * @ORMEntity */ class Author { /** * @ORMColumn(name=“id", type=“string") * @ORMId */ private $id; /** * @ORMColumn(name=“name", type="string", length=255) */ private $name; /** * @ORMOneToMany(targetEntity=“Book", mappedBy=“author", * cascade={“persist"}) */ private $books; } MAPPING
  14. /** @ORMTable(name="book") @ORMEntity */ class Book { /**@ORMColumn(name="id", type="string") @ORMId()*/ private $id; /**@ORMManyToOne(targetEntity="Author", inversedBy="books") */ private $author; /**@ORMColumn(name="publisher", type="string", length=255) */ private $publisher; /**@ORMColumn(name="title", type="string", length=255) */ private $title; /**@ORMColumn(name="published_at", type="datetime", * nullable=false) */ private $publishedAt; } MAPPING
  15. IDENTITY MAP
  16. $em->find(Author::class, $authorId);
  17. // query $author1 = $em->find(Author::class, 1); // no query $author2 = $em->find(Author::class, 1); $author1 === $author2
  18. // bypass the identity map // a query were submitted to the DB $authorRepository->findByName($authorName);
  19. IDENTIFIERS
  20. $user = User::register(/*..*/); $em->persist($user); $em->flush()
  21. $user = User::register(/*..*/); $em->persist($user); $em->flush() UUId Auto Increment Sequence
  22. $user = User::register(/*..*/); $em->persist($user); $em->flush() UUId Auto Increment Sequence echo $user->getId(); echo $user->getId(); echo $user->getId();
  23. $user = User::register(/*..*/); $em->persist($user); $em->flush() UUId // null // 1 // 1 Auto Increment Sequence echo $user->getId(); echo $user->getId(); echo $user->getId();
  24. $user = User::register(/*..*/); $em->persist($user); $em->flush() UUId // null // 1 // 1 Auto Increment // null // null // 1 Sequence echo $user->getId(); echo $user->getId(); echo $user->getId();
  25. $user = User::register(/*..*/); $em->persist($user); $em->flush() UUId // 2840555c-54ab-aa68-96 // 2840555c-54ab-aa68-96 // 2840555c-54ab-aa68-96 // null // 1 // 1 Auto Increment // null // null // 1 Sequence echo $user->getId(); echo $user->getId(); echo $user->getId();
  26. Unit of work
  27. Unit of Work $this->em->persist($author);
  28. DETECT 
 CHANGES
  29. $author = $em->find(Author::class, $authorId); 
 $author->addBook(. . .); 
 $this->em->flush(); IMPLICIT /** * @Entity * @ChangeTrackingPolicy( * “DEFERRED_IMPLICIT" * ) */ class Author{}
  30. /** * @Entity * @ChangeTrackingPolicy("DEFERRED_EXPLICIT") */ class Author{} $this->em->persist($alessandro); $this->em->flush(); EXPLICIT
  31. use DoctrineCommonNotifyPropertyChanged, DoctrineCommonPropertyChangedListener; /** * @Entity * @ChangeTrackingPolicy(“NOTIFY") */ class Author implements NotifyPropertyChanged { private $_listeners = array(); public function addPropertyChangedListener(PropertyChangedListener $listener) { $this->_listeners[] = $listener; } } foreach ($this->_listeners as $listener) { $listener->propertyChanged($this, $propName, $oldValue, $newValue); } NOTIFY
  32. Read
  33. DEMO Thanks to https://github.com/Ocramius/Doctrine2StepHydration
  34. DON’T complain
  35. caches DOMAIN ENTITY MANAGER UNIT OF WORK REPOSITORY See DoctrineCache @ SymfonyDayIt 2015 - https://vimeo.com/144858752
  36. caches DOMAIN ENTITY MANAGER UNIT OF WORK REPOSITORY metadata cache sql cache result cache identity map 2° level cache See DoctrineCache @ SymfonyDayIt 2015 - https://vimeo.com/144858752
  37. USE THE RIGHT TOOL
  38. consider alternatives for : Reporting Append only data CQRS
  39. QUESTIONS
  40. THANKS https://joind.in/talk/0cc4f
Advertisement