Upload
kris-wallsmith
View
5.266
Download
5
Tags:
Embed Size (px)
DESCRIPTION
An introduction to MongoDB and the Doctrine MongoDB ODM.
Citation preview
Doctrine MongoDB ODMKris 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 youput 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
• $set
• $unset
• $push
• $pushAll
• $addToSet
• $pop
• $pull
• $pullAll
• $rename
Advanced Queries
$roles = array('organizer', 'presenter');
$db->people->find(array( 'roles' => array('$all' => $roles),));
Conditional Operators
• $ne
• $in
• $nin
• $mod
• $all
• $size
• $exists
• $type
• $or
• $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 MongoDBObject Document Mapper
maps documentsto 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()
?
Controller Document Manager
Document
Manager
Documents
ActiveRecord is more abstract.
Doctrine calculates theoptimal 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