Upgrade to Pro — share decks privately, control downloads, hide ads and more …

MongoDB in Symfony2 with Doctrine - Slides

dknx01
March 01, 2016

MongoDB in Symfony2 with Doctrine - Slides

Slides from presentation @ bephpug (Berlin PHP Usergroup)

dknx01

March 01, 2016
Tweet

More Decks by dknx01

Other Decks in Programming

Transcript

  1. Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further

    readings Ende MongoDb with Doctrine A short introduction Erik Witthauer [email protected] twitter: ewnx01 February 26, 2016
  2. Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further

    readings Ende Agenda Requirements Conguration Documents SubDocuments Property annotations Mongo ObjectId Unique elds Strings and Integer elds DateTime elds Boolean elds Subdocument elds Array elds Example Entry Repository Saving a document Find by a eld Find with limit, skipping, sorting and one condition Get last inserted entry Get all entries based on one eld value Further readings
  3. Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further

    readings Ende Requirements • PHP: >=5.3.9 • MongoDB Server • Doctrine: 2.4.8
  4. Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further

    readings Ende Requirements • PHP: >=5.3.9 • MongoDB Server • Doctrine: 2.4.8 • Doctrine MongoDB-ODM: 1.0.3
  5. Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further

    readings Ende Requirements • PHP: >=5.3.9 • MongoDB Server • Doctrine: 2.4.8 • Doctrine MongoDB-ODM: 1.0.3 • Doctrine MongoDB-ODM-Bundle: 3.1.x-dev
  6. Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further

    readings Ende Requirements • PHP: >=5.3.9 • MongoDB Server • Doctrine: 2.4.8 • Doctrine MongoDB-ODM: 1.0.3 • Doctrine MongoDB-ODM-Bundle: 3.1.x-dev • Symfony: >=2.7
  7. Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further

    readings Ende Conguration E.g. in Symfony: 1 # Doctrine C o n f i g u r a t i o n 2 doctrine: 3 dbal: # NORMAL C O N F I G U R A T I O N E . G . MySQL 4 doctrine_mongodb: 5 connections: 6 d e f a u l t : 7 s e r v e r : mongodb:// l o c a l h o s t :27017 8 options: { username: " % m o n g o d b _ u s e r % " , password: " % m o n g o d b _ p a s s w o r d % "} 9 default_database: t e s t 10 document_managers: 11 d e f a u l t : 12 auto_mapping: true
  8. Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further

    readings Ende Documents Documents are data sets or 'rows' in MongoDB
  9. Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further

    readings Ende Documents Documents are data sets or 'rows' in MongoDB Custom Doctrine annotations needed 1 use Doctrine \ODM\MongoDB\Mapping\ Annotations as MongoDB ; 1 /∗∗ 2 ∗ @MongoDB\Document ( r e p o s i t o r y C l a s s="PATH\TO\REPOSITORY") 3 ∗/
  10. Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further

    readings Ende SubDocuments SubDocuments are documents embeded into other documents
  11. Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further

    readings Ende SubDocuments SubDocuments are documents embeded into other documents 1 /∗∗ 2 ∗ @MongoDB\EmbeddedDocument 3 ∗/
  12. Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further

    readings Ende Property annotations - Mongo ObjectId Unique object id, used to reference to it (like a primary key).
  13. Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further

    readings Ende Property annotations - Mongo ObjectId Unique object id, used to reference to it (like a primary key). 1 /∗∗ 2 ∗ @var s t r i n g 3 ∗ @MongoDB\ S t r i n g 4 ∗ @MongoDB\ ObjectId 5 ∗ @MongoDB\ Id (name="_id " , s t r a t e g y="Auto ") 6 ∗/ 7 protected $id ;
  14. Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further

    readings Ende Property annotations - Unique elds Unique elds are refered as UniqueIndex
  15. Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further

    readings Ende Property annotations - Unique elds Unique elds are refered as UniqueIndex 1 /∗∗ 2 ∗ @var s t r i n g 3 ∗ @MongoDB\ S t r i n g 4 ∗ @MongoDB\ UniqueIndex 5 ∗/ 6 protected $ t w i t t e r I d ;
  16. Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further

    readings Ende Property annotations - Strings and Integer elds 1 /∗∗ 2 ∗ @var s t r i n g 3 ∗ @MongoDB\ S t r i n g 4 ∗/ 5 protected $text ; 6 7 /∗∗ 8 ∗ @var i n t 9 ∗ @MongoDB\ I n t 10 ∗/ 11 protected $retweetCount ;
  17. Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further

    readings Ende Property annotations - DateTime elds PHP DateTime object should/must be saved as MongoDate
  18. Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further

    readings Ende Property annotations - DateTime elds PHP DateTime object should/must be saved as MongoDate 1 /∗∗ 2 ∗ @var DateTime 3 ∗ @MongoDB\Date 4 ∗/ 5 protected $createdAt ;
  19. Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further

    readings Ende Property annotations - Boolean elds DB NULL or not set => PHP false
  20. Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further

    readings Ende Property annotations - Boolean elds DB NULL or not set => PHP false If the eld is null, the datdabase must not be lled/created
  21. Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further

    readings Ende Property annotations - Boolean elds DB NULL or not set => PHP false If the eld is null, the datdabase must not be lled/created 1 /∗∗ 2 ∗ @var boolean 3 ∗ @MongoDB\ Boolean 4 ∗/ 5 protected $pinned ;
  22. Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further

    readings Ende Property annotations - Subdocument elds Object inside the current one
  23. Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further

    readings Ende Property annotations - Subdocument elds Object inside the current one Same structure inside MongoDB
  24. Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further

    readings Ende Property annotations - Subdocument elds Object inside the current one Same structure inside MongoDB 1 /∗∗ 2 ∗ @var OriginalData 3 ∗ @MongoDB\EmbedOne( targetDocument="Document\Sub\ OriginalData ") 4 ∗/ 5 protected $originalData ;
  25. Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further

    readings Ende Property annotations - Array elds MongoDB can holds arrays
  26. Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further

    readings Ende Property annotations - Array elds MongoDB can holds arrays In PHP like a normal array
  27. Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further

    readings Ende Property annotations - Array elds MongoDB can holds arrays In PHP like a normal array 1 /∗∗ 2 ∗ @var array 3 ∗ @MongoDB\ C o l l e c t i o n 4 ∗/ 5 protected $mentions = array () ;
  28. Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further

    readings Ende Property annotations - Example Entry The source code for an example class can be found at slide 10: https://speakerdeck.com/dknx01/ mongodb-in-symfony2-with-doctrine
  29. Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further

    readings Ende Property annotations - Example Entry The source code for an example class can be found at slide 10: https://speakerdeck.com/dknx01/ mongodb-in-symfony2-with-doctrine Figure: example entry
  30. Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further

    readings Ende Repository - Saving a document 1 /∗∗ 2 ∗ @param TwitterEntry $ t w i t t e r E n t r y 3 ∗/ 4 p u b l i c f u n c t i o n save ( TwitterEntry $ t w i t t e r E n t r y ) 5 { 6 $this −>dm−>p e r s i s t ( $ t w i t t e r E n t r y ) ; 7 $this −>dm−>f l u s h ( $ t w i t t e r E n t r y ) ; 8 }
  31. Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further

    readings Ende Repository - Find by a eld 1 /∗∗ 2 ∗ @param s t r i n g $id 3 ∗ @return n u l l | TwitterEntry 4 ∗/ 5 p u b l i c f u n c t i o n fi ndBy Twit terI d ( $id ) 6 { 7 r e t u r n $this −>findOneBy ( arr ay ( ' t w i t t e r I d ' => $id ) ) ; 8 }
  32. Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further

    readings Ende Repository - Find with limit, skipping, sorting and one condition Find all entries that have not set eld 'deleted' or where this eld is 'false' Order by 'twitterId' DESC
  33. Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further

    readings Ende Repository - Find with limit, skipping, sorting and one condition Find all entries that have not set eld 'deleted' or where this eld is 'false' Order by 'twitterId' DESC 1 p u b l i c function findWithLimit ( $ l i m i t = 50 , $skip = 0) 2 { 3 $qb = $this −>createQueryBuilder () ; 4 /∗∗ @var Cursor $ r e s u l t ∗/ 5 $ r e s u l t = $qb−>f i e l d ( ' deleted ' )−>notEqual ( true ) 6 −>skip ( $skip ) 7 −>l i m i t ( $ l i m i t ) 8 −>s o r t ( ' t w i t t e r I d ' , −1) 9 −>getQuery () 10 −>execute () ; 11 return $result −>hydrate () ; 12 }
  34. Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further

    readings Ende Repository - Get last inserted entry 1 p u b l i c function getLastId () 2 { 3 $qb = $this −>createQueryBuilder () ; 4 /∗∗ @var Cursor $ r e s u l t ∗/ 5 $ r e s u l t = $qb−>s e l e c t ( ' t w i t t e r I d ' ) 6 −>l i m i t (1) 7 −>s o r t ( ' t w i t t e r I d ' , −1) 8 −>getQuery () 9 −>execute () ; 10 return $result −>getNext ()−>getTwitterId () ; 11 }
  35. Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further

    readings Ende Repository - Get all entries based on one eld value LIKE query:
  36. Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further

    readings Ende Repository - Get all entries based on one eld value LIKE query: 1 p u b l i c function findByUserName ( $userName , $limit , $skip ) 2 { 3 $qb = $this −>createQueryBuilder () ; 4 /∗∗ @var Cursor $ r e s u l t ∗/ 5 $ r e s u l t = $qb−>f i e l d ( ' from ' )−>equals ( $userName ) 6 −>skip ( $skip ) 7 −>l i m i t ( $ l i m i t ) 8 −>s o r t ( ' t w i t t e r I d ' , −1) 9 −>getQuery () 10 −>execute () ; 11 return $result −>hydrate () ;
  37. Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further

    readings Ende Further readings • Doctrine MongoDB reference http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/
  38. Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further

    readings Ende Further readings • Doctrine MongoDB reference http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/ • SQL to MongoDB Mapping Chart https://docs.mongodb.org/manual/reference/sql-comparison/
  39. Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further

    readings Ende Further readings • Doctrine MongoDB reference http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/ • SQL to MongoDB Mapping Chart https://docs.mongodb.org/manual/reference/sql-comparison/ • If you like: my very own example/documentation https://speakerdeck.com/dknx01/mongodb-in-symfony2-with-doctrine