Created
July 27, 2010 16:59
-
-
Save jwage/492509 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
$path = '/path/to/doctrine/mongodb-odm'; | |
require_once $path.'/lib/vendor/doctrine-common/lib/Doctrine/Common/ClassLoader.php'; | |
use Doctrine\Common\ClassLoader, | |
Doctrine\Common\Annotations\AnnotationReader, | |
Doctrine\ODM\MongoDB\DocumentManager, | |
Doctrine\ODM\MongoDB\Configuration, | |
Doctrine\ODM\MongoDB\Mongo, | |
Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver; | |
$classLoader = new ClassLoader('Doctrine\ODM', $path.'/lib'); | |
$classLoader->register(); | |
$classLoader = new ClassLoader('Doctrine', $path.'/lib/vendor/doctrine-common/lib'); | |
$classLoader->register(); | |
$classLoader = new ClassLoader('Symfony\Components\Yaml', $path.'/lib/vendor'); | |
$classLoader->register(); | |
$config = new Configuration(); | |
$config->setProxyDir(__DIR__); | |
$config->setProxyNamespace('Proxies'); | |
$config->setDefaultDB('doctrine_odm_tests'); | |
$reader = new AnnotationReader(); | |
$reader->setDefaultAnnotationNamespace('Doctrine\ODM\MongoDB\Mapping\\'); | |
$config->setMetadataDriverImpl(new AnnotationDriver($reader, __DIR__ . '/Documents')); | |
$dm = DocumentManager::create(new Mongo(), $config); | |
/** @Document(collection="users") */ | |
class User | |
{ | |
/** @Id */ | |
private $id; | |
/** @String */ | |
private $username; | |
/** @EmbedMany(targetDocument="Profile") */ | |
private $profiles = array(); | |
public function setUsername($username) | |
{ | |
$this->username = $username; | |
} | |
public function addProfile(Profile $profile) | |
{ | |
$this->profiles[] = $profile; | |
} | |
} | |
/** @EmbeddedDocument */ | |
class Profile | |
{ | |
/** @String */ | |
private $name; | |
/** @EmbedMany(targetDocument="Address") */ | |
private $addresses = array(); | |
public function setName($name) | |
{ | |
$this->name = $name; | |
} | |
public function addAddress(Address $address) | |
{ | |
$this->addresses[] = $address; | |
} | |
} | |
/** @EmbeddedDocument */ | |
class Address | |
{ | |
/** @String */ | |
private $number; | |
/** @String */ | |
private $street; | |
/** @String */ | |
private $city; | |
/** @String */ | |
private $state; | |
/** @String */ | |
private $zipcode; | |
public function setNumber($number) | |
{ | |
$this->number = $number; | |
} | |
public function setStreet($street) | |
{ | |
$this->street = $street; | |
} | |
public function setCity($city) | |
{ | |
$this->city = $city; | |
} | |
public function setState($state) | |
{ | |
$this->state = $state; | |
} | |
public function setZipcode($zipcode) | |
{ | |
$this->zipcode = $zipcode; | |
} | |
} | |
$user = new User(); | |
$user->setUsername('jwage'); | |
$profile = new Profile(); | |
$profile->setName('Profile #1'); | |
$user->addProfile($profile); | |
$address = new Address(); | |
$address->setNumber('6512'); | |
$address->setStreet('Mercomatic'); | |
$address->setCity('Nashville'); | |
$address->setState('Tennessee'); | |
$address->setZipcode('37209'); | |
$profile->addAddress($address); | |
$profile = new Profile(); | |
$profile->setName('Profile #2'); | |
$user->addProfile($profile); | |
$address = new Address(); | |
$address->setNumber('475'); | |
$address->setStreet('Buckhead Ave'); | |
$address->setCity('Atlanta'); | |
$address->setState('Georgia'); | |
$address->setZipcode('30303'); | |
$profile->addAddress($address); | |
$dm->persist($user); | |
$dm->flush(); | |
//print_r($dm->getDocumentCollection('User')->findOne()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hello, this is what i expect to happen when i fetch a document that it bring it embedded documents with it, but is not working for me, can you see this question thanks