Date post: | 16-Apr-2017 |
Category: |
Technology |
Upload: | simone-di-maulo |
View: | 197 times |
Download: | 0 times |
ORM Hero
Hello!I am Simone Di Maulo
Software Engineer @ AdEspressoOOP
FP
BEER@toretto460
Why do you need an ORM
?
IMPEDANCE MISMATCH
RBDMSObjectsObject reference IntegerFloatDoubleStringDateTime…
Table RowForeignKey
IntegerFloat
DoubleStringDate
…
PHP
Author Book
Author
Book
Book
Common mistake
Magic
Magic
> 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 -
HOW DOES IT WORK ¿
METADATA
/** * @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
/** @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
IDENTITY MAP
$em->find(Author::class, $authorId);
// query $author1 = $em->find(Author::class, 1);
// no query $author2 = $em->find(Author::class, 1);
$author1 === $author2
// bypass the identity map // a query were submitted to the DB
$authorRepository->findByName($authorName);
IDENTIFIERS
$user = User::register(/*..*/);
$em->persist($user);
$em->flush()
$user = User::register(/*..*/);
$em->persist($user);
$em->flush()
UUId
Auto Increment
Sequence
$user = User::register(/*..*/);
$em->persist($user);
$em->flush()
UUId
Auto Increment
Sequence
echo $user->getId();
echo $user->getId();
echo $user->getId();
$user = User::register(/*..*/);
$em->persist($user);
$em->flush()
UUId
// null
// 1
// 1
Auto Increment
Sequence
echo $user->getId();
echo $user->getId();
echo $user->getId();
$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();
$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();
Unit of work
Unit of Work
$this->em->persist($author);
DETECT CHANGES
$author = $em->find(Author::class, $authorId);
$author->addBook(. . .);
$this->em->flush();
IMPLICIT
/** * @Entity * @ChangeTrackingPolicy( * “DEFERRED_IMPLICIT" * ) */ class Author{}
/** * @Entity * @ChangeTrackingPolicy("DEFERRED_EXPLICIT") */ class Author{}
$this->em->persist($alessandro); $this->em->flush();
EXPLICIT
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
Read
DEMOThanks to https://github.com/Ocramius/Doctrine2StepHydration
DON’T complain
caches DOMAIN
ENTITY MANAGER
UNIT OF WORK REPOSITORY
See Doctrine\Cache @ SymfonyDayIt 2015 - https://vimeo.com/144858752
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
USE THE RIGHT TOOL
consider alternatives for :
Reporting
Append only data
CQRS
QUESTIONS