Created
October 9, 2014 21:28
-
-
Save gistya/861809e185f87f95efca to your computer and use it in GitHub Desktop.
Doctrine ODM MongoDB PHP test
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 | |
| /** | |
| * set up Doctrine | |
| */ | |
| use Doctrine\MongoDB\Connection; | |
| use Doctrine\ODM\MongoDB\Configuration; | |
| use Doctrine\ODM\MongoDB\DocumentManager; | |
| use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver; | |
| if ( ! file_exists($file = __DIR__.'/vendor/autoload.php')) { | |
| echo "error.. /vendor/autoload.php not found"; | |
| throw new RuntimeException('Install dependencies to run this script.'); | |
| } | |
| $loader = require_once $file; | |
| $loader->add('Documents', __DIR__); | |
| $connection = new Connection(); | |
| $config = new Configuration(); | |
| $config->setProxyDir(__DIR__ . '/Proxies'); | |
| $config->setProxyNamespace('Proxies'); | |
| $config->setHydratorDir(__DIR__ . '/Hydrators'); | |
| $config->setHydratorNamespace('Hydrators'); | |
| $config->setDefaultDB('doctrine_odm'); | |
| $config->setMetadataDriverImpl(AnnotationDriver::create(__DIR__ . '/Documents')); | |
| AnnotationDriver::registerAnnotationClasses(); | |
| $dm = DocumentManager::create($connection, $config); | |
| //set up classes | |
| use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; | |
| /** @ODM\MappedSuperclass */ | |
| abstract class BaseEmployee | |
| { | |
| /** @ODM\Id */ | |
| private $id; | |
| /** @ODM\Increment */ | |
| private $changes = 0; | |
| /** @ODM\Collection */ | |
| private $notes = array(); | |
| /** @ODM\String */ | |
| private $name; | |
| /** @ODM\Float */ | |
| private $salary; | |
| /** @ODM\Date */ | |
| private $started; | |
| /** @ODM\Date */ | |
| private $left; | |
| /** @ODM\EmbedOne(targetDocument="Address") */ | |
| private $address; | |
| // ... | |
| } | |
| /** @ODM\Document */ | |
| class Employee extends BaseEmployee | |
| { | |
| /** @ODM\ReferenceOne(targetDocument="Documents\Manager") */ | |
| private $manager; | |
| // ... | |
| } | |
| /** @ODM\Document */ | |
| class Manager extends BaseEmployee | |
| { | |
| /** @ODM\ReferenceMany(targetDocument="Documents\Project") */ | |
| private $projects = array(); | |
| // ... | |
| } | |
| /** @ODM\EmbeddedDocument */ | |
| class Address | |
| { | |
| /** @ODM\String */ | |
| private $address; | |
| /** @ODM\String */ | |
| private $city; | |
| /** @ODM\String */ | |
| private $state; | |
| /** @ODM\String */ | |
| private $zipcode; | |
| // ... | |
| } | |
| /** @ODM\Document */ | |
| class Project | |
| { | |
| /** @ODM\Id */ | |
| private $id; | |
| /** @ODM\String */ | |
| private $name; | |
| public function __construct($name) | |
| { | |
| $this->name = $name; | |
| } | |
| // ... | |
| } | |
| //actual code: | |
| $employee = new Employee(); | |
| $employee->setName('Employee'); | |
| $employee->setSalary(50000.00); | |
| $employee->setStarted(new \DateTime()); | |
| $address = new Address(); | |
| $address->setAddress('555 Doctrine Rd.'); | |
| $address->setCity('Nashville'); | |
| $address->setState('TN'); | |
| $address->setZipcode('37209'); | |
| $employee->setAddress($address); | |
| $project = new Project('New Project'); | |
| $manager = new Manager(); | |
| $manager->setName('Manager'); | |
| $manager->setSalary(100000.00); | |
| $manager->setStarted(new \DateTime()); | |
| $manager->addProject($project); | |
| $dm->persist($employee); | |
| $dm->persist($address); | |
| $dm->persist($project); | |
| $dm->persist($manager); | |
| $dm->flush(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment