Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further readings Ende Requirements

Slide 4

Slide 4 text

Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further readings Ende Requirements • PHP: >=5.3.9

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further readings Ende Conguration

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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 ∗/

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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 ∗/

Slide 16

Slide 16 text

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).

Slide 17

Slide 17 text

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 ;

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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 ;

Slide 20

Slide 20 text

Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further readings Ende Property annotations - Strings and Integer elds

Slide 21

Slide 21 text

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 ;

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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 ;

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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 ;

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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 ;

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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 () ;

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further readings Ende Repository - Saving a document

Slide 36

Slide 36 text

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 }

Slide 37

Slide 37 text

Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further readings Ende Repository - Find by a eld

Slide 38

Slide 38 text

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 }

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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 }

Slide 41

Slide 41 text

Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further readings Ende Repository - Get last inserted entry

Slide 42

Slide 42 text

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 }

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

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 () ;

Slide 45

Slide 45 text

Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further readings Ende Further readings

Slide 46

Slide 46 text

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/

Slide 47

Slide 47 text

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/

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

Erik Witthauer Requirements Conguration Documents SubDocuments Property annotations Repository Further readings Ende End Thank you