symfony day 2010 doctrine mongodb odm

73
www.doctrine-project.org www.sensiolabs.org Doctrine MongoDB Object Document Manager #sfdaycgn Doctrine MongoDB Object Document Mapper (ODM)

Upload: jonathan-wage

Post on 12-Jan-2015

9.157 views

Category:

Technology


2 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

DoctrineMongoDB Object Document Mapper (ODM)

Page 2: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

What is Doctrine?

Page 3: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

• Open source PHP project started in 2006

• Relational database abstraction layer.• mysql, oracle, pgsql, sqlite, etc.

• Object persistence layers for RDBMS, MongoDB, etc.

• Other database related functionalities.

What is Doctrine?

http://www.doctrine-project.org

Page 4: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

What is MongoDB?

• Key-Value document based storage system.

• Bridge between traditional relational databases and key-value data stores.

http://www.mongodb.org

Page 5: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

Terminology

RDBMS MongoDBDatabase Database

Table Collection

Row Document

Page 6: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

Using MongoDB in PHP

Page 7: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

Connecting

$mongo = new Mongo('mongodb://localhost');

Page 8: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

$mongo = new Mongo('mongodb://localhost');$db = $mongo->selectDB('dbname');

Selecting Databases

Page 9: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

Selecting Collections

$mongo = new Mongo('mongodb://localhost');$db = $mongo->selectDB('dbname');$coll = $db->selectCollection('users');

Page 10: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

Inserting Documents

$user = array( 'username' => 'jwage', 'password' => md5('changeme') 'active' => true);$coll->insert($user);

echo $user['_id'];

Insert a new user document and echo the newly generated id

Page 11: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

Updating Documents

$user = $coll->findOne(array('username' => 'jwage'));$user['username'] = 'jonwage';$user['password'] = md5('newpassword');$coll->save($user);

Update username and password and save the whole document

Page 12: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

Atomic Updates

• Faster than saving the entire document.

• Safer than updating the entire document.

• Updates are done in place and are atomic.

Page 13: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

Atomic Updates

$coll->update(array( 'username' => 'jwage'), array( '$set' => array( 'username' => 'jonwage', 'password' => md5('newpassword') )));

Update username and password where username is jwage

Page 14: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

Atomic Updates

• $inc - increments field by the number value• $set - sets field to value• $unset - deletes a given field• $push - appends value to an array• $pushAll - appends multiple values to an array• $addToSet - appends multiple values to an array that don’t already exist in the

array• $pop - removes the last element in an array• $pull - removes all occurrences of a value from an array• $pullAll - removes all occurrences of multiple values from an array

Many other atomic operators exist for manipulating a documents data

Page 15: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

Removing Documents

$coll->remove(array('username' => 'jwage'));

Page 16: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

Doctrine + MongoDB

Page 17: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

DocumentManager

• Abstraction on top of the PHP Mongo class

• Manages the persistent state of PHP objects

• Implements UnitOfWork for change tracking

http://martinfowler.com/eaaCatalog/unitOfWork.html

Page 18: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

• A NEW document instance has no persistent identity, and is not yet associated with a DocumentManager (i.e. those just created with the "new" operator).

• A MANAGED document instance is an instance with a persistent identity that is associated with an DocumentManager and whose persistence is thus managed.

• A DETACHED document instance is an instance with a persistent identity that is not (or no longer) associated with an DocumentManager.

• A REMOVED document instance is an instance with a persistent identity, associated with an DocumentManager, that will be removed from the database upon transaction commit.

Document States

Page 19: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

$config = new \Doctrine\ODM\MongoDB\Configuration();$config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);$driverImpl = $config->newDefaultAnnotationDriver(array(__DIR__."/Documents"));$config->setMetadataDriverImpl($driverImpl);

$config->setProxyDir(__DIR__ . '/Proxies');$config->setProxyNamespace('Proxies');

$dm = \Doctrine\ORM\DocumentManager::create($mongo, $config);

DocumentManagerCreate your DocumentManager instance for managing object persistence

Wraps around existing $mongo connection

Page 20: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

Defining Documentclass User{ private $id; private $username; private $password;

public function getId() { return $this->id; }

public function getUsername() { return $this->username; }

public function setUsername($username) { $this->username = $username; }

public function getPassword() { return $this->password; }

public function setPassword($password) { $this->password = md5($password); }}

A document is just a regular ole’ PHP class

Page 21: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

Mapping the Document

/** @Document */class User{ /** @Id */ private $id; /** @String */ private $username; /** @String */ private $password; // ...}

Page 22: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

Ready to GoDoctrine now knows about this document and is able

to manage its persistent state

Page 23: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

Inserting, Updating and Deleting Documents

Page 24: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

Inserting Documents$user = new User();$user->setUsername('jwage');$user->setPassword('changeme');

$dm->persist($user);$dm->flush(); // inserts document

$users = array( array( 'username' => 'jwage', 'password' => 'changeme' ));$coll->batchInsert($users);

Page 25: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

Updating Documents$user = $dm->findOne('User', array('username' => 'jwage'));

$user->setUsername('jonwage');$user->setPassword('newpassword');

$dm->flush(); // updates document

$coll->update( array('_id' => 'theid'), array('$set' => array( 'username' => 'jonwage', 'password' => '5e9d11a14ad1c8dd77e98ef9b53fd1ba' ));

Page 26: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

Removing Documents

$user = $dm->findOne('User', array('username' => 'jwage'));

$dm->remove($user);$dm->flush(); // removes document

$coll->remove(array('_id' => 'theid'));

Page 27: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

Query APIFluent OO Query API

Page 28: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

Querying MongoDB Directly

// Get user array directly from mongo$user = $mongo->db->users->findOne(array('username' => 'jwage'));

Returns an array of information and not a User object

Page 29: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

// Get user class instance through Doctrine$user = $dm->findOne('User', array('username' => 'jwage'));

Querying Through Doctrine

Going through Doctrine gives you managed objects back instead of arrays

Page 30: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

// Get user class instance through Doctrine Query API$user = $dm->createQuery('User') ->field('username')->equals('jwage') ->getSingleResult();

Querying Through Doctrine Query API

Query for same user using the Query API

Page 31: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

// Find posts within a range of dates$posts = $dm->createQuery('Post') ->field('createdAt')->range($startDate, $endDate) ->execute();

Querying Through Doctrine Query API

Page 32: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

Updating Documents

// Update a document$dm->createQuery('User') ->update() ->field('username')->set('jonwage') ->field('password')->set('newpassword') ->field('username')->equals('jwage') ->execute();

Page 33: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

// Remove a document$dm->createQuery('User') ->remove() ->field('username')->equals('jwage') ->execute();

// Remove a document$dm->createQuery('User') ->remove() ->field('username')->equals('jwage') ->execute();

Removing Documents

Page 34: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

Embedded Documents

Page 35: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

class User{ /** @Id */ public $id;

/** @String */ public $name;

/** @EmbedMany(targetDocument="Address") */ public $addresses = array();}

Page 36: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

/** @EmbeddedDocument */class Address{ /** @String */ public $address;

/** @String */ public $city;

/** @String */ public $state;

/** @String */ public $zipcode;}

Page 37: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

$user = new User();$user->name = 'Jonathan H. Wage';

$address = new Address();$address->address = '6512 Mercomatic Ct';$address->city = 'Nashville';$address->state = 'Tennessee';$address->zipcode = '37209';$user->addresses[] = $address;

$dm->persist($user);$dm->flush();

Page 38: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

$users = array( array( 'name' => 'Jonathan H. Wage', 'addresses' => array( array( 'address' => '6512 Mercomatic Ct', 'city' => 'Nashville', 'state' => 'Tennesseee', 'zipcode' => '37209' ) ) ));$coll->batchInsert($users);

Page 39: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

Updating Embedded Documents

Uses dot notation and atomic operators for updating

Page 40: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

$user = $dm->findOne('User', array('name' => 'Jonathan H. Wage'));$user->addresses[0]->zipcode = '37205';$dm->flush();

$coll->update( array('_id' => 'theuserid'), array('$set' => array('addresses.0.zipcode' => '37209')));

Page 41: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

Add New Address

Page 42: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

$user = $dm->findOne('User', array('name' => 'Jonathan H. Wage'));

$address = new Address();$address->address = '475 Buckhead Ave.';$address->city = 'Atlanta';$address->state = 'Georgia';$address->zipcode = '30305';$user->addresses[] = $address;

$dm->flush();

$coll->update( array('_id' => 'theuserid'), array('$pushAll' => array( 'addresses' => array( array( 'address' => '475 Buckhead Ave.', 'city' => 'Atlanta', 'state' => 'Georgia', 'zipcode' => '30305' ) ) )));

Page 43: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

unset($user->addresses[0]->zipcode);$dm->flush();

$coll->update( array('_id' => 'theuserid'), array( '$unset' => array( 'addresses.0.zipcode' => 1 ) ));

Page 44: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

ReferencesMongoDB does not have joins or foreign keys but you

can still store references to other documents and resolve the references in your application code.

Doctrine abstracts the handling of referneces for you so working with them is seamless.

Page 45: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

/** @Document */class Organization{ /** @Id */ private $id;

/** @String */ private $name;

/** @ReferenceMany(targetDocument="User") */ private $users = array();

public function setName($name) { $this->name = $name; }

public function addUser(User $user) { $this->users[] = $user; }}

Organization

Page 46: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

/** @Document */class User{ /** @Id */ private $id;

/** @String */ private $name;

/** @ReferenceOne(targetDocument="Organization") */ private $organization;

public function setName($name) { $this->name = $name; }

public function setOrganization(Organization $organization) { $this->organization = $organization; $organization->addUser($this); }}

User

Page 47: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

$organization = new Organization();$organization->setName('Sensio Labs');

$user = new User();$user->setName('Jonathan H. Wage');$user->setOrganization($organization);

$dm->persist($organization);$dm->persist($user);$dm->flush();

Page 48: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

The Result

Page 49: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

Array( [_id] => 4c86acd78ead0e8759000000 [name] => Sensio Labs [users] => Array ( [0] => Array ( [$ref] => User [$id] => 4c86acd78ead0e8759010000 [$db] => doctrine_odm_sandbox )

)

)

Array( [_id] => 4c86acd78ead0e8759010000 [name] => Jonathan H. Wage [organization] => Array ( [$ref] => Organization [$id] => 4c86acd78ead0e8759000000 [$db] => doctrine_odm_sandbox )

)

Page 50: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

Working with References

Page 51: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

$user = $dm->find('User', array('name' => 'Jonathan H. Wage'));

// instance of uninitialized OrganizationProxy$organization = $user->getOrganization();

// calling getter or setter for uninitialized proxy// queries the database and initialized the proxy document

// query invoked, organization data loaded and doc initializedecho $organization->getName();

Page 52: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

$organization = $dm->find('Organization', array('name' => 'Sensio Labs'));$users = $organization->getUsers(); // uninitialized collection

// Queries database for users and initializes collectionforeach ($users as $user){ // ...}

Page 53: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

Change Tracking

Page 54: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

UnitOfWork• Tracks changes in objects between flushes

• Maintains copy of old values to compare new values to

• Changesets computed and persisted in the most efficient way using atomic operators $inc, $set, $unset, $pullAll, $pushAll, etc.

Page 55: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

Other Features

Page 56: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

Document Query Language

Language for querying documents in a similar way to SQL

Page 57: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

BNF Grammar

Page 58: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

QueryLanguage ::= FindQuery | InsertQuery | UpdateQuery | RemoveQuery

FindQuery ::= FindClause [WhereClause] [MapClause] [ReduceClause] [SortClause] [LimitClause] [SkipClause]FindClause ::= "FIND" all | SelectField {"," SelectField}SelectField ::= DocumentFieldNameSortClause ::= SortClauseField {"," SortClauseField}SortClauseField ::= DocumentFieldName "ASC | DESC"LimitClause ::= "LIMIT" LimitIntegerSkipClause ::= "SKIP" SkipIntegerMapClause ::= "MAP" MapFunctionReduceClause ::= "REDUCE" ReduceFunction

DocumentFieldName ::= DocumentFieldName | EmbeddedDocument "." {"." DocumentFieldName}WhereClause ::= "WHERE" WhereClausePart {"AND" WhereClausePart}WhereClausePart ::= ["all", "not"] DocumentFieldName WhereClauseExpression ValueWhereClauseExpression ::= "=" | "!=" | ">=" | "<=" | ">" | "<" | "in" "notIn" | "all" | "size" | "exists" | "type"Value ::= LiteralValue | JsonObject | JsonArray

UpdateQuery ::= UpdateClause [WhereClause]UpdateClause ::= [SetExpression], [UnsetExpression], [IncrementExpression], [PushExpression], [PushAllExpression], [PullExpression], [PullAllExpression], [AddToSetExpression], [AddManyToSetExpression], [PopFirstExpression], [PopLastExpression]SetExpression ::= "SET" DocumentFieldName "=" Value {"," SetExpression}UnsetExpression ::= "UNSET" DocumentFieldName {"," UnsetExpression}IncrementExpression ::= "INC" DocumentFieldName "=" IncrementInteger {"," IncrementExpression}PushExpression ::= "PUSH" DocumentFieldName Value {"," PushExpression}PushAllExpression ::= "PUSHALL" DocumentFieldName Value {"," PushAllExpression}PullExpression ::= "PULL" DocumentFieldName Value {"," PullExpression}PullAllExpression ::= "PULLALL" DocumentFieldName Value {"," PullAllExpression}AddToSetExpression ::= "ADDTOSET" DocumentFieldName Value {"," AddToSetExpression}AddManyToSetExpression ::= "ADDMANYTOSET" DocumentFieldName Value {"," AddManyToSetExpression}PopFirstExpression ::= "POPFIRST" DocumentFieldName {"," PopFirstExpression}PopLastExpression ::= "POPLAST" DocumentFieldName {"," PopLastExpression}

InsertQuery ::= InsertClause InsertSetClause {"," InsertSetClause}InsertSetClause ::= DocumentFieldName "=" Value

RemoveQuery ::= RemoveClause [WhereClause]

Page 59: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

Creating Query

$users = $dm->query('find all FROM User');

Page 60: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

Selecting Fields

$users = $dm->query('find username FROM User');

Page 61: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

Paging Results

$users = $dm->query('find all FROM User limit 30 skip 30');

Page 62: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

Selecting Slice of Embedded Documents

$post = $dm->query('find title, comments limit 20 skip 10 FROM BlogPost WHERE id = ?', array($id));

Page 63: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

Other Examples

// Find all posts greater than or equal to a date$posts = $dm->query('find all FROM BlogPost WHERE createdAt >= ?', array($date));

// Find all posts sorted by created at desc$posts = $dm->query('find all FROM BlogPost sort createdAt desc');

// Update user$dm->query('update Documents\User set password = ? where username = ?', array('newpassword', 'jwage'));

Page 64: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

Lifecycle Events

Page 65: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

http://www.doctrine-project.org/projects/mongodb_odm/1.0/docs/reference/events/en#lifecycle-callbacks

“A lifecycle event is a regular event with the additional feature of providing a mechanism to register direct

callbacks inside the corresponding document classes that are executed when the lifecycle event occurs.”

Page 66: Symfony Day 2010 Doctrine MongoDB ODM

• preRemove - The preRemove event occurs for a given document before the respective DocumentManager remove operation for that document is executed.

• postRemove - The postRemove event occurs for an document after the document has been removed. It will be invoked after the database delete operations.

• prePersist - The prePersist event occurs for a given document before the respective DocumentManager persist operation for that document is executed.

• postPersist - The postPersist event occurs for an document after the document has been made persistent. It will be invoked after the database insert operations. Generated primary key values are available in the postPersist event.

• preUpdate - The preUpdate event occurs before the database update operations to document data.

• postUpdate - The postUpdate event occurs after the database update operations to document data.

• postLoad - The postLoad event occurs for an document after the document has been loaded into the current DocumentManager from the database or after the refresh operation has been applied to it.

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

Page 67: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

Event Examples

Page 68: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

/** * @Document * @HasLifecycleCallbacks */class BlogPost{ /** @Id */ public $id; /** @Date */ public $createdAt; /** @Date */ public $updatedAt;

/** @PreUpdate */ public function prePersist() { $this->createdAt = new DateTime(); }

/** @PreUpdate */ public function preUpdate() { $this->updatedAt = new DateTime(); }}

Page 69: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

Migrating Data

Page 70: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

/** * @Document */class User{ /** @Id */ public $id; /** @String */ public $name;}

Initial Document

Page 71: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

/** * @Document */class User{ /** @Id */ public $id; /** @String */ public $firstName; /** @String */ public $lastName;}

New Document

Page 72: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

/** * @Document * @HasLifecycleCallbacks */class User{ /** @Id */ public $id; /** @String */ public $firstName; /** @String */ public $lastName;

/** @PreLoad */ public function preLoad(array &$data) { if (isset($data['name'])) { $e = explode(' ', $data['name']); unset($data['name']); $data['firstName'] = $e[0]; $data['lastName'] = $e[1]; } }

Migrate Documents With Old Field Name

Page 73: Symfony Day 2010 Doctrine MongoDB ODM

www.doctrine-project.org www.sensiolabs.org

Doctrine MongoDB Object Document Manager #sfdaycgn

You can contact Jonathan about Doctrine and Open-Source or for training, consulting, application development, or business related

questions at [email protected]

Jonathan H. Wage

• http://www.twitter.com/jwage

• http://www.jwage.com

• http://www.facebook.com/jwage

Questions?