SlideShare uma empresa Scribd logo
1 de 43
Baixar para ler offline
Doctrine NoSQL
Benjamin Eberlei (SimpleThings GmbH)
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
The Doctrine Project
www.doctrine-project.org

The Doctrine Project is the home of a selected
set of PHP libraries primarily focused on
providing persistence services and related
functionality.
Doctrine Subprojects
 DBAL and ORM
 Document Mapper (MongoDB, CouchDB, PHPCR)
 Annotations
 XML
 What is next?
Doctrine Philosophy
        Separate Persistence and Model
Doctrine Philosophy
            Similar look and feel
Doctrine Philosophy
            Embrace Differences
Why NoSQL Mapper?
Schemaless storage allows:

  Arbitrary associations
  Embedded objects
  Lists and Associative Arrays

No duplicate schema-maintenance!
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
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
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);
}
Persistence Interfaces
<?php
interface ObjectRepository
{
    function find($id);
    function findAll();
    function findBy(array $criteria,
        $orderBy = null,
        $limit = null,
        $offset = null
    )
    function findOneBy(array $criteria);
}
Sample Document
<?php
/** @Document */
class Message
{
    /** @Id */
    public $id;
    /** @Field(type="string") */
    public $text;
}

$message = new Message();
$message->setText("Hello World!");
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";
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();
Working with Objects 2
Find and update document:

<?php
/** @var $dm DocumentManager */
$message = $dm->find("Message", 1);
$message->setText("New Message");
$dm->flush();
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();
Persistence API Use-Cases
 Focus on "in memory" object workflows
 Specialized reusable Modules
 Symfony2:
   User Management
   Comment
   Admin Generators
   lichess.org
Associations in NoSQL
Pros
 Embedded Documents
 References between arbitrary types

Cons
 No referential integrity
 No support for transactions
Association Mappings
<?php
/** @Document */
class Blog
{
    /** @ReferenceMany */
    private $articles;
    /** @ReferenceOne(targetDocument="User") */
    private $owner;
}
Association keys
<?php
$id = "1";
$articleSlug = "hello-world";

$blog = $dm->find("Blog", $id);
$blog->articles[$articleSlug]->getHeadline();
Embedded Mappings
<?php
/** @Document */
class User
{
    /** @EmbedMany */
    private $phonenumbers;
    /** @EmbedOne(targetDocument="Address") */
    private $address;
}
CouchDB and Doctrine
 JSON Datastorage
 HTTP/REST API
 MVCC, eventually consistent (Conflicts)
 Replication
 Attachments
 Views and Map/Reduce in Javascript
 CouchDB Lucene
 Doctrine CouchDB 1.0 Alpha 1
JSON Document
{
    "_id": "716104ac33c797b12d50c0a6483f1661",
    "_rev": "1-32db404b78f130fd8f7575905859e19b",
    "doctrine_metadata":
    {
        "type": "MyProject.Document.Message",
        "associations":
        {
            "user": "055fe8a3ab06c3998d27b6d99f5a9bdd"
        }
    },
    "message": "I am a message"
}
Document Version
Implement Optimistic-Locking

<?php
class Article
{
    /** @Version */
    private $version;
}

$article = $dm->find(
    "Article", $id, $expectedVersion
);
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();
}
Attachments 2
<?php
use DoctrineCouchDBAttachment;

$article = $dm->find("Article", 1);
$data = $article->attachments["teaser.jpg"]->getContent();

$a = Attachment::createFromBase64data($data, "image/jpg");
$article->attachments["author.jpg"] = $a;

$dm->flush();
Views
Doctrine CouchDB maps filesystem to design document:

application/
    couchdb/
        views/
             username/
                 map.js
                 reduce.js

Use javascript syntax highlighting in your IDE/Editor.
Views 2
<?php
use DoctrineCouchDBViewFileFolderDesignDocument;

$path = "/path/application/couchdb";
$designDoc = new FileFolderDesignDocument($path);

/* @doc $couch CouchClient */
$docName = "myapp";
$couch->createDesignDoc($docName, $designDoc);
Query Views
<?php
/* @var $dm DocumentManager */
$query = $dm->createQuery("myapp", "username");
$result = $query->setStartKey("b")
                ->setEndKey("c")
                ->setLimit(10)
                ->setSkip(10)
                ->includeDocs(true)
                ->execute();

Using include docs creates PHP instances for you.
Lucene Queries
Support for the CouchDB Lucene extension:

<?php
$query = $dm->createLuceneQuery("lucenedoc", "users");
$result = $query->setQuery('"John Galt" OR "John Wayne"')
                ->setLimit(10)
                ->setSkip(10)
                ->includeDocs(true)
                ->execute();
MongoDB and Doctrine
 Indexing and on the fly
 queries
 Very fast
 In-Place Updates²
 GridFS, Geolocation
 Sharding
 Doctrine MongoDB 1.0 Beta2
Complex Associations
<?php
class User
{
    /**
      * @ReferenceMany(
      *    targetDocument="Comment",
      *    mappedBy="blogPost",
      *    sort={"date"="desc"},
      *    limit=5)
      */
    private $last5Comments;
}
Query API
<?php
$qb = $dm->createQueryBuilder('User')
      ->field('groups')
      ->all(array('Group 1', 'Group 2'))
      ->sort("username", "asc")
      ->limit(10)
      ->skip(10)
      ->execute();
Map/Reduce
<?php
$qb = $dm->createQueryBuilder('DocumentsUser')
    ->field('type')->equals('sale')
    ->map('function() { emit(this.user.$id, 1); }')
    ->reduce('function(k, vals) {
         var sum = 0;
         for (var i in vals) {
             sum += vals[i];
         }
         return sum;
    }');
Geospatial Queries
<?php
/** @Document @Index(keys={"coordinates"="2d"}) */
class City
{
    /** @EmbedOne(targetDocument="Coordinates") */
    public $coordinates;
    /** @Distance */
    public $distance;
}
class Coordinates
{
    public $lat;
    public $long;
}
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();
Eventual Migration
Handle simple and complex schema refactorings

<?php
/** @Document */
class Person
{
    public $id;

     public $name; // old

     /** @AlsoLoad("name") */
     public $fullName;
}
More of Doctrine MongoDB
 Support for Trees
 Support for Files in MongoGridFS
 Capped Collections
 Tailable Cursors
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
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);
Using Doctrine 2.0.x ORM?
Please checkout 2.1 BETA1
 Backwards compatible!
 You win a present if you can prove otherwise.
Thank you!
Rate this talk:

http://joind.in/talk/view/3515

Mais conteúdo relacionado

Mais procurados

Java Development with MongoDB
Java Development with MongoDBJava Development with MongoDB
Java Development with MongoDBScott Hernandez
 
Declarative Internal DSLs in Lua: A Game Changing Experience
Declarative Internal DSLs in Lua: A Game Changing ExperienceDeclarative Internal DSLs in Lua: A Game Changing Experience
Declarative Internal DSLs in Lua: A Game Changing ExperienceAlexander Gladysh
 
Mule esb – connecting to ms sql db
Mule esb – connecting to ms sql dbMule esb – connecting to ms sql db
Mule esb – connecting to ms sql dbGunjan Deshmukh
 
Doctrine 2
Doctrine 2Doctrine 2
Doctrine 2zfconfua
 
Entities in drupal 7
Entities in drupal 7Entities in drupal 7
Entities in drupal 7Zsolt Tasnadi
 
An introduction to CouchDB
An introduction to CouchDBAn introduction to CouchDB
An introduction to CouchDBDavid Coallier
 
MongoDB + Java - Everything you need to know
MongoDB + Java - Everything you need to know MongoDB + Java - Everything you need to know
MongoDB + Java - Everything you need to know Norberto Leite
 
Drupal 8: Forms
Drupal 8: FormsDrupal 8: Forms
Drupal 8: Formsdrubb
 
PHP Data Objects
PHP Data ObjectsPHP Data Objects
PHP Data ObjectsWez Furlong
 
Introduction to couch_db
Introduction to couch_dbIntroduction to couch_db
Introduction to couch_dbRomain Testard
 
PHP 5.3 and Lithium: the most rad php framework
PHP 5.3 and Lithium: the most rad php frameworkPHP 5.3 and Lithium: the most rad php framework
PHP 5.3 and Lithium: the most rad php frameworkG Woo
 
Implementing a many-to-many Relationship with Slick
Implementing a many-to-many Relationship with SlickImplementing a many-to-many Relationship with Slick
Implementing a many-to-many Relationship with SlickHermann Hueck
 

Mais procurados (18)

Drupal 8 migrate!
Drupal 8 migrate!Drupal 8 migrate!
Drupal 8 migrate!
 
Java Development with MongoDB
Java Development with MongoDBJava Development with MongoDB
Java Development with MongoDB
 
J query1
J query1J query1
J query1
 
Declarative Internal DSLs in Lua: A Game Changing Experience
Declarative Internal DSLs in Lua: A Game Changing ExperienceDeclarative Internal DSLs in Lua: A Game Changing Experience
Declarative Internal DSLs in Lua: A Game Changing Experience
 
Mule esb – connecting to ms sql db
Mule esb – connecting to ms sql dbMule esb – connecting to ms sql db
Mule esb – connecting to ms sql db
 
J query
J queryJ query
J query
 
Doctrine 2
Doctrine 2Doctrine 2
Doctrine 2
 
Entities in drupal 7
Entities in drupal 7Entities in drupal 7
Entities in drupal 7
 
An introduction to CouchDB
An introduction to CouchDBAn introduction to CouchDB
An introduction to CouchDB
 
MongoDB + Java - Everything you need to know
MongoDB + Java - Everything you need to know MongoDB + Java - Everything you need to know
MongoDB + Java - Everything you need to know
 
Drupal 8: Forms
Drupal 8: FormsDrupal 8: Forms
Drupal 8: Forms
 
Drupal Render API
Drupal Render APIDrupal Render API
Drupal Render API
 
PHP Data Objects
PHP Data ObjectsPHP Data Objects
PHP Data Objects
 
Build your own entity with Drupal
Build your own entity with DrupalBuild your own entity with Drupal
Build your own entity with Drupal
 
Introduction to couch_db
Introduction to couch_dbIntroduction to couch_db
Introduction to couch_db
 
Your Entity, Your Code
Your Entity, Your CodeYour Entity, Your Code
Your Entity, Your Code
 
PHP 5.3 and Lithium: the most rad php framework
PHP 5.3 and Lithium: the most rad php frameworkPHP 5.3 and Lithium: the most rad php framework
PHP 5.3 and Lithium: the most rad php framework
 
Implementing a many-to-many Relationship with Slick
Implementing a many-to-many Relationship with SlickImplementing a many-to-many Relationship with Slick
Implementing a many-to-many Relationship with Slick
 

Semelhante a Doctrine and NoSQL

Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_ToolZend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_ToolGordon Forsythe
 
Php on the desktop and php gtk2
Php on the desktop and php gtk2Php on the desktop and php gtk2
Php on the desktop and php gtk2Elizabeth Smith
 
Multilingualism makes better programmers
Multilingualism makes better programmersMultilingualism makes better programmers
Multilingualism makes better programmersAlexander Varwijk
 
What's new in the Drupal 7 API?
What's new in the Drupal 7 API?What's new in the Drupal 7 API?
What's new in the Drupal 7 API?Alexandru Badiu
 
Build powerfull and smart web applications with Symfony2
Build powerfull and smart web applications with Symfony2Build powerfull and smart web applications with Symfony2
Build powerfull and smart web applications with Symfony2Hugo Hamon
 
Symfony CMF - PHP Conference Brazil 2011
Symfony CMF - PHP Conference Brazil 2011Symfony CMF - PHP Conference Brazil 2011
Symfony CMF - PHP Conference Brazil 2011Jacopo Romei
 
CouchDB Mobile - From Couch to 5K in 1 Hour
CouchDB Mobile - From Couch to 5K in 1 HourCouchDB Mobile - From Couch to 5K in 1 Hour
CouchDB Mobile - From Couch to 5K in 1 HourPeter Friese
 
Laravel dokumentacja Restful API - swagger
Laravel dokumentacja Restful API - swaggerLaravel dokumentacja Restful API - swagger
Laravel dokumentacja Restful API - swaggerLaravel Poland MeetUp
 
Symfony2 - from the trenches
Symfony2 - from the trenchesSymfony2 - from the trenches
Symfony2 - from the trenchesLukas Smith
 
Dependency injection in Drupal 8
Dependency injection in Drupal 8Dependency injection in Drupal 8
Dependency injection in Drupal 8Alexei Gorobets
 
Drupal II: The SQL
Drupal II: The SQLDrupal II: The SQL
Drupal II: The SQLddiers
 
Your Entity, Your Code
Your Entity, Your CodeYour Entity, Your Code
Your Entity, Your CodeDrupalDay
 
Doctrine For Beginners
Doctrine For BeginnersDoctrine For Beginners
Doctrine For BeginnersJonathan Wage
 
Codeigniter : Two Step View - Concept Implementation
Codeigniter : Two Step View - Concept ImplementationCodeigniter : Two Step View - Concept Implementation
Codeigniter : Two Step View - Concept ImplementationAbdul Malik Ikhsan
 
Exploring Symfony's Code
Exploring Symfony's CodeExploring Symfony's Code
Exploring Symfony's CodeWildan Maulana
 

Semelhante a Doctrine and NoSQL (20)

Doctrine for NoSQL
Doctrine for NoSQLDoctrine for NoSQL
Doctrine for NoSQL
 
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_ToolZend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
 
Php on the desktop and php gtk2
Php on the desktop and php gtk2Php on the desktop and php gtk2
Php on the desktop and php gtk2
 
Multilingualism makes better programmers
Multilingualism makes better programmersMultilingualism makes better programmers
Multilingualism makes better programmers
 
Hexagonal architecture in PHP
Hexagonal architecture in PHPHexagonal architecture in PHP
Hexagonal architecture in PHP
 
Laravel 101
Laravel 101Laravel 101
Laravel 101
 
What's new in the Drupal 7 API?
What's new in the Drupal 7 API?What's new in the Drupal 7 API?
What's new in the Drupal 7 API?
 
Build powerfull and smart web applications with Symfony2
Build powerfull and smart web applications with Symfony2Build powerfull and smart web applications with Symfony2
Build powerfull and smart web applications with Symfony2
 
Symfony CMF - PHP Conference Brazil 2011
Symfony CMF - PHP Conference Brazil 2011Symfony CMF - PHP Conference Brazil 2011
Symfony CMF - PHP Conference Brazil 2011
 
CouchDB Mobile - From Couch to 5K in 1 Hour
CouchDB Mobile - From Couch to 5K in 1 HourCouchDB Mobile - From Couch to 5K in 1 Hour
CouchDB Mobile - From Couch to 5K in 1 Hour
 
Drupal 8 Hooks
Drupal 8 HooksDrupal 8 Hooks
Drupal 8 Hooks
 
Laravel dokumentacja Restful API - swagger
Laravel dokumentacja Restful API - swaggerLaravel dokumentacja Restful API - swagger
Laravel dokumentacja Restful API - swagger
 
Symfony2 - from the trenches
Symfony2 - from the trenchesSymfony2 - from the trenches
Symfony2 - from the trenches
 
Dependency injection in Drupal 8
Dependency injection in Drupal 8Dependency injection in Drupal 8
Dependency injection in Drupal 8
 
Drupal II: The SQL
Drupal II: The SQLDrupal II: The SQL
Drupal II: The SQL
 
Quebec pdo
Quebec pdoQuebec pdo
Quebec pdo
 
Your Entity, Your Code
Your Entity, Your CodeYour Entity, Your Code
Your Entity, Your Code
 
Doctrine For Beginners
Doctrine For BeginnersDoctrine For Beginners
Doctrine For Beginners
 
Codeigniter : Two Step View - Concept Implementation
Codeigniter : Two Step View - Concept ImplementationCodeigniter : Two Step View - Concept Implementation
Codeigniter : Two Step View - Concept Implementation
 
Exploring Symfony's Code
Exploring Symfony's CodeExploring Symfony's Code
Exploring Symfony's Code
 

Mais de Benjamin Eberlei

Just Married: Zend Framework and Doctrine
Just Married: Zend Framework and DoctrineJust Married: Zend Framework and Doctrine
Just Married: Zend Framework and DoctrineBenjamin Eberlei
 
Unittesting Bad-Practices by Example
Unittesting Bad-Practices by ExampleUnittesting Bad-Practices by Example
Unittesting Bad-Practices by ExampleBenjamin Eberlei
 
Towards the Cloud: Event-driven Architectures in PHP
Towards the Cloud: Event-driven Architectures in PHPTowards the Cloud: Event-driven Architectures in PHP
Towards the Cloud: Event-driven Architectures in PHPBenjamin Eberlei
 
Unit-Testing Bad-Practices by Example
Unit-Testing Bad-Practices by ExampleUnit-Testing Bad-Practices by Example
Unit-Testing Bad-Practices by ExampleBenjamin Eberlei
 
Framework-Qualität: Tests als Gütesiegel
Framework-Qualität: Tests als GütesiegelFramework-Qualität: Tests als Gütesiegel
Framework-Qualität: Tests als GütesiegelBenjamin Eberlei
 

Mais de Benjamin Eberlei (6)

Introduction to Tideways
Introduction to TidewaysIntroduction to Tideways
Introduction to Tideways
 
Just Married: Zend Framework and Doctrine
Just Married: Zend Framework and DoctrineJust Married: Zend Framework and Doctrine
Just Married: Zend Framework and Doctrine
 
Unittesting Bad-Practices by Example
Unittesting Bad-Practices by ExampleUnittesting Bad-Practices by Example
Unittesting Bad-Practices by Example
 
Towards the Cloud: Event-driven Architectures in PHP
Towards the Cloud: Event-driven Architectures in PHPTowards the Cloud: Event-driven Architectures in PHP
Towards the Cloud: Event-driven Architectures in PHP
 
Unit-Testing Bad-Practices by Example
Unit-Testing Bad-Practices by ExampleUnit-Testing Bad-Practices by Example
Unit-Testing Bad-Practices by Example
 
Framework-Qualität: Tests als Gütesiegel
Framework-Qualität: Tests als GütesiegelFramework-Qualität: Tests als Gütesiegel
Framework-Qualität: Tests als Gütesiegel
 

Último

Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 

Último (20)

Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 

Doctrine and NoSQL

  • 1. Doctrine NoSQL Benjamin Eberlei (SimpleThings GmbH)
  • 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
  • 3. The Doctrine Project www.doctrine-project.org The Doctrine Project is the home of a selected set of PHP libraries primarily focused on providing persistence services and related functionality.
  • 4. Doctrine Subprojects DBAL and ORM Document Mapper (MongoDB, CouchDB, PHPCR) Annotations XML What is next?
  • 5. Doctrine Philosophy Separate Persistence and Model
  • 6. Doctrine Philosophy Similar look and feel
  • 7. Doctrine Philosophy Embrace Differences
  • 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); }
  • 12. Persistence Interfaces <?php interface ObjectRepository { function find($id); function findAll(); function findBy(array $criteria, $orderBy = null, $limit = null, $offset = null ) function findOneBy(array $criteria); }
  • 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
  • 20. Association Mappings <?php /** @Document */ class Blog { /** @ReferenceMany */ private $articles; /** @ReferenceOne(targetDocument="User") */ private $owner; }
  • 21. Association keys <?php $id = "1"; $articleSlug = "hello-world"; $blog = $dm->find("Blog", $id); $blog->articles[$articleSlug]->getHeadline();
  • 22. Embedded Mappings <?php /** @Document */ class User { /** @EmbedMany */ private $phonenumbers; /** @EmbedOne(targetDocument="Address") */ private $address; }
  • 23. CouchDB and Doctrine JSON Datastorage HTTP/REST API MVCC, eventually consistent (Conflicts) Replication Attachments Views and Map/Reduce in Javascript CouchDB Lucene Doctrine CouchDB 1.0 Alpha 1
  • 24. JSON Document { "_id": "716104ac33c797b12d50c0a6483f1661", "_rev": "1-32db404b78f130fd8f7575905859e19b", "doctrine_metadata": { "type": "MyProject.Document.Message", "associations": { "user": "055fe8a3ab06c3998d27b6d99f5a9bdd" } }, "message": "I am a message" }
  • 25. Document Version Implement Optimistic-Locking <?php class Article { /** @Version */ private $version; } $article = $dm->find( "Article", $id, $expectedVersion );
  • 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(); }
  • 27. Attachments 2 <?php use DoctrineCouchDBAttachment; $article = $dm->find("Article", 1); $data = $article->attachments["teaser.jpg"]->getContent(); $a = Attachment::createFromBase64data($data, "image/jpg"); $article->attachments["author.jpg"] = $a; $dm->flush();
  • 28. Views Doctrine CouchDB maps filesystem to design document: application/ couchdb/ views/ username/ map.js reduce.js Use javascript syntax highlighting in your IDE/Editor.
  • 29. Views 2 <?php use DoctrineCouchDBViewFileFolderDesignDocument; $path = "/path/application/couchdb"; $designDoc = new FileFolderDesignDocument($path); /* @doc $couch CouchClient */ $docName = "myapp"; $couch->createDesignDoc($docName, $designDoc);
  • 30. Query Views <?php /* @var $dm DocumentManager */ $query = $dm->createQuery("myapp", "username"); $result = $query->setStartKey("b") ->setEndKey("c") ->setLimit(10) ->setSkip(10) ->includeDocs(true) ->execute(); Using include docs creates PHP instances for you.
  • 31. Lucene Queries Support for the CouchDB Lucene extension: <?php $query = $dm->createLuceneQuery("lucenedoc", "users"); $result = $query->setQuery('"John Galt" OR "John Wayne"') ->setLimit(10) ->setSkip(10) ->includeDocs(true) ->execute();
  • 32. MongoDB and Doctrine Indexing and on the fly queries Very fast In-Place Updates² GridFS, Geolocation Sharding Doctrine MongoDB 1.0 Beta2
  • 33. Complex Associations <?php class User { /** * @ReferenceMany( * targetDocument="Comment", * mappedBy="blogPost", * sort={"date"="desc"}, * limit=5) */ private $last5Comments; }
  • 34. Query API <?php $qb = $dm->createQueryBuilder('User') ->field('groups') ->all(array('Group 1', 'Group 2')) ->sort("username", "asc") ->limit(10) ->skip(10) ->execute();
  • 35. Map/Reduce <?php $qb = $dm->createQueryBuilder('DocumentsUser') ->field('type')->equals('sale') ->map('function() { emit(this.user.$id, 1); }') ->reduce('function(k, vals) { var sum = 0; for (var i in vals) { sum += vals[i]; } return sum; }');
  • 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.
  • 43. Thank you! Rate this talk: http://joind.in/talk/view/3515