0
Doctrine MongoDB ODM
        Kris Wallsmith




         October 19, 2010
@kriswallsmith

•   Symfony Release Manager

•   Doctrine Team

•   Senior Software Engineer at

•   10 years experience w...
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 W...
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 Ke...
Me too!
$query = array(
    'name' => 'Kris Wallsmith',
);

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

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

print_r(i...
Array
(
    [4cbe0a9de84ded7952010000] => Array
        (
            [_id] => MongoId Object
            [name] => Sam Ke...
Be surgical.
$query = array('roles' => 'presenter');

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

$d...
Atomic Operators

• $inc                • $addToSet
• $set                • $pop
• $unset              • $pull
• $push    ...
Advanced Queries
$roles = array('organizer', 'presenter');

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

• $ne               • $size
• $in               • $exists
• $nin              • $type
• $mod       ...
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(
       ...
$sam = $db->getDBRef($samRef);
$db->people->find(array(
    'likes.$id' => $kris['_id'],
));
Terminology
 RDBMS           MongoDB

 Database         Database

   Table          Collection

   Row            Document...
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;
  /** @Co...
POPO FTW!
$kris = new Person();
$kris->name = 'Kris Wallsmith';
$kris->roles[] = 'presenter';
$kris->likes[] = $sam;
$kris->addresse...
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')
...
Lifecycle Callbacks
/** @Document @HasLifecycleCallbacks */
class Foo
{
  /** @Timestamp */
  public $createdAt;

    /** @PrePersist */
    p...
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
Doctrine MongoDB ODM (PDXPHP)
Upcoming SlideShare
Loading in...5
×

Doctrine MongoDB ODM (PDXPHP)

5,418

Published on

An introduction to MongoDB and the Doctrine MongoDB ODM.

Published in: Technology
1 Comment
12 Likes
Statistics
Notes
No Downloads
Views
Total Views
5,418
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
84
Comments
1
Likes
12
Embeds 0
No embeds

No notes for slide

Transcript of "Doctrine MongoDB ODM (PDXPHP)"

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

    Clipping is a handy way to collect important slides you want to go back to later.

×