SlideShare ist ein Scribd-Unternehmen logo
1 von 40
Downloaden Sie, um offline zu lesen
ORM Hero
Hello!
I am Simone Di Maulo
Software Engineer @ AdEspresso
OOP
FP
BEER
@toretto460
Why do you need
an ORM
?
IMPEDANCE
MISMATCH
RBDMS
Objects
Object reference
Integer
Float
Double
String
DateTime
…
Table Row
ForeignKey
Integer
Float
Double
String
Date
…
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
/**
* @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
/** @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
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 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
Read
DEMO
Thanks to https://github.com/Ocramius/Doctrine2StepHydration
DON’T
complain
caches DOMAIN
ENTITY MANAGER
UNIT OF WORK REPOSITORY
See DoctrineCache @ SymfonyDayIt 2015 - https://vimeo.com/144858752
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
USE THE
RIGHT TOOL
consider alternatives for :
Reporting
Append only data
CQRS
QUESTIONS
THANKS
https://joind.in/talk/0cc4f

Weitere ähnliche Inhalte

Andere mochten auch

ORM, JPA, & Hibernate Overview
ORM, JPA, & Hibernate OverviewORM, JPA, & Hibernate Overview
ORM, JPA, & Hibernate Overview
Brett Meyer
 
Operational risk management (orm)
Operational risk management (orm)Operational risk management (orm)
Operational risk management (orm)
Bushra Angbeen
 

Andere mochten auch (8)

Why not ORM
Why not ORMWhy not ORM
Why not ORM
 
ORM is offensive
ORM is offensiveORM is offensive
ORM is offensive
 
ORM: Object-relational mapping
ORM: Object-relational mappingORM: Object-relational mapping
ORM: Object-relational mapping
 
ORM을 활용할 경우의 설계, 개발 과정
ORM을 활용할 경우의 설계, 개발 과정ORM을 활용할 경우의 설계, 개발 과정
ORM을 활용할 경우의 설계, 개발 과정
 
Reform: путь к лучшему ORM
Reform: путь к лучшему ORMReform: путь к лучшему ORM
Reform: путь к лучшему ORM
 
Hibernate ORM: Tips, Tricks, and Performance Techniques
Hibernate ORM: Tips, Tricks, and Performance TechniquesHibernate ORM: Tips, Tricks, and Performance Techniques
Hibernate ORM: Tips, Tricks, and Performance Techniques
 
ORM, JPA, & Hibernate Overview
ORM, JPA, & Hibernate OverviewORM, JPA, & Hibernate Overview
ORM, JPA, & Hibernate Overview
 
Operational risk management (orm)
Operational risk management (orm)Operational risk management (orm)
Operational risk management (orm)
 

Ähnlich wie Orm hero

Open Source Package Php Mysql 1228203701094763 9
Open Source Package Php Mysql 1228203701094763 9Open Source Package Php Mysql 1228203701094763 9
Open Source Package Php Mysql 1228203701094763 9
isadorta
 
DDD on example of Symfony (SfCampUA14)
DDD on example of Symfony (SfCampUA14)DDD on example of Symfony (SfCampUA14)
DDD on example of Symfony (SfCampUA14)
Oleg Zinchenko
 
Debugging and Error handling
Debugging and Error handlingDebugging and Error handling
Debugging and Error handling
Suite Solutions
 

Ähnlich wie Orm hero (20)

Don't scrape, Glean!
Don't scrape, Glean!Don't scrape, Glean!
Don't scrape, Glean!
 
A Toda Maquina Con Ruby on Rails
A Toda Maquina Con Ruby on RailsA Toda Maquina Con Ruby on Rails
A Toda Maquina Con Ruby on Rails
 
Um2010
Um2010Um2010
Um2010
 
Ruby Hell Yeah
Ruby Hell YeahRuby Hell Yeah
Ruby Hell Yeah
 
John Rowley Notes
John Rowley NotesJohn Rowley Notes
John Rowley Notes
 
/Regex makes me want to (weep|give up|(╯°□°)╯︵ ┻━┻)\.?/i
/Regex makes me want to (weep|give up|(╯°□°)╯︵ ┻━┻)\.?/i/Regex makes me want to (weep|give up|(╯°□°)╯︵ ┻━┻)\.?/i
/Regex makes me want to (weep|give up|(╯°□°)╯︵ ┻━┻)\.?/i
 
REST in practice with Symfony2
REST in practice with Symfony2REST in practice with Symfony2
REST in practice with Symfony2
 
Rest in practice con Symfony2
Rest in practice con Symfony2Rest in practice con Symfony2
Rest in practice con Symfony2
 
Write your Ruby in Style
Write your Ruby in StyleWrite your Ruby in Style
Write your Ruby in Style
 
Open Source Package PHP & MySQL
Open Source Package PHP & MySQLOpen Source Package PHP & MySQL
Open Source Package PHP & MySQL
 
Open Source Package Php Mysql 1228203701094763 9
Open Source Package Php Mysql 1228203701094763 9Open Source Package Php Mysql 1228203701094763 9
Open Source Package Php Mysql 1228203701094763 9
 
Origami, a monadic fold library for Scala
Origami, a monadic fold library for ScalaOrigami, a monadic fold library for Scala
Origami, a monadic fold library for Scala
 
PHP MySQL
PHP MySQLPHP MySQL
PHP MySQL
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
Code with Style - PyOhio
Code with Style - PyOhioCode with Style - PyOhio
Code with Style - PyOhio
 
Hacking 101 for developers
Hacking 101 for developersHacking 101 for developers
Hacking 101 for developers
 
Code with style
Code with styleCode with style
Code with style
 
How Xslate Works
How Xslate WorksHow Xslate Works
How Xslate Works
 
DDD on example of Symfony (SfCampUA14)
DDD on example of Symfony (SfCampUA14)DDD on example of Symfony (SfCampUA14)
DDD on example of Symfony (SfCampUA14)
 
Debugging and Error handling
Debugging and Error handlingDebugging and Error handling
Debugging and Error handling
 

Kürzlich hochgeladen

Kürzlich hochgeladen (20)

Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 

Orm hero