+ All Categories
Home > Technology > Orm hero

Orm hero

Date post: 16-Apr-2017
Category:
Upload: simone-di-maulo
View: 197 times
Download: 0 times
Share this document with a friend
40
ORM Hero
Transcript
Page 1: Orm hero

ORM Hero

Page 2: Orm hero

Hello!I am Simone Di Maulo

Software Engineer @ AdEspressoOOP

FP

BEER@toretto460

Page 3: Orm hero

Why do you need an ORM

?

Page 4: Orm hero

IMPEDANCE MISMATCH

Page 5: Orm hero

RBDMSObjectsObject reference IntegerFloatDoubleStringDateTime…

Table RowForeignKey

IntegerFloat

DoubleStringDate

PHP

Page 6: Orm hero

Author Book

Author

Book

Book

Page 7: Orm hero

Common mistake

Page 8: Orm hero

Magic

Page 9: Orm hero

Magic

Page 10: Orm hero

> 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 -

Page 11: Orm hero

HOW DOES IT WORK ¿

Page 12: Orm hero

METADATA

Page 13: Orm hero

/** * @ORM\Table(name=“author") * @ORM\Entity */class Author{ /** * @ORM\Column(name=“id", type=“string") * @ORM\Id */ private $id;

/** * @ORM\Column(name=“name", type="string", length=255) */ private $name;

/** * @ORM\OneToMany(targetEntity=“Book", mappedBy=“author", * cascade={“persist"}) */ private $books;}

MAPPING

Page 14: Orm hero

/** @ORM\Table(name="book") @ORM\Entity */class Book{ /**@ORM\Column(name="id", type="string") @ORM\Id()*/ private $id;

/**@ORM\ManyToOne(targetEntity="Author", inversedBy="books") */ private $author;

/**@ORM\Column(name="publisher", type="string", length=255) */ private $publisher;

/**@ORM\Column(name="title", type="string", length=255) */ private $title;

/**@ORM\Column(name="published_at", type="datetime", * nullable=false) */ private $publishedAt;}

MAPPING

Page 15: Orm hero

IDENTITY MAP

Page 16: Orm hero

$em->find(Author::class, $authorId);

Page 17: Orm hero

// query $author1 = $em->find(Author::class, 1);

// no query $author2 = $em->find(Author::class, 1);

$author1 === $author2

Page 18: Orm hero

// bypass the identity map // a query were submitted to the DB

$authorRepository->findByName($authorName);

Page 19: Orm hero

IDENTIFIERS

Page 20: Orm hero

$user = User::register(/*..*/);

$em->persist($user);

$em->flush()

Page 21: Orm hero

$user = User::register(/*..*/);

$em->persist($user);

$em->flush()

UUId

Auto Increment

Sequence

Page 22: Orm hero

$user = User::register(/*..*/);

$em->persist($user);

$em->flush()

UUId

Auto Increment

Sequence

echo $user->getId();

echo $user->getId();

echo $user->getId();

Page 23: Orm hero

$user = User::register(/*..*/);

$em->persist($user);

$em->flush()

UUId

// null

// 1

// 1

Auto Increment

Sequence

echo $user->getId();

echo $user->getId();

echo $user->getId();

Page 24: Orm hero

$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();

Page 25: Orm hero

$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();

Page 26: Orm hero

Unit of work

Page 27: Orm hero

Unit of Work

$this->em->persist($author);

Page 28: Orm hero

DETECT CHANGES

Page 29: Orm hero

$author = $em->find(Author::class, $authorId);

$author->addBook(. . .);

$this->em->flush();

IMPLICIT

/** * @Entity * @ChangeTrackingPolicy( * “DEFERRED_IMPLICIT" * ) */ class Author{}

Page 30: Orm hero

/** * @Entity * @ChangeTrackingPolicy("DEFERRED_EXPLICIT") */ class Author{}

$this->em->persist($alessandro); $this->em->flush();

EXPLICIT

Page 31: Orm hero

use Doctrine\Common\NotifyPropertyChanged, Doctrine\Common\PropertyChangedListener;

/** * @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

Page 32: Orm hero

Read

Page 33: Orm hero

DEMOThanks to https://github.com/Ocramius/Doctrine2StepHydration

Page 34: Orm hero

DON’T complain

Page 35: Orm hero

caches DOMAIN

ENTITY MANAGER

UNIT OF WORK REPOSITORY

See Doctrine\Cache @ SymfonyDayIt 2015 - https://vimeo.com/144858752

Page 36: Orm hero

caches DOMAIN

ENTITY MANAGER

UNIT OF WORK REPOSITORY

metadata cache

sql cacheresult cache

identity map

2° level cache

See Doctrine\Cache @ SymfonyDayIt 2015 - https://vimeo.com/144858752

Page 37: Orm hero

USE THE RIGHT TOOL

Page 38: Orm hero

consider alternatives for :

Reporting

Append only data

CQRS

Page 39: Orm hero

QUESTIONS

Page 40: Orm hero

THANKShttps://joind.in/talk/0cc4f


Recommended