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
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
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 ∗/
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 ;
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 ;
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 ;
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 ;
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 () ;
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
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
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 }
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 }
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
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 }
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 }
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 () ;
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