Doctrine ORM Good Practices and Tricks

Doctrine ORM Good Practices and Tricks

PHP UK Conference via YouTube Direct link

Intro

1 of 55

1 of 55

Intro

Class Central Classrooms beta

YouTube videos curated by Class Central.

Classroom Contents

Doctrine ORM Good Practices and Tricks

Automatically move to the next video in the Classroom when playback concludes

  1. 1 Intro
  2. 2 DOCTRINE PROJECT A group of persistence-oriented libraries for PHP
  3. 3 WE ARE LOOKING FOR CONTRIBUTORS! (Must be prepared to take unpopular decisions)
  4. 4 DOCTRINE 2 ORM BEST PRACTICES
  5. 5 KNOW YOUR ENEMY
  6. 6 OLTP
  7. 7 FAST PROTOTYPING
  8. 8 DYNAMIC DATA STRUCTURES
  9. 9 REPORTING That's what SQL is for It's a QUERY language, after all
  10. 10 ENTITIES (OF COURSE) Entities should work without the ORM Entities should work without the DB
  11. 11 ENTITIES (MOSTLY) REPRESENT YOUR DOMAIN
  12. 12 THE DATABASE IS JUST SAVING THINGS
  13. 13 DESIGN ENTITIES FIRST
  14. 14 DEFINE THE DATABASE AFTER MODELING YOUR DOMAIN
  15. 15 DEFINE MAPPINGS AFTER DESIGNING THE ENTITIES
  16. 16 DESIGNING ENTITIES
  17. 17 ENTITIES ARE NOT TYPED ARRAYS
  18. 18 ENTITIES HAVE BEHAVIOR
  19. 19 ENTITY INTERNAL STATE DESIGN IS IRRELEVANT You can deal with state after designing the API Designing State-first leads to terrible coupling
  20. 20 RESPECT THE LOD
  21. 21 DISALLOW COLLECTION ACCESS FROM OUTSIDE THE ENTITY
  22. 22 STAY VALID AFTER _CONSTRUCT (Regardless of the DB) Named constructors are OK
  23. 23 AVOID SETTERS
  24. 24 AVOID COUPLING WITH THE APPLICATION LAYER
  25. 25 FORM COMPONENTS BREAK ENTITY VALIDITY
  26. 26 AVOID LIFECYCLE CALLBACKS
  27. 27 LIFECYCLE CALLBACKS ARE A PERSISTENCE HACK
  28. 28 AUTO INCREMENT IS ABUSED FOR SORTING Are you looking for a DATETIME field instead?
  29. 29 AVOID DERIVED PRIMARY KEYS You are just normalizing for the sake of it Does your domain really NEED it?
  30. 30 AVOID COMPOSITE PRIMARY KEYS Any reason to not use an unique constraint instead? Do they make a difference in your domain?
  31. 31 FAVOUR IMMUTABLE ENTITIES Or append-only data-structures
  32. 32 Immutable data is simple Immutable data is cacheable (forever) Immutable data is predictable Immutable data enables historical analysis
  33. 33 AVOID SOFT-DELETES
  34. 34 SOFT DELETES ARE A BROKEN IDEA
  35. 35 SOFT DELETES BREAK DATA INTEGRITY (and therefore validity)
  36. 36 Soft Deletes can usually be replaced with more specific domain concepts
  37. 37 MAPPING DRIVER CHOICE
  38. 38 USE ANNOTATIONS IN PRIVATE PACKAGES
  39. 39 USE XML MAPPINGS IN PUBLIC PACKAGES
  40. 40 LAZY OR EAGER?
  41. 41 EAGER LOADING IS USELESS
  42. 42 EXTRA LAZY INDICATES HIGH RISK AREAS We reach the limits of the ORM Careful about performance/transaction size!
  43. 43 AVOID BI-DIRECTIONAL ASSOCIATIONS
  44. 44 BI-DIRECTIONAL ASSOCIATIONS ARE OVERHEAD
  45. 45 USE CUSTOM REPOSITORIES FOR IMPROVED EXPRESSIVENESS
  46. 46 REPOSITORIES ARE SERVICES So are Query Functions
  47. 47 INJECT REPOSITORIES INSTEAD
  48. 48 KEEP NORMALIZATION UNDER CONTROL
  49. 49 KEEP NORMALIZATION (FREAKS) UNDER CONTROL
  50. 50 You may need to gag your DBAs... Or get them to understand your needs Academic and practical knowledge may differ
  51. 51 WHAT ABOUT PERFORMANCE?
  52. 52 RECAP
  53. 53 DOMAIN FIRST
  54. 54 DO NOT NORMALIZE WITHOUT A NEED FOR IT Otherwise, you're digging your own grave!
  55. 55 CONSIDER USING SEPARATE DBS Transactional Data

Never Stop Learning.

Get personalized course recommendations, track subjects and courses with reminders, and more.

Someone learning on their laptop while sitting on the floor.