SlideShare una empresa de Scribd logo
1 de 19
Descargar para leer sin conexión
Doctrine Tips’n’Tricks
(Ilya Antipenko, Grossum)
HOW TO ADD RELATIONSHIP FROM INVERSED SIDE?
• User YML config contains:
WWW.GROSSUM.COM SYMFONY CAFÉ KYIV
HOW TO ADD RELATIONSHIP FROM INVERSED SIDE?
• What will we get in profile table?
• Why? Doctrine documentation:
– Doctrine will only check the owning side of an association for changes.
– Changes made only to the inverse side of an association are ignored. Make sure
to update both sides of a bidirectional association (or at least the owning side,
from Doctrine’s point of view)
WWW.GROSSUM.COM
HOW TO FIX THIS BEHAVIOR?
WWW.GROSSUM.COM
HOW TO REMOVE ITEM FROM ONE-TO-MANY COLLECTION?
WWW.GROSSUM.COM
HOW TO REMOVE ITEM FROM ONE-TO-MANY COLLECTION?
WWW.GROSSUM.COM
o We should use “orphan removal” pattern to remove item from collection
INHERITANCE
• Mapped Superclasses
• Single Table Inheritance
• Class Table Inheritance
WWW.GROSSUM.COM
INHERITANCE. MAPPED SUPERCLASSES.
• Abstract
• Unidirectional relationships only => One-To-Many are not possible
• Many-To-Many associations are only possible if the mapped
superclass is only used in exactly one entity at the moment.
WWW.GROSSUM.COM
INHERITANCE. SINGLE TABLE INHERITANCE.
• All classes of hierarchy are mapped to a single table
• All fields, added to hierarchy entity, will be added to this single table
– Employee entity extended by Person
WWW.GROSSUM.COM
INHERITANCE. SINGLE TABLE INHERITANCE.
• After execute code:
• Table “person_single_table” contain two records for Employee and
Person entities:
WWW.GROSSUM.COM
INHERITANCE. SINGLE TABLE INHERITANCE.
• This strategy is very efficient for querying across all types in the
hierarchy or for specific types.
• No table joins are required, only a WHERE clause listing the type
identifiers. In particular, relationships involving types that employ this
mapping strategy are very performant.
• Otherwise Doctrine CANNOT create proxy instances of this entity and
will ALWAYS load the entity eagerly.
WWW.GROSSUM.COM
INHERITANCE. CLASS TABLE INHERITANCE.
• Several tables. One table for base entity and tables for each child
entity.
WWW.GROSSUM.COM
INHERITANCE. CLASS TABLE INHERITANCE.
• After execute code:
• Table “person_joined”:
• Table “employee_joined”:
WWW.GROSSUM.COM
INHERITANCE. CLASS TABLE INHERITANCE.
• This strategy inherently requires multiple JOIN operations to
perform just about any query which can have a negative impact on
performance.
• Doctrine CANNOT create proxy instances of this entity and will
ALWAYS load the entity eagerly.
WWW.GROSSUM.COM
COMPOSITE AND FOREIGN KEYS AS PRIMARY KEY
• Only allowed on Many-To-One or One-To-One associations.
• Set a key associationKey: with the field name of the association in
YAML.
• 3 USE-CASES:
– Dynamic Attributes
– Simple Derived Identity
– Join-Table with Metadata
WWW.GROSSUM.COM
Composite and Foreign Keys as Primary Key
DYNAMIC ATTRIBUTES
ArticleAttribute:
type: entity
id:
article:
associationKey: true
attribute:
type: string
fields:
value:
type: string
manyToOne:
article:
targetEntity: Article
inversedBy: attributes
Article:
type: entity
id:
id:
type: integer
generator:
strategy: AUTO
oneToMany:
targetEntity: ArticleAttribute
mappedBy: article
indexBy: attribute
cascade:
- all
Usage:
class Article
{
…
public function addAttribute($name,
$value)
{
$this->attributes[$name] = new
ArticleAttribute($name, $value, $this);
}
...
}
WWW.GROSSUM.COM
Composite and Foreign Keys as Primary Key
SIMPLE DERIVED IDENTITY
• Sometimes you have the requirement that two objects are related by a One-
To-One association and that the dependent class should re-use the primary
key of the class it depends on.
• We encounter problems with SonataORMAdminBundle
WWW.GROSSUM.COM
User:
type: entity
id:
id:
type: integer
generator:
strategy: AUTO
Address:
type: entity
id:
user:
associationKey:
true
oneToOne:
user:
targetEntity: User
Composite and Foreign Keys as Primary Key
JOIN-TABLE WITH METADATA
Order:
type: entity
id:
id:
type: integer
generator:
strategy: AUTO
manyToOne:
targetEntity: Customer
oneToMany:
targetEntity: OrderItem
mappedBy: order
Product:
type: entity
id:
id:
type: integer
generator:
strategy: AUTO
fields:
name:
type: string
price:
type: decimal
OrderItem:
type: entity
id:
order:
associationKey: true
product:
associationKey: true
manyToOne:
order:
targetEntity: Order
product:
targetEntity: Product
fields:
amount:
type: integer
WWW.GROSSUM.COM
THANKS!
ILYA ANTIPENKO
Company: Grossum
E-mail: ilya@antipenko.pp.ua
WWW.GROSSUM.COM SYMFONY CAFÉ KYIV
Examples seen in presentation can be found here:
github.com/aivus/doctrine-tips-n-tricks

Más contenido relacionado

Similar a Doctrine Tips and Tricks - Ilya Antipenko, Grossum

State management 1
State management 1State management 1
State management 1
singhadarsh
 
Mock Objects, Design and Dependency Inversion Principle
Mock Objects, Design and Dependency Inversion PrincipleMock Objects, Design and Dependency Inversion Principle
Mock Objects, Design and Dependency Inversion Principle
P Heinonen
 
Ocasta: Clustering Configuration Settings for Error Recovery (DSN'14)
Ocasta: Clustering Configuration Settings for Error Recovery (DSN'14)Ocasta: Clustering Configuration Settings for Error Recovery (DSN'14)
Ocasta: Clustering Configuration Settings for Error Recovery (DSN'14)
Zhen Huang
 

Similar a Doctrine Tips and Tricks - Ilya Antipenko, Grossum (20)

OOP in JS
OOP in JSOOP in JS
OOP in JS
 
All about Storage - Series 3 - All about indexes
All about Storage - Series 3 - All about indexesAll about Storage - Series 3 - All about indexes
All about Storage - Series 3 - All about indexes
 
Hello, React Hooks!
Hello, React Hooks!Hello, React Hooks!
Hello, React Hooks!
 
Salesforce Training Institute & Courses Pune | SFDC Training PCMC
Salesforce Training Institute & Courses Pune | SFDC Training PCMCSalesforce Training Institute & Courses Pune | SFDC Training PCMC
Salesforce Training Institute & Courses Pune | SFDC Training PCMC
 
Code Testability
Code TestabilityCode Testability
Code Testability
 
ORM Concepts and JPA 2.0 Specifications
ORM Concepts and JPA 2.0 SpecificationsORM Concepts and JPA 2.0 Specifications
ORM Concepts and JPA 2.0 Specifications
 
10 Powerful Ways to Optimize Your EE Site
10 Powerful Ways to Optimize Your EE Site10 Powerful Ways to Optimize Your EE Site
10 Powerful Ways to Optimize Your EE Site
 
Tech Talk - JPA and Query Optimization - publish
Tech Talk  -  JPA and Query Optimization - publishTech Talk  -  JPA and Query Optimization - publish
Tech Talk - JPA and Query Optimization - publish
 
AD1279 "Marty, You're Not Thinking Fourth Dimensionally" - Troubleshooting XP...
AD1279 "Marty, You're Not Thinking Fourth Dimensionally" - Troubleshooting XP...AD1279 "Marty, You're Not Thinking Fourth Dimensionally" - Troubleshooting XP...
AD1279 "Marty, You're Not Thinking Fourth Dimensionally" - Troubleshooting XP...
 
Advance sql - window functions patterns and tricks
Advance sql - window functions patterns and tricksAdvance sql - window functions patterns and tricks
Advance sql - window functions patterns and tricks
 
Migrating To PostgreSQL
Migrating To PostgreSQLMigrating To PostgreSQL
Migrating To PostgreSQL
 
State management 1
State management 1State management 1
State management 1
 
The deep bootstrap framework review
The deep bootstrap framework reviewThe deep bootstrap framework review
The deep bootstrap framework review
 
SaaSy maps - using django-tenants and geodjango to provide web-gis software-a...
SaaSy maps - using django-tenants and geodjango to provide web-gis software-a...SaaSy maps - using django-tenants and geodjango to provide web-gis software-a...
SaaSy maps - using django-tenants and geodjango to provide web-gis software-a...
 
Grails domain
Grails domainGrails domain
Grails domain
 
JPA and Hibernate
JPA and HibernateJPA and Hibernate
JPA and Hibernate
 
session07.ppt
session07.pptsession07.ppt
session07.ppt
 
7 Database Mistakes YOU Are Making -- Linuxfest Northwest 2019
7 Database Mistakes YOU Are Making -- Linuxfest Northwest 20197 Database Mistakes YOU Are Making -- Linuxfest Northwest 2019
7 Database Mistakes YOU Are Making -- Linuxfest Northwest 2019
 
Mock Objects, Design and Dependency Inversion Principle
Mock Objects, Design and Dependency Inversion PrincipleMock Objects, Design and Dependency Inversion Principle
Mock Objects, Design and Dependency Inversion Principle
 
Ocasta: Clustering Configuration Settings for Error Recovery (DSN'14)
Ocasta: Clustering Configuration Settings for Error Recovery (DSN'14)Ocasta: Clustering Configuration Settings for Error Recovery (DSN'14)
Ocasta: Clustering Configuration Settings for Error Recovery (DSN'14)
 

Último

Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
masabamasaba
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Último (20)

Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 

Doctrine Tips and Tricks - Ilya Antipenko, Grossum

  • 2. HOW TO ADD RELATIONSHIP FROM INVERSED SIDE? • User YML config contains: WWW.GROSSUM.COM SYMFONY CAFÉ KYIV
  • 3. HOW TO ADD RELATIONSHIP FROM INVERSED SIDE? • What will we get in profile table? • Why? Doctrine documentation: – Doctrine will only check the owning side of an association for changes. – Changes made only to the inverse side of an association are ignored. Make sure to update both sides of a bidirectional association (or at least the owning side, from Doctrine’s point of view) WWW.GROSSUM.COM
  • 4. HOW TO FIX THIS BEHAVIOR? WWW.GROSSUM.COM
  • 5. HOW TO REMOVE ITEM FROM ONE-TO-MANY COLLECTION? WWW.GROSSUM.COM
  • 6. HOW TO REMOVE ITEM FROM ONE-TO-MANY COLLECTION? WWW.GROSSUM.COM o We should use “orphan removal” pattern to remove item from collection
  • 7. INHERITANCE • Mapped Superclasses • Single Table Inheritance • Class Table Inheritance WWW.GROSSUM.COM
  • 8. INHERITANCE. MAPPED SUPERCLASSES. • Abstract • Unidirectional relationships only => One-To-Many are not possible • Many-To-Many associations are only possible if the mapped superclass is only used in exactly one entity at the moment. WWW.GROSSUM.COM
  • 9. INHERITANCE. SINGLE TABLE INHERITANCE. • All classes of hierarchy are mapped to a single table • All fields, added to hierarchy entity, will be added to this single table – Employee entity extended by Person WWW.GROSSUM.COM
  • 10. INHERITANCE. SINGLE TABLE INHERITANCE. • After execute code: • Table “person_single_table” contain two records for Employee and Person entities: WWW.GROSSUM.COM
  • 11. INHERITANCE. SINGLE TABLE INHERITANCE. • This strategy is very efficient for querying across all types in the hierarchy or for specific types. • No table joins are required, only a WHERE clause listing the type identifiers. In particular, relationships involving types that employ this mapping strategy are very performant. • Otherwise Doctrine CANNOT create proxy instances of this entity and will ALWAYS load the entity eagerly. WWW.GROSSUM.COM
  • 12. INHERITANCE. CLASS TABLE INHERITANCE. • Several tables. One table for base entity and tables for each child entity. WWW.GROSSUM.COM
  • 13. INHERITANCE. CLASS TABLE INHERITANCE. • After execute code: • Table “person_joined”: • Table “employee_joined”: WWW.GROSSUM.COM
  • 14. INHERITANCE. CLASS TABLE INHERITANCE. • This strategy inherently requires multiple JOIN operations to perform just about any query which can have a negative impact on performance. • Doctrine CANNOT create proxy instances of this entity and will ALWAYS load the entity eagerly. WWW.GROSSUM.COM
  • 15. COMPOSITE AND FOREIGN KEYS AS PRIMARY KEY • Only allowed on Many-To-One or One-To-One associations. • Set a key associationKey: with the field name of the association in YAML. • 3 USE-CASES: – Dynamic Attributes – Simple Derived Identity – Join-Table with Metadata WWW.GROSSUM.COM
  • 16. Composite and Foreign Keys as Primary Key DYNAMIC ATTRIBUTES ArticleAttribute: type: entity id: article: associationKey: true attribute: type: string fields: value: type: string manyToOne: article: targetEntity: Article inversedBy: attributes Article: type: entity id: id: type: integer generator: strategy: AUTO oneToMany: targetEntity: ArticleAttribute mappedBy: article indexBy: attribute cascade: - all Usage: class Article { … public function addAttribute($name, $value) { $this->attributes[$name] = new ArticleAttribute($name, $value, $this); } ... } WWW.GROSSUM.COM
  • 17. Composite and Foreign Keys as Primary Key SIMPLE DERIVED IDENTITY • Sometimes you have the requirement that two objects are related by a One- To-One association and that the dependent class should re-use the primary key of the class it depends on. • We encounter problems with SonataORMAdminBundle WWW.GROSSUM.COM User: type: entity id: id: type: integer generator: strategy: AUTO Address: type: entity id: user: associationKey: true oneToOne: user: targetEntity: User
  • 18. Composite and Foreign Keys as Primary Key JOIN-TABLE WITH METADATA Order: type: entity id: id: type: integer generator: strategy: AUTO manyToOne: targetEntity: Customer oneToMany: targetEntity: OrderItem mappedBy: order Product: type: entity id: id: type: integer generator: strategy: AUTO fields: name: type: string price: type: decimal OrderItem: type: entity id: order: associationKey: true product: associationKey: true manyToOne: order: targetEntity: Order product: targetEntity: Product fields: amount: type: integer WWW.GROSSUM.COM
  • 19. THANKS! ILYA ANTIPENKO Company: Grossum E-mail: ilya@antipenko.pp.ua WWW.GROSSUM.COM SYMFONY CAFÉ KYIV Examples seen in presentation can be found here: github.com/aivus/doctrine-tips-n-tricks