Inheritance: implements Doctrine\Common\Persistence\ObjectManager
 /**
  * Clean up database after running tests
  */
 public function tearDown()
 {
     parent::tearDown();
     if (isset($this->documentManager)) {
         $this->documentManager->getHttpClient()->request('DELETE', '/' . $this->settings['databaseName']);
     }
 }
Example #2
0
 /**
  * Updates a group.
  *
  * @param GroupInterface $group
  */
 public function updateGroup(GroupInterface $group, $andFlush = true)
 {
     $this->dm->persist($group);
     if ($andFlush) {
         $this->dm->flush();
     }
 }
Example #3
0
 public function resolveJsonField(ClassMetadata $class, DocumentManager $dm, $documentState, $jsonName, $originalData)
 {
     $uow = $dm->getUnitOfWork();
     $couchClient = $dm->getCouchDBClient();
     if ($jsonName == 'doctrine_metadata' && isset($originalData['doctrine_metadata']['associations'])) {
         foreach ($originalData['doctrine_metadata']['associations'] as $assocName) {
             $assocValue = $originalData[$assocName];
             if (isset($class->associationsMappings[$assocName])) {
                 if ($class->associationsMappings[$assocName]['type'] & ClassMetadata::TO_ONE) {
                     if ($assocValue) {
                         if ($class->associationsMappings[$assocName]['targetDocument']) {
                             $assocValue = $dm->getReference($class->associationsMappings[$assocName]['targetDocument'], $assocValue);
                         } else {
                             $response = $couchClient->findDocument($assocValue);
                             if ($response->status == 404) {
                                 $assocValue = null;
                             } else {
                                 $hints = array();
                                 $assocValue = $uow->createDocument(null, $response->body, $hints);
                             }
                         }
                     }
                     $documentState[$class->associationsMappings[$assocName]['fieldName']] = $assocValue;
                 } else {
                     if ($class->associationsMappings[$assocName]['type'] & ClassMetadata::MANY_TO_MANY) {
                         if ($class->associationsMappings[$assocName]['isOwning']) {
                             $documentState[$class->associationsMappings[$assocName]['fieldName']] = new PersistentIdsCollection(new ArrayCollection(), $class->associationsMappings[$assocName]['targetDocument'], $dm, $assocValue);
                         }
                     }
                 }
             }
         }
     }
     return $documentState;
 }
Example #4
0
 /**
  * Constructor
  * 
  * @param CouchDBClient $couchDBClient
  * @param DocumentManager $dm
  */
 public function __construct(CouchDBClient $couchDBClient = null, DocumentManager $dm = null)
 {
     if (!$couchDBClient && $dm) {
         $couchDBClient = $dm->getCouchDBClient();
     }
     $this->couchDBClient = $couchDBClient;
     $this->dm = $dm;
 }
 public function generate($document, ClassMetadata $cm, DocumentManager $dm)
 {
     if (empty($this->uuids)) {
         $UUIDGenerationBufferSize = $dm->getConfiguration()->getUUIDGenerationBufferSize();
         $this->uuids = $dm->getCouchDBClient()->getUuids($UUIDGenerationBufferSize);
     }
     $id = array_pop($this->uuids);
     $cm->reflFields[$cm->identifier]->setValue($document, $id);
     return $id;
 }
 public function testLoadingMappedsuperclass()
 {
     $document = new ExtendingClass();
     $document->topic = 'Superclass test';
     $document->headline = 'test test test';
     $this->dm->persist($document);
     $this->dm->flush();
     $id = $document->id;
     $this->dm->clear();
     $doc = $this->dm->find('Doctrine\\Tests\\Models\\Mapping\\ExtendingClass', $id);
     $this->assertInstanceOf('\\Doctrine\\Tests\\Models\\Mapping\\ExtendingClass', $doc);
     $this->assertEquals('test test test', $doc->headline);
     $this->assertEquals('Superclass test', $doc->topic);
 }
 public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 {
     if (count($criteria) == 1) {
         foreach ($criteria as $field => $value) {
             $query = $this->dm->createQuery('doctrine_repositories', 'equal_constraint')->setKey(array($this->documentType, $field, $value))->setIncludeDocs(true)->toArray(true);
             if ($limit) {
                 $query->setLimit($limit);
             }
             if ($offset) {
                 $query->setSkip($offset);
             }
             return $query->execute();
         }
     } else {
         $ids = array();
         $num = 0;
         foreach ($criteria as $field => $value) {
             $ids[$num] = array();
             $result = $this->dm->createNativeQuery('doctrine_repositories', 'equal_constraint')->setKey(array($this->documentType, $field, $value))->execute();
             foreach ($result as $doc) {
                 $ids[$num][] = $doc['id'];
             }
             $num++;
         }
         $mergeIds = $ids[0];
         for ($i = 1; $i < $num; $i++) {
             $mergeIds = array_intersect($mergeIds, $ids[$i]);
         }
         return $this->findMany(array_values($mergeIds), $limit, $offset);
     }
 }
 /**
  * Gets the class metadata descriptor for a class.
  *
  * @param string $className The name of the class.
  * @return Doctrine\ODM\CouchDB\Mapping\ClassMetadata
  */
 public function getMetadataFor($className)
 {
     if (!isset($this->loadedMetadata[$className])) {
         $realClassName = $className;
         // Check for namespace alias
         if (strpos($className, ':') !== false) {
             list($namespaceAlias, $simpleClassName) = explode(':', $className);
             $realClassName = $this->dm->getConfiguration()->getDocumentNamespace($namespaceAlias) . '\\' . $simpleClassName;
             if (isset($this->loadedMetadata[$realClassName])) {
                 // We do not have the alias name in the map, include it
                 $this->loadedMetadata[$className] = $this->loadedMetadata[$realClassName];
                 return $this->loadedMetadata[$realClassName];
             }
         }
         if ($this->cacheDriver) {
             if (($cached = $this->cacheDriver->fetch("{$realClassName}\$COUCHDBCLASSMETADATA")) !== false) {
                 $this->loadedMetadata[$realClassName] = $cached;
             } else {
                 foreach ($this->loadMetadata($realClassName) as $loadedClassName) {
                     $this->cacheDriver->save("{$loadedClassName}\$COUCHDBCLASSMETADATA", $this->loadedMetadata[$loadedClassName], null);
                 }
             }
         } else {
             $this->loadMetadata($realClassName);
         }
         if ($className != $realClassName) {
             // We do not have the alias name in the map, include it
             $this->loadedMetadata[$className] = $this->loadedMetadata[$realClassName];
         }
     }
     if (!isset($this->loadedMetadata[$className])) {
         throw MappingException::classNotMapped();
     }
     return $this->loadedMetadata[$className];
 }
 /**
  * @param \Zend\ServiceManager\ServiceLocatorInterface $serviceLocator
  * @return \Doctrine\ODM\Couch\DocumentManager
  */
 public function createService(ServiceLocatorInterface $serviceLocator)
 {
     $options = $this->getOptions($serviceLocator, 'documentmanager');
     $connection = $serviceLocator->get($options->getConnection());
     $config = $serviceLocator->get($options->getConfiguration());
     $eventManager = $serviceLocator->get($options->getEventManager());
     return DocumentManager::create($connection, $config, $eventManager);
 }
Example #10
0
 /**
  * @return void
  */
 public function flush()
 {
     try {
         $this->documentManager->flush();
     } catch (\Exception $exception) {
         $this->systemLogger->log('Could not flush ODM unit of work, error: ' . $exception->getMessage());
     }
 }
Example #11
0
 public function execute()
 {
     $response = $this->doExecute();
     if ($this->dm && $this->getParameter('include_docs') === true) {
         $uow = $this->dm->getUnitOfWork();
         foreach ($response->body['rows'] as $k => $v) {
             if (!isset($v['type']) && !$this->documentName) {
                 throw new \InvalidArgumentException("Cannot query " . $this->getHttpQuery() . " lucene and convert to document instances, " . "the type of document " . $v['id'] . " is not stored in Lucene. You can query without " . "include_docs and pass the ids to findMany() of the repository you know this document is " . "a type of.");
             }
             $v['type'] = isset($v['type']) ? $v['type'] : $this->documentName;
             $doc = $this->dm->find(str_replace(".", "\\", $v['type']), $v['id']);
             if ($this->onlyDocs) {
                 $response->body['rows'][$k] = $doc;
             } else {
                 $response->body['rows'][$k]['doc'] = $doc;
             }
         }
     }
     return $this->createResult($response);
 }
Example #12
0
 public function setUp()
 {
     $this->type = 'Doctrine\\Tests\\ODM\\CouchDB\\UoWUser';
     $this->dm = \Doctrine\ODM\CouchDB\DocumentManager::create(array('dbname' => 'test'));
     $this->uow = new UnitOfWork($this->dm);
     $metadata = new \Doctrine\ODM\CouchDB\Mapping\ClassMetadata($this->type);
     $metadata->mapField(array('fieldName' => 'id', 'id' => true));
     $metadata->mapField(array('fieldName' => 'username', 'type' => 'string'));
     $metadata->idGenerator = \Doctrine\ODM\CouchDB\Mapping\ClassMetadata::IDGENERATOR_ASSIGNED;
     $cmf = $this->dm->getClassMetadataFactory();
     $cmf->setMetadataFor($this->type, $metadata);
 }
 /**
  * Fetch an object from persistence layer.
  *
  * @param string $identity
  * @param string $targetType
  * @return object
  * @throws \TYPO3\Flow\Property\Exception\TargetNotFoundException
  * @throws \TYPO3\Flow\Property\Exception\InvalidSourceException
  */
 protected function fetchObjectFromPersistence($identity, $targetType)
 {
     if (is_string($identity)) {
         $object = $this->documentManager->find($targetType, $identity);
     } else {
         throw new \TYPO3\Flow\Property\Exception\InvalidSourceException('The identity property "' . $identity . '" is not a string.', 1356681336);
     }
     if ($object === NULL) {
         throw new \TYPO3\Flow\Property\Exception\TargetNotFoundException('Document with identity "' . print_r($identity, TRUE) . '" not found.', 1356681356);
     }
     return $object;
 }
Example #14
0
 public function execute()
 {
     $response = $this->doExecute();
     $data = array();
     if ($this->dm && $this->getParameter('include_docs') === true) {
         $uow = $this->dm->getUnitOfWork();
         foreach ($response->body['rows'] as $k => $v) {
             $doc = $uow->createDocument(null, $v['doc']);
             if ($this->toArray) {
                 $data[] = $doc;
             } else {
                 if ($this->onlyDocs) {
                     $response->body['rows'][$k] = $doc;
                 } else {
                     $response->body['rows'][$k]['doc'] = $doc;
                 }
             }
         }
     }
     return $this->toArray ? $data : $this->createResult($response);
 }
 public function createDocumentManager()
 {
     $couchDBClient = $this->createCouchDBClient();
     $httpClient = $couchDBClient->getHttpClient();
     $database = $couchDBClient->getDatabase();
     $httpClient->request('DELETE', '/' . $database);
     $resp = $httpClient->request('PUT', '/' . $database);
     $reader = new \Doctrine\Common\Annotations\SimpleAnnotationReader();
     $reader->addNamespace('Doctrine\\ODM\\CouchDB\\Mapping\\Annotations');
     $paths = __DIR__ . "/../../Models";
     $metaDriver = new AnnotationDriver($reader, $paths);
     $config = $this->createConfiguration($metaDriver);
     return DocumentManager::create($couchDBClient, $config);
 }
Example #16
0
 /**
  * Find many documents by id.
  *
  * Important: Each document is returned with the key it has in the $ids array!
  *
  * @param array $ids
  * @param string $documentName
  * @param int $limit
  * @param int $offset
  * @return array
  */
 public function findMany(array $ids, $documentName = null, $limit = null, $offset = null)
 {
     $response = $this->dm->getCouchDBClient()->findDocuments($ids, $limit, $offset);
     $keys = array_flip($ids);
     if ($response->status != 200) {
         throw new \Exception("loadMany error code " . $response->status);
     }
     $docs = array();
     foreach ($response->body['rows'] as $responseData) {
         if (isset($responseData['doc'])) {
             $docs[$keys[$responseData['id']]] = $this->createDocument($documentName, $responseData['doc']);
         }
     }
     return $docs;
 }
 /**
  * @Flow\Around("within(TYPO3\Flow\Persistence\PersistenceManagerInterface) && method(.*->getObjectByIdentifier())")
  * @param \TYPO3\Flow\Aop\JoinPointInterface $joinPoint The current join point
  * @return object
  */
 public function getObjectByIdentifier(\TYPO3\Flow\Aop\JoinPointInterface $joinPoint)
 {
     $object = $joinPoint->getAdviceChain()->proceed($joinPoint);
     if ($object === NULL) {
         try {
             $document = $this->documentManager->find($joinPoint->getMethodArgument('objectType'), $joinPoint->getMethodArgument('identifier'));
             if ($document !== NULL) {
                 return $document;
             }
         } catch (\Doctrine\ODM\CouchDB\Mapping\MappingException $exception) {
             // probably not a valid document, so ignore it
         }
     }
     return $object;
 }
Example #18
0
	static public function createOdm(
		$database, $host = 'localhost', $port = '5984', 
		$username = null, $password = null,
		array $path = array(),
		$namespace = 'Doctrine\ODM\CouchDB\Mapping\\')
	{
		$driver = self::loadDriverForDocuments($path, $namespace);
		
		$config = new Configuration();
		$config->setDatabase($database);
		$config->setMetadataDriverImpl($driver);

		$httpClient = new SocketClient($host, $port, $username, $password);
		$config->setHttpClient($httpClient);
		
		return DocumentManager::create($config);
	}
 public function createDocumentManager()
 {
     $couchDBClient = $this->createCouchDBClient();
     $httpClient = $couchDBClient->getHttpClient();
     $database = $couchDBClient->getDatabase();
     $httpClient->request('DELETE', '/' . $database);
     $resp = $httpClient->request('PUT', '/' . $database);
     $reader = new AnnotationReader();
     $reader->setDefaultAnnotationNamespace('Doctrine\\ODM\\CouchDB\\Mapping\\');
     $paths = __DIR__ . "/../../Models";
     $metaDriver = new AnnotationDriver($reader, $paths);
     $config = new Configuration();
     $config->setProxyDir(\sys_get_temp_dir());
     $config->setMetadataDriverImpl($metaDriver);
     $setMetadataCacheImpl = $config->setMetadataCacheImpl(new ArrayCache());
     $config->setLuceneHandlerName('_fti');
     return DocumentManager::create($couchDBClient, $config);
 }
 /**
  * {@inheritdoc}
  */
 protected function getFqcnFromAlias($namespaceAlias, $simpleClassName)
 {
     return $this->dm->getConfiguration()->getDocumentNamespace($namespaceAlias) . '\\' . $simpleClassName;
 }
 public function reloadUser(UserInterface $user)
 {
     $this->dm->refresh($user);
 }
 public function testCreateNewDocumentManagerWithoutHttpClientUsingSocketDefault()
 {
     $dm = \Doctrine\ODM\CouchDB\DocumentManager::create(array('dbname' => 'test'));
     $this->assertInstanceOf('Doctrine\\CouchDB\\HTTP\\SocketClient', $dm->getHttpClient());
 }
Example #23
0
 public function getMetadataFactory()
 {
     $dm = \Doctrine\ODM\CouchDB\DocumentManager::create(array('dbname' => 'test'));
     return new \Doctrine\ODM\CouchDB\Mapping\ClassMetadataFactory($dm);
 }
 public function setUp()
 {
     $this->dm = \Doctrine\ODM\CouchDB\DocumentManager::create(array('dbname' => 'test'));
 }
Example #25
0
 /**
  * Constructs and sets all options at once.
  *
  * @param array $options
  *
  * Options are:
  *
  * @option string             $dbname The name of the database
  * @option string             $type The connection type, "socket" or "stream"
  * @option string             $host
  * @option int                $port
  * @option string             $user
  * @option string             $password
  * @option string             $ip
  * @option bool               $logging
  * @option Configuration      $config
  * @option EventManager       $manager
  * @option array               $namespaces Array of additional document namespaces
  *
  * @return \Doctrine\ODM\CouchDB\DocumentManager
  */
 public static function documentManager($options = array())
 {
     $_key = Option::get($options, 'host', 'localhost') . ':' . Option::get($options, 'port', 5984);
     if (!isset(self::$_dms[$_key])) {
         self::$_dms[$_key] = \Doctrine\ODM\CouchDB\DocumentManager::create(Option::get($options, 'client', static::couchDbClient($options)), Option::get($options, 'config'), Option::get($options, 'manager'));
     }
     return self::$_dms[$_key];
 }
 /**
  * Creates a Doctrine ODM DocumentManager
  *
  * @return \Doctrine\ODM\CouchDB\DocumentManager
  */
 public function create()
 {
     if (isset($this->documentManager)) {
         return $this->documentManager;
     }
     $httpClient = new \Doctrine\CouchDB\HTTP\SocketClient($this->settings['host'], $this->settings['port'], $this->settings['username'], $this->settings['password'], $this->settings['ip']);
     $reader = new \Doctrine\Common\Annotations\AnnotationReader();
     $metaDriver = new \Doctrine\ODM\CouchDB\Mapping\Driver\AnnotationDriver($reader);
     $config = new \Doctrine\ODM\CouchDB\Configuration();
     $config->setMetadataDriverImpl($metaDriver);
     $packages = $this->packageManager->getActivePackages();
     foreach ($packages as $package) {
         $designDocumentRootPath = \TYPO3\Flow\Utility\Files::concatenatePaths(array($package->getPackagePath(), 'Migrations/CouchDB/DesignDocuments'));
         if (is_dir($designDocumentRootPath)) {
             $packageDesignDocumentFolders = glob($designDocumentRootPath . '/*');
             foreach ($packageDesignDocumentFolders as $packageDesignDocumentFolder) {
                 if (is_dir($packageDesignDocumentFolder)) {
                     $designDocumentName = strtolower(basename($packageDesignDocumentFolder));
                     $config->addDesignDocument($designDocumentName, 'Radmiraal\\CouchDB\\View\\Migration', array('packageKey' => $package->getPackageKey(), 'path' => $packageDesignDocumentFolder));
                 }
             }
         }
     }
     $proxyDirectory = \TYPO3\Flow\Utility\Files::concatenatePaths(array($this->environment->getPathToTemporaryDirectory(), 'DoctrineODM/Proxies'));
     \TYPO3\Flow\Utility\Files::createDirectoryRecursively($proxyDirectory);
     $config->setProxyDir($proxyDirectory);
     $config->setProxyNamespace('TYPO3\\Flow\\Persistence\\DoctrineODM\\Proxies');
     $config->setAutoGenerateProxyClasses(TRUE);
     $couchClient = new \Doctrine\CouchDB\CouchDBClient($httpClient, $this->settings['databaseName']);
     $this->documentManager = \Doctrine\ODM\CouchDB\DocumentManager::create($couchClient, $config);
     return $this->documentManager;
 }
 /**
  * Convenience method for flushing the document manager
  *
  * @return void
  */
 public function flushDocumentManager()
 {
     $this->documentManager->flush();
 }