SlideShare uma empresa Scribd logo
1 de 19
Baixar para ler offline
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

Mais conteúdo relacionado

Semelhante a Doctrine Tips and Tricks - Ilya Antipenko, Grossum

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 indexesDAGEOP LTD
 
Hello, React Hooks!
Hello, React Hooks!Hello, React Hooks!
Hello, React Hooks!Heejong Ahn
 
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 PCMCvictoriousdigital
 
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 SpecificationsAhmed Ramzy
 
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 SiteLeslie Doherty
 
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 - publishGleydson Lima
 
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...Paul Withers
 
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 tricksEyal Trabelsi
 
Migrating To PostgreSQL
Migrating To PostgreSQLMigrating To PostgreSQL
Migrating To PostgreSQLGrant Fritchey
 
State management 1
State management 1State management 1
State management 1singhadarsh
 
The deep bootstrap framework review
The deep bootstrap framework reviewThe deep bootstrap framework review
The deep bootstrap framework reviewtaeseon ryu
 
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...Anusha Chickermane
 
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 2019Dave Stokes
 
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 PrincipleP 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
 

Semelhante 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

The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...kalichargn70th171
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
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...Jittipong Loespradit
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfVishalKumarJha10
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
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 insideshinachiaurasa2
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdfPearlKirahMaeRagusta1
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 

Último (20)

The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
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...
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
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
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
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
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 

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