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

Doctrine MongoDB ODM (PDXPHP)

on

  • 5,840 views

An introduction to MongoDB and the Doctrine MongoDB ODM.

An introduction to MongoDB and the Doctrine MongoDB ODM.

Statistics

Views

Total Views
5,840
Views on SlideShare
5,634
Embed Views
206

Actions

Likes
13
Downloads
75
Comments
1

7 Embeds 206

http://engineering.opensky.com 125
http://engineering.shopopensky.com 49
http://floeh.com 16
http://krautsock.posterous.com 6
http://www.thewebhatesme.com 4
http://floeh.symfony.local 3
http://feeds.feedburner.com 3
More...

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

11 of 1

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Doctrine MongoDB ODM (PDXPHP) Doctrine MongoDB ODM (PDXPHP) Presentation Transcript

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