2. About me
Benjamin Eberlei
Working at SimpleThings GmbH
http://www.simplethings.de
Open Source contributor
Doctrine2, Symfony2
(Zeta Components, PHPUnit, ...)
Twitter @beberlei
Blog http://www.whitewashing.de
8. Why NoSQL Mapper?
Schemaless storage allows:
Arbitrary associations
Embedded objects
Lists and Associative Arrays
No duplicate schema-maintenance!
9. Doctrine NoSQL History
MongoDB Mapper early 2010 (OpenSky)
CouchDB Mapper started in October 2010 (Liip)
PHPCR ODM started in early 2011 (Liip)
APIs heavily inspired from ORM
10. SQL and NoSQL Similarities
Extracted common persistence interfaces
Covering roughly 10-20% of the use-cases
Simple Finder Methods
Insert/Update/Delete
Metadata API
Support for Annotations/XML/YAML/PHP Mapping
11. Persistence Interfaces
<?php
interface ObjectManager
{
function find($class, $id);
function getReference($class, $id);
function persist($object);
function remove($object);
function flush();
function getClassMetadata($class);
function getRepository($class);
}
13. Sample Document
<?php
/** @Document */
class Message
{
/** @Id */
public $id;
/** @Field(type="string") */
public $text;
}
$message = new Message();
$message->setText("Hello World!");
14. NoSQL benefits
<?php
/** @Document */
class Product
{
/** other fields */
/** @Field(type="array") */
public $attributes;
/** @Field(type="array") */
public $translations;
}
$product->attributes["isbn"] = "A-B-C-D";
$product->translations["de"]["name"] = "Ein Produkt";
15. Working with Objects 1
Creating a new document:
<?php
/** @var $dm DocumentManager */
$message = new Message();
$message->setText("I am new!");
$dm->persist($message);
$dm->flush();
echo "ID: " . $message->getId();
16. Working with Objects 2
Find and update document:
<?php
/** @var $dm DocumentManager */
$message = $dm->find("Message", 1);
$message->setText("New Message");
$dm->flush();
17. Working with Objects 3
Find and remove documents:
<?php
/** @var $dm DocumentManager */
$repository = $dm->getRepository("User");
$criteria = array("status" => "inactive");
$users = $repository->findBy($criteria);
foreach ($users AS $user) {
$dm->remove($user);
}
$dm->flush();
18. Persistence API Use-Cases
Focus on "in memory" object workflows
Specialized reusable Modules
Symfony2:
User Management
Comment
Admin Generators
lichess.org
19. Associations in NoSQL
Pros
Embedded Documents
References between arbitrary types
Cons
No referential integrity
No support for transactions
26. Attachments
CouchDB supports Attachments to documents
Doctrine converts into Attachment object
Lazy Load binary data from the server
Stream support planned
<?php
class Article
{
/** @Attachments */
public $attachments = array();
}
36. Geospatial Queries
<?php
/** @Document @Index(keys={"coordinates"="2d"}) */
class City
{
/** @EmbedOne(targetDocument="Coordinates") */
public $coordinates;
/** @Distance */
public $distance;
}
class Coordinates
{
public $lat;
public $long;
}
37. Geospatial Queries 2
Execute a Geospatial query and find locations near a point:
<?php
/* @var $dm DocumentManager */
$cities = $dm->createQuery('City')
->field('coordinates')->near(50, 60)
->execute();
38. Eventual Migration
Handle simple and complex schema refactorings
<?php
/** @Document */
class Person
{
public $id;
public $name; // old
/** @AlsoLoad("name") */
public $fullName;
}
39. More of Doctrine MongoDB
Support for Trees
Support for Files in MongoGridFS
Capped Collections
Tailable Cursors
40. PHPCR ODM
PHPCR: Port of the Java Content Repository API
Jackalope: Access to Apache Jackrabbit in PHP
Doctrine PHPCR ODM: PHP objects from PHP Content Repositories
41. Object to XML Mapper
Convert objects to XML documents and back using metadata
<?php
$user = new User();
$user->setFirstName('John');
$user->setLastName('Doe');
$user->setAddress(new Address('123 Street', 'New Haven'));
$user->addContact(new CustomerContact('no@way.com'));
$xml = $marshaller->marshalToString($user);
$user = $marshaller->unmarshalFromString($xml);
42. Using Doctrine 2.0.x ORM?
Please checkout 2.1 BETA1
Backwards compatible!
You win a present if you can prove otherwise.