~/talk

Doctrine ORM Good Practices and Tricks

Talkat PHP UK Conference 2016 · 2016-02-18..02-19· [php, databases]
slides:none on file

Doctrine ORM targets OLTP not reporting and benefits from domain-first design: entities must be valid after construct, use DTOs for form input, favor immutable or append-only models and avoid lifecycle callbacks, bidirectional associations and soft deletes. Practical hacks: use UUIDs for identities so objects work before DB roundtrips (Ben Ramsey library and UUID collision tiny even at 1M/s for 100 years), enable second level cache to speed reads, prefer SQL or custom repositories for heavy queries, and use append-only reads for simple auditing.

AI-written summary · about

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

Ignored while you are typing. Pages without a filter rail have no search to focus.