Your SlideShare is downloading. ×
Doctrine MongoDB ODM (PDXPHP)
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Doctrine MongoDB ODM (PDXPHP)

5,334
views

Published on

An introduction to MongoDB and the Doctrine MongoDB ODM.

An introduction to MongoDB and the Doctrine MongoDB ODM.

Published in: Technology

1 Comment
12 Likes
Statistics
Notes
No Downloads
Views
Total Views
5,334
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
83
Comments
1
Likes
12
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Doctrine MongoDB ODM Kris Wallsmith October 19, 2010
  • 2. @kriswallsmith • Symfony Release Manager • Doctrine Team • Senior Software Engineer at • 10 years experience with PHP and web development • Open source evangelist and international speaker
  • 3. This is MongoDB…
  • 4. $mongo = new Mongo(); $db = $mongo->pdxphp; $db->people->save(array( 'name' => 'Kris Wallsmith', ));
  • 5. $cursor = $db->people->find(); print_r(iterator_to_array($cursor));
  • 6. Array ( [4cbdffdae84ded424f000000] => Array ( [_id] => MongoId Object [name] => Kris Wallsmith ) )
  • 7. MongoDB is where you put your arrays for later.
  • 8. $db->people->save(array( 'name' => 'Sam Keen', 'roles' => array( 'organizer', 'presenter', ), ));
  • 9. $query = array('roles' => 'presenter'); $cursor = $db->people->find($query); print_r(iterator_to_array($cursor));
  • 10. Array ( [4cbe03cfe84dedb850010000] => Array ( [_id] => MongoId Object [name] => Sam Keen [roles] => Array ( [0] => organizer [1] => presenter ) ) )
  • 11. Me too!
  • 12. $query = array( 'name' => 'Kris Wallsmith', ); $kris = $db->people->findOne($query); $kris['roles'] = array('presenter'); $db->people->save($kris);
  • 13. $query = array('roles' => 'presenter'); $fields = array('name'); $cursor = $db->people->find($query, $fields); print_r(iterator_to_array($cursor));
  • 14. Array ( [4cbe0a9de84ded7952010000] => Array ( [_id] => MongoId Object [name] => Sam Keen ) [4cbe0a9de84ded7952000000] => Array ( [_id] => MongoId Object [name] => Kris Wallsmith ) )
  • 15. Be surgical.
  • 16. $query = array('roles' => 'presenter'); $update = array( '$push' => array( 'roles' => 'cool guy', ), ); $db->people->update($query, $update);
  • 17. Atomic Operators • $inc • $addToSet • $set • $pop • $unset • $pull • $push • $pullAll • $pushAll • $rename
  • 18. Advanced Queries
  • 19. $roles = array('organizer', 'presenter'); $db->people->find(array( 'roles' => array('$all' => $roles), ));
  • 20. Conditional Operators • $ne • $size • $in • $exists • $nin • $type • $mod • $or • $all • $elemMatch
  • 21. Cursor Methods
  • 22. $cursor = $db->people->find(); $cursor->sort(array('name' => 1)); foreach ($cursor as $person) { // ... }
  • 23. I like you, Sam.
  • 24. $samRef = MongoDBRef::create('people', $samId); $db->people->update( array('_id' => $kris['_id']), array( '$addToSet' => array( 'likes' => $samRef, ), ) );
  • 25. $sam = $db->getDBRef($samRef);
  • 26. $db->people->find(array( 'likes.$id' => $kris['_id'], ));
  • 27. Terminology RDBMS MongoDB Database Database Table Collection Row Document Foreign Key Database Reference
  • 28. A document is an array.
  • 29. Arrays are nice.
  • 30. Objects are better.* * Whenever objects are better.
  • 31. The Doctrine MongoDB Object Document Mapper maps documents to and from objects.
  • 32. We just need to tell it how.
  • 33. /** @Document(collection="people") */ class Person { /** @Id */ public $id; /** @String */ public $name; /** @Collection */ public $roles = array(); /** @ReferenceMany */ public $likes = array(); /** @EmbedMany(targetDocument="Address") */ public $addresses = array(); }
  • 34. POPO FTW!
  • 35. $kris = new Person(); $kris->name = 'Kris Wallsmith'; $kris->roles[] = 'presenter'; $kris->likes[] = $sam; $kris->addresses[] = $homeAddy; $documentManager->persist($kris); $documentManager->flush();
  • 36. Wherefore art thou ->save() ?
  • 37. Documents Controller Document Manager
  • 38. ActiveRecord is more abstract.
  • 39. Doctrine calculates the optimal query for you.
  • 40. $kris = $dm->findOne('Person', array( 'name' => 'Kris Wallsmith', )); $kris->roles[] = 'cool guy'; $dm->flush();
  • 41. $db->people->update(array( '_id' => $kris->id, ), array( '$push' => array( 'roles' => 'cool guy', ), ));
  • 42. Query API
  • 43. $query = $dm->createQuery('Person') ->field('name')->notEqual('Kris Wallsmith') ->field('roles')->equals('presenter') ->sort('name', 'asc'); $cursor = $query->execute();
  • 44. Lifecycle Callbacks
  • 45. /** @Document @HasLifecycleCallbacks */ class Foo { /** @Timestamp */ public $createdAt; /** @PrePersist */ public function ensureCreatedAt() { $this->createdAt = new DateTime(); } }
  • 46. OpenSky is Hiring! http://engineering.shopopensky.com Please contact me if you're interested.
  • 47. OpenSky is Hiring! http://engineering.shopopensky.com Please contact me if you're interested.
  • 48. mongodb.org doctrine-project.org symfony-reloaded.org