Created
February 15, 2012 20:36
-
-
Save chregu/1838831 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCRODM; | |
$vendorDir = __DIR__.'/lib/vendor'; | |
require_once $vendorDir.'/jackalope/lib/phpcr-utils/lib/vendor/Symfony/Component/ClassLoader/UniversalClassLoader.php'; | |
$classLoader = new \Symfony\Component\ClassLoader\UniversalClassLoader(); | |
$classLoader->register(); | |
$classLoader->registerNamespaces(array( | |
'Doctrine\ODM' => dirname($vendorDir), | |
'Doctrine\Common' => $vendorDir.'/doctrine-common/lib', | |
'Symfony' => array($vendorDir, $vendorDir.'/jackalope/lib/phpcr-utils/lib/vendor'), | |
'PHPCR' => array($vendorDir.'/jackalope/lib/phpcr/src', $vendorDir.'/jackalope/lib/phpcr-utils/src'), | |
)); | |
$classLoader->registerNamespaces(array( | |
'Jackalope'=> $vendorDir.'/jackalope/src', | |
)); | |
$extraCommands[] = new \Jackalope\Tools\Console\Command\JackrabbitCommand(); | |
$params = array( | |
'jackalope.jackrabbit_uri' => 'http://localhost:8081/server/', | |
); | |
$workspace = 'default'; | |
$user = 'admin'; | |
$pass = 'admin'; | |
/* bootstrapping the repository implementation. for jackalope, do this: */ | |
$repository = \Jackalope\RepositoryFactoryJackrabbit::getRepository($params); | |
$credentials = new \PHPCR\SimpleCredentials($user, $pass); | |
$session = $repository->login($credentials, $workspace); | |
/* prepare the doctrine configuration */ | |
$config = new \Doctrine\ODM\PHPCR\Configuration(); | |
$reader = new \Doctrine\Common\Annotations\AnnotationReader(); | |
$driver = new \Doctrine\ODM\PHPCR\Mapping\Driver\AnnotationDriver($reader, array('/opt/git/phpr-odm/')); | |
require("./lib/Doctrine/ODM/PHPCR/Mapping/Annotations/DoctrineAnnotations.php"); | |
$config->setMetadataDriverImpl($driver); | |
$config->setProxyDir('/tmp'); | |
$config->setProxyNamespace('PHPCRProxies'); | |
$config->setAutoGenerateProxyClasses(true); | |
$dm = \Doctrine\ODM\PHPCR\DocumentManager::create($session, $config); | |
$newUser = $dm->find('MyDocument','/timmy2'); | |
if (!$newUser) { | |
$newUser = new MyDocument(); | |
$newUser->title = 'Timmy'; | |
$newUser->content = 'foo@example.com'; | |
$newUser->path = '/timmy2'; | |
$newArticle = new Article(); | |
$newArticle->path = "/article6"; | |
$newArticle->title = 'Article 1'; | |
$newUser->addArticle($newArticle); | |
$newArticle = new Article(); | |
$newArticle->path = "/article8"; | |
$newArticle->title = 'Article 2'; | |
$newUser->addArticle($newArticle); | |
$dm->persist($newUser); | |
$dm->flush(); | |
} | |
/** | |
* @PHPCRODM\Document | |
*/ | |
class MyDocument | |
{ | |
/** | |
* @PHPCRODM\Id() | |
*/ | |
public $path; | |
/** | |
* @PHPCRODM\String() | |
*/ | |
public $title; | |
/** | |
* @PHPCRODM\String() | |
*/ | |
public $content; | |
/** | |
* articles | |
* @var array | |
* @PHPCRODM\ReferenceMany(targetDocument="Article") | |
*/ | |
private $articles; | |
public function addArticle(Article $article) | |
{ | |
$this->articles[] = $article; | |
} | |
} | |
/** | |
* @PHPCRODM\Document( referenceable=true) | |
*/ | |
class Article | |
{ | |
/** | |
* @PHPCRODM\Id() | |
*/ | |
public $path; | |
/** | |
* @PHPCRODM\String() | |
*/ | |
public $title; | |
/** | |
* @PHPCRODM\String() | |
*/ | |
public $content; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment