Doctrine MongoDB ODM (PDXPHP)

49
Doctrine MongoDB ODM Kris Wallsmith October 19, 2010

description

An introduction to MongoDB and the Doctrine MongoDB ODM.

Transcript of Doctrine MongoDB ODM (PDXPHP)

Page 1: Doctrine MongoDB ODM (PDXPHP)

Doctrine MongoDB ODMKris Wallsmith

October 19, 2010

Page 2: Doctrine MongoDB ODM (PDXPHP)

@kriswallsmith

• Symfony Release Manager

• Doctrine Team

• Senior Software Engineer at

• 10 years experience with PHP and web development

• Open source evangelist and international speaker

Page 3: Doctrine MongoDB ODM (PDXPHP)

This is MongoDB…

Page 4: Doctrine MongoDB ODM (PDXPHP)

$mongo = new Mongo();$db = $mongo->pdxphp;

$db->people->save(array( 'name' => 'Kris Wallsmith',));

Page 5: Doctrine MongoDB ODM (PDXPHP)

$cursor = $db->people->find();print_r(iterator_to_array($cursor));

Page 6: Doctrine MongoDB ODM (PDXPHP)

Array( [4cbdffdae84ded424f000000] => Array ( [_id] => MongoId Object [name] => Kris Wallsmith ))

Page 7: Doctrine MongoDB ODM (PDXPHP)

MongoDB is where youput your arrays for later.

Page 8: Doctrine MongoDB ODM (PDXPHP)

$db->people->save(array( 'name' => 'Sam Keen', 'roles' => array( 'organizer', 'presenter', ),));

Page 9: Doctrine MongoDB ODM (PDXPHP)

$query = array('roles' => 'presenter');

$cursor = $db->people->find($query);

print_r(iterator_to_array($cursor));

Page 10: Doctrine MongoDB ODM (PDXPHP)

Array( [4cbe03cfe84dedb850010000] => Array ( [_id] => MongoId Object [name] => Sam Keen [roles] => Array ( [0] => organizer [1] => presenter )

))

Page 11: Doctrine MongoDB ODM (PDXPHP)

Me too!

Page 12: Doctrine MongoDB ODM (PDXPHP)

$query = array( 'name' => 'Kris Wallsmith',);

$kris = $db->people->findOne($query);$kris['roles'] = array('presenter');

$db->people->save($kris);

Page 13: Doctrine MongoDB ODM (PDXPHP)

$query = array('roles' => 'presenter');$fields = array('name');

$cursor = $db->people->find($query, $fields);

print_r(iterator_to_array($cursor));

Page 14: Doctrine MongoDB ODM (PDXPHP)

Array( [4cbe0a9de84ded7952010000] => Array ( [_id] => MongoId Object [name] => Sam Keen ) [4cbe0a9de84ded7952000000] => Array ( [_id] => MongoId Object [name] => Kris Wallsmith ))

Page 15: Doctrine MongoDB ODM (PDXPHP)

Be surgical.

Page 16: Doctrine MongoDB ODM (PDXPHP)

$query = array('roles' => 'presenter');

$update = array( '$push' => array( 'roles' => 'cool guy', ),);

$db->people->update($query, $update);

Page 17: Doctrine MongoDB ODM (PDXPHP)

Atomic Operators

• $inc

• $set

• $unset

• $push

• $pushAll

• $addToSet

• $pop

• $pull

• $pullAll

• $rename

Page 18: Doctrine MongoDB ODM (PDXPHP)

Advanced Queries

Page 19: Doctrine MongoDB ODM (PDXPHP)

$roles = array('organizer', 'presenter');

$db->people->find(array( 'roles' => array('$all' => $roles),));

Page 20: Doctrine MongoDB ODM (PDXPHP)

Conditional Operators

• $ne

• $in

• $nin

• $mod

• $all

• $size

• $exists

• $type

• $or

• $elemMatch

Page 21: Doctrine MongoDB ODM (PDXPHP)

Cursor Methods

Page 22: Doctrine MongoDB ODM (PDXPHP)

$cursor = $db->people->find();$cursor->sort(array('name' => 1));

foreach ($cursor as $person) { // ...}

Page 23: Doctrine MongoDB ODM (PDXPHP)

I like you, Sam.

Page 24: Doctrine MongoDB ODM (PDXPHP)

$samRef = MongoDBRef::create('people', $samId);

$db->people->update( array('_id' => $kris['_id']), array( '$addToSet' => array( 'likes' => $samRef, ), ));

Page 25: Doctrine MongoDB ODM (PDXPHP)

$sam = $db->getDBRef($samRef);

Page 26: Doctrine MongoDB ODM (PDXPHP)

$db->people->find(array( 'likes.$id' => $kris['_id'],));

Page 27: Doctrine MongoDB ODM (PDXPHP)

Terminology

RDBMS MongoDB

Database Database

Table Collection

Row Document

Foreign Key Database Reference

Page 28: Doctrine MongoDB ODM (PDXPHP)

A document is an array.

Page 29: Doctrine MongoDB ODM (PDXPHP)

Arrays are nice.

Page 30: Doctrine MongoDB ODM (PDXPHP)

Objects are better.*

* Whenever objects are better.

Page 31: Doctrine MongoDB ODM (PDXPHP)

The Doctrine MongoDBObject Document Mapper

maps documentsto and from objects.

Page 32: Doctrine MongoDB ODM (PDXPHP)

We just need to tell it how.

Page 33: Doctrine MongoDB ODM (PDXPHP)

/** @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();}

Page 34: Doctrine MongoDB ODM (PDXPHP)

POPO FTW!

Page 35: Doctrine MongoDB ODM (PDXPHP)

$kris = new Person();$kris->name = 'Kris Wallsmith';$kris->roles[] = 'presenter';$kris->likes[] = $sam;$kris->addresses[] = $homeAddy;

$documentManager->persist($kris);$documentManager->flush();

Page 36: Doctrine MongoDB ODM (PDXPHP)

Wherefore art thou->save()

?

Page 37: Doctrine MongoDB ODM (PDXPHP)

Controller Document Manager

Document

Manager

Documents

Page 38: Doctrine MongoDB ODM (PDXPHP)
Page 39: Doctrine MongoDB ODM (PDXPHP)

ActiveRecord is more abstract.

Page 40: Doctrine MongoDB ODM (PDXPHP)

Doctrine calculates theoptimal query for you.

Page 41: Doctrine MongoDB ODM (PDXPHP)

$kris = $dm->findOne('Person', array( 'name' => 'Kris Wallsmith',));

$kris->roles[] = 'cool guy';

$dm->flush();

Page 42: Doctrine MongoDB ODM (PDXPHP)

$db->people->update(array( '_id' => $kris->id,), array( '$push' => array( 'roles' => 'cool guy', ),));

Page 43: Doctrine MongoDB ODM (PDXPHP)

Query API

Page 44: Doctrine MongoDB ODM (PDXPHP)

$query = $dm->createQuery('Person') ->field('name')->notEqual('Kris Wallsmith') ->field('roles')->equals('presenter') ->sort('name', 'asc');

$cursor = $query->execute();

Page 45: Doctrine MongoDB ODM (PDXPHP)

Lifecycle Callbacks

Page 46: Doctrine MongoDB ODM (PDXPHP)

/** @Document @HasLifecycleCallbacks */class Foo{ /** @Timestamp */ public $createdAt;

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

Page 47: Doctrine MongoDB ODM (PDXPHP)

OpenSky is Hiring!http://engineering.shopopensky.com

Please contact me if you're interested.

Page 48: Doctrine MongoDB ODM (PDXPHP)

OpenSky is Hiring!http://engineering.shopopensky.com

Please contact me if you're interested.

Page 49: Doctrine MongoDB ODM (PDXPHP)

mongodb.org

doctrine-project.org

symfony-reloaded.org