Skip to content

brki/phpcr-odm

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHPCR ODM for Doctrine2

Current Status

  • (very) basic CRUD is implemented
  • metadata reading implemented for annotations
  • there is a symfony2 bundle available in symfony core.

Todo

  • fix the tests that fail
  • figure out how we can do relations in a sane way

Notes

  • The type of the document is stored in each repository node (stored as _doctrine_alias for the moment)

Getting Started

  1. Define one of those mapping drivers

    // Annotation driver
    $reader = new \Doctrine\Common\Annotations\AnnotationReader();
    $reader->setDefaultAnnotationNamespace('Doctrine\ODM\PHPCR\Mapping\\');
    // This line defines the jcr namespace, so you can annotate with @jcr:Property for example instead of just @Property
    $reader->setAnnotationNamespaceAlias('Doctrine\ODM\PHPCR\Mapping\\', 'jcr');
    $driver = new \Doctrine\ODM\PHPCR\Mapping\Driver\AnnotationDriver($reader, array('/path/to/your/document/classes'));
    
    // Xml driver
    $driver = new \Doctrine\ODM\PHPCR\Mapping\Driver\XmlDriver(array('/path/to/your/mapping/files'));
    
    // Yaml driver
    $driver = new \Doctrine\ODM\PHPCR\Mapping\Driver\YamlDriver(array('/path/to/your/mapping/files'));
    
  2. Initialize a Jackalope session

    $repository = new \Jackalope\Repository('http://localhost:8080/server');
    $credentials = new \PHPCR\SimpleCredentials('user', 'pass');
    $session = $repository->login($credentials, 'your_workspace');
    
  3. Initialize the DocumentManager

    $config = new \Doctrine\ODM\PHPCR\Configuration();
    $config->setMetadataDriverImpl($driver);
    $config->setPhpcrSession($session);
    
    $dm = new \Doctrine\ODM\PHPCR\DocumentManager($config);
    
  4. Example usage

    // fetching a document by path
    $user = $dm->getRepository('Namespace\For\Document\User')->find('/bob');
    
    // create a new document
    $newUser = new \Namespace\For\Document\User();
    $newUser->username = 'Timmy';
    $newUser->email = 'foo@example.com';
    $dm->persist($newUser, '/timmy');
    
    // store all changes, insertions, etc.
    $dm->flush();
    

Document Classes

You write your own document classes that will be mapped to and from the phpcr database by doctrine. The documents are usually simple

<?php
namespace Acme\SampleBundle\Document;
/**
 * @phpcr:Document(alias="mydocument")
 */
class MyDocument
{
    /**
     * @phpcr:String()
     */
    public $title;

    /**
     * @phpcr:String()
     */
    public $content;
}

Available annotations are

@Path: The phpcr path to this node.
@Node: The phpcr NodeInterface instance for direct access.
@Id: The unique id of this node. (only allowed if node is referenceable).
@Version: The version of this node, for versioned nodes.
@Property: Any property of the node, without specified type.
@Boolean,
@Int,
@Long,
@Float,
@String,
@Date,
@Binary,
@ArrayField:
Typed property

TODO: References and child / embed annotations.

About

Doctrine2 PHPCR ODM

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 100.0%