/** * {@inheritdoc} */ public function create(DocumentManager $dm, array $mapping, BaseCollection $coll = null) { if ($coll === null) { $coll = !empty($mapping['collectionClass']) ? $this->createCollectionClass($mapping['collectionClass']) : new ArrayCollection(); } if (empty($mapping['collectionClass'])) { return new PersistentCollection($coll, $dm, $dm->getUnitOfWork()); } $className = $dm->getConfiguration()->getPersistentCollectionGenerator()->loadClass($mapping['collectionClass'], $dm->getConfiguration()->getAutoGeneratePersistentCollectionClasses()); return new $className($coll, $dm, $dm->getUnitOfWork()); }
/** * Gets the Query executable. * * @param array $options * @return Query $query */ public function getQuery(array $options = array()) { if ($this->query['type'] === Query::TYPE_MAP_REDUCE) { $this->hydrate = false; } $documentPersister = $this->dm->getUnitOfWork()->getDocumentPersister($this->class->name); $query = $this->query; $query['query'] = $this->expr->getQuery(); $query['query'] = $documentPersister->addDiscriminatorToPreparedQuery($query['query']); $query['query'] = $documentPersister->addFilterToPreparedQuery($query['query']); $query['newObj'] = $this->expr->getNewObj(); if (isset($query['distinct'])) { $query['distinct'] = $documentPersister->prepareFieldName($query['distinct']); } if (isset($query['select'])) { $query['select'] = $documentPersister->prepareSortOrProjection($query['select']); } if (isset($query['sort'])) { $query['sort'] = $documentPersister->prepareSortOrProjection($query['sort']); } if ($this->class->slaveOkay) { $query['slaveOkay'] = $this->class->slaveOkay; } return new Query($this->dm, $this->class, $this->collection, $query, $options, $this->hydrate, $this->refresh, $this->primers, $this->requireIndexes); }
/** * {@inheritdoc} */ protected function createResourceRepository($class, DocumentManager $documentManager, ClassMetadata $metadata, ResourceInterface $resource = null) { if ($resource !== null && is_a($class, TranslatableRepository::class, true)) { return new $class($documentManager, $documentManager->getUnitOfWork(), $metadata, $resource, $this->getLocaleContext()); } return parent::createResourceRepository($class, $documentManager, $metadata, $resource); }
/** * Execute the query and returns the results. * * @return mixed */ public function execute() { $uow = $this->dm->getUnitOfWork(); $results = parent::execute(); // Convert the regular mongodb cursor to the odm cursor if ($results instanceof BaseCursor) { $cursor = $results->getMongoCursor(); $results = new Cursor($cursor, $this->dm->getUnitOfWork(), $this->class); $results->hydrate($this->hydrate); $results->refresh($this->refresh); } $hints = array(); if ($this->refresh) { $hints[self::HINT_REFRESH] = true; } // GeoLocationFindQuery just returns an instance of ArrayIterator so we have to // iterator over it and hydrate each object. if ($this->query['type'] === self::TYPE_GEO_LOCATION && $this->hydrate) { foreach ($results as $key => $result) { $document = $result['obj']; if ($this->class->distance) { $document[$this->class->distance] = $result['dis']; } $results[$key] = $uow->getOrCreateDocument($this->class->name, $document, $hints); } $results->reset(); } if ($this->hydrate && is_array($results) && isset($results['_id'])) { // Convert a single document array to a document object $results = $uow->getOrCreateDocument($this->class->name, $results, $hints); } return $results; }
/** * @param string $class * @param DocumentManager $documentManager * @param ClassMetadata $metadata * @param ResourceInterface|null $resource * * @return ObjectRepository */ protected function createResourceRepository($class, DocumentManager $documentManager, ClassMetadata $metadata, ResourceInterface $resource = null) { if ($resource !== null && is_a($class, BaseRepositoryInterface::class, true)) { return new $class($documentManager, $documentManager->getUnitOfWork(), $metadata, $resource); } return parent::createRepository($documentManager, $metadata->getName()); }
/** * @return \Doctrine\ODM\MongoDB\DocumentManager */ public function dm() { if ($this->dm === null) { $this->dm = $this->createTestDocumentManager(); $this->uow = $this->dm->getUnitOfWork(); Type::addType('Phonenumber', PhonenumberType::class); Type::addType('Role', RoleType::class); $this->dm->getEventManager()->addEventSubscriber(new MetadataEventSubscriber()); $this->dm->getEventManager()->addEventSubscriber(new CollectionsEventSubscriber()); $this->dm->getEventManager()->addEventSubscriber(new ModelEventSubscriber()); $this->dm->getEventManager()->addEventSubscriber(new IdentityEventSubscriber()); $this->dm->getEventManager()->addEventSubscriber(new GeolocationEventSubscriber()); $this->dm->getEventManager()->addEventSubscriber(new SystemEventSubscriber()); } return $this->dm; }
/** * Gets the Query executable. * * @param array $options * @return Query $query */ public function getQuery(array $options = array()) { if ($this->query['type'] === Query::TYPE_MAP_REDUCE) { $this->hydrate = false; } $documentPersister = $this->dm->getUnitOfWork()->getDocumentPersister($this->class->name); $query = $this->query; $query['query'] = $this->expr->getQuery(); $query['query'] = $documentPersister->addDiscriminatorToPreparedQuery($query['query']); $query['query'] = $documentPersister->addFilterToPreparedQuery($query['query']); $query['newObj'] = $this->expr->getNewObj(); if (isset($query['distinct'])) { $query['distinct'] = $documentPersister->prepareFieldName($query['distinct']); } if (!empty($query['select'])) { $query['select'] = $documentPersister->prepareSortOrProjection($query['select']); if ($this->hydrate && $this->class->inheritanceType === ClassMetadataInfo::INHERITANCE_TYPE_SINGLE_COLLECTION && !isset($query['select'][$this->class->discriminatorField])) { $includeMode = 0 < count(array_filter($query['select'], function ($mode) { return $mode == 1; })); if ($includeMode && !isset($query['select'][$this->class->discriminatorField])) { $query['select'][$this->class->discriminatorField] = 1; } } } if (isset($query['sort'])) { $query['sort'] = $documentPersister->prepareSortOrProjection($query['sort']); } if ($this->class->slaveOkay) { $query['slaveOkay'] = $this->class->slaveOkay; } return new Query($this->dm, $this->class, $this->collection, $query, $options, $this->hydrate, $this->refresh, $this->primers, $this->requireIndexes); }
/** * Wraps the supplied base cursor as an ODM one. * * @param Doctrine\MongoDB\Cursor $cursor The base cursor * * @return Cursor An ODM cursor */ private function wrapCursor(BaseCursor $cursor) { if ($cursor instanceof BaseLoggableCursor) { return new LoggableCursor($this->dm->getConnection(), $this->collection, $this->dm->getUnitOfWork(), $this->class, $cursor, $cursor->getQuery(), $cursor->getFields(), $this->dm->getConfiguration()->getRetryQuery(), $cursor->getLoggerCallable()); } else { return new Cursor($this->dm->getConnection(), $this->collection, $this->dm->getUnitOfWork(), $this->class, $cursor, $cursor->getQuery(), $cursor->getFields(), $this->dm->getConfiguration()->getRetryQuery()); } }
/** * Initializes a new BasicDocumentPersister instance. * * @param Doctrine\ODM\MongoDB\DocumentManager $dm * @param Doctrine\ODM\MongoDB\Mapping\ClassMetadata $class */ public function __construct(DocumentManager $dm, ClassMetadata $class) { $this->_dm = $dm; $this->_uow = $dm->getUnitOfWork(); $this->_class = $class; $this->_documentName = $class->getName(); $this->_collection = $dm->getDocumentCollection($class->name); }
/** * @group odm */ public function testODMCreate() { $manager = new FileManager($this->odmStorage, $this->rootDir); $file = $manager->create('myDomain.en.yml', '/test/root/dir/src/Project/CoolBundle/Resources/translations'); $this->assertEquals(ODMUnitOfWork::STATE_MANAGED, $this->dm->getUnitOfWork()->getDocumentState($file)); $this->assertEquals('myDomain', $file->getDomain()); $this->assertEquals('en', $file->getLocale()); $this->assertEquals('yml', $file->getExtention()); $this->assertEquals('myDomain.en.yml', $file->getName()); $this->assertEquals('../src/Project/CoolBundle/Resources/translations', $file->getPath()); $file = $manager->create('messages.fr.xliff', '/test/root/dir/app/Resources/translations', true); $this->assertEquals(ODMUnitOfWork::STATE_MANAGED, $this->dm->getUnitOfWork()->getDocumentState($file)); $this->assertEquals('messages', $file->getDomain()); $this->assertEquals('fr', $file->getLocale()); $this->assertEquals('xliff', $file->getExtention()); $this->assertEquals('messages.fr.xliff', $file->getName()); $this->assertEquals('Resources/translations', $file->getPath()); }
/** * Initializes a new instance of the <tt>ProxyFactory</tt> class that is * connected to the given <tt>DocumentManager</tt>. * * @param \Doctrine\ODM\MongoDB\DocumentManager $documentManager The DocumentManager the new factory works for. * @param string $proxyDir The directory to use for the proxy classes. It * must exist. * @param string $proxyNamespace The namespace to use for the proxy classes. * @param integer $autoGenerate Whether to automatically generate proxy classes. */ public function __construct(DocumentManager $documentManager, $proxyDir, $proxyNamespace, $autoGenerate = AbstractProxyFactory::AUTOGENERATE_NEVER) { $this->metadataFactory = $documentManager->getMetadataFactory(); $this->uow = $documentManager->getUnitOfWork(); $this->proxyNamespace = $proxyNamespace; $proxyGenerator = new ProxyGenerator($proxyDir, $proxyNamespace); $proxyGenerator->setPlaceholder('baseProxyInterface', 'Doctrine\\ODM\\MongoDB\\Proxy\\Proxy'); parent::__construct($proxyGenerator, $this->metadataFactory, $autoGenerate); }
/** * Marks this collection as changed/dirty. */ private function changed() { if (!$this->isDirty) { $this->isDirty = true; if ($this->dm && $this->mapping !== null && $this->dm->getClassMetadata(get_class($this->owner))->isChangeTrackingNotify()) { $this->dm->getUnitOfWork()->scheduleForDirtyCheck($this->owner); } } }
function let(EntityManager $em, DocumentManager $dm, ManagerRegistry $registry, ObjectRepository $repository, ClassMetadata $classMetadata, MongoDBODMUnitOfWork $uow) { $registry->getManagerForClass(Argument::type('string'))->will(function ($args) use($em, $dm) { return 'MyItemClass' === $args[0] ? $em : $dm; }); $registry->getRepository('MyItemClass')->willReturn($repository); $em->getClassMetadata('MyItemClass')->willReturn($classMetadata); $dm->getUnitOfWork()->willReturn($uow); $this->beConstructedWith('MyItemClass', [4, 8, 15], $registry); }
/** * Create a new repository instance for a document class. * * @param DocumentManager $documentManager The DocumentManager instance. * @param string $documentName The name of the document. * * @return \Doctrine\Common\Persistence\ObjectRepository */ protected function createRepository(DocumentManager $documentManager, $documentName) { $metadata = $documentManager->getClassMetadata($documentName); $repositoryClassName = $metadata->customRepositoryClassName; if ($repositoryClassName === null) { $configuration = $documentManager->getConfiguration(); $repositoryClassName = $configuration->getDefaultRepositoryClassName(); } return new $repositoryClassName($documentManager, $documentManager->getUnitOfWork(), $metadata); }
/** * Gets the Query executable. * * @param array $options * @return QueryInterface $query */ public function getQuery(array $options = array()) { if ($this->query['type'] === Query::TYPE_MAP_REDUCE) { $this->hydrate = false; } $query = $this->query; $query['query'] = $this->dm->getUnitOfWork()->getDocumentPersister($this->class->name)->prepareQuery($this->expr->getQuery()); $query['newObj'] = $this->expr->getNewObj(); return new Query($this->dm, $this->class, $this->database, $this->collection, $query, $options, $this->cmd, $this->hydrate, $this->refresh); }
/** * Initializes a new instance of the <tt>ProxyFactory</tt> class that is * connected to the given <tt>DocumentManager</tt>. * * @param \Doctrine\ODM\MongoDB\DocumentManager $documentManager The DocumentManager the new factory works for. * @param string $proxyDir The directory to use for the proxy classes. It * must exist. * @param string $proxyNamespace The namespace to use for the proxy classes. * @param integer $autoGenerate Whether to automatically generate proxy classes. */ public function __construct(DocumentManager $documentManager, $proxyDir, $proxyNamespace, $autoGenerate = AbstractProxyFactory::AUTOGENERATE_NEVER) { $this->metadataFactory = $documentManager->getMetadataFactory(); $this->uow = $documentManager->getUnitOfWork(); $this->proxyNamespace = $proxyNamespace; $this->lifecycleEventManager = new LifecycleEventManager($documentManager, $this->uow, $documentManager->getEventManager()); $proxyGenerator = new ProxyGenerator($proxyDir, $proxyNamespace); $proxyGenerator->setPlaceholder('baseProxyInterface', Proxy::class); parent::__construct($proxyGenerator, $this->metadataFactory, $autoGenerate); }
/** * Gets or creates a Persister instance for the given class name. * * @param string $className * @return Persister $persister */ public function getDocumentPersister($className) { if (isset($this->persisters[$className])) { return $this->persisters[$className]; } $class = $this->dm->getClassMetadata($className); $collection = $this->dm->getDocumentCollection($className); $persister = $this->dm->getUnitOfWork()->getDocumentPersister($className); $this->persisters[$className] = new Persister($this->configuration, $class, $collection, $persister); return $this->persisters[$className]; }
private function wrapCursor(BaseCursor $baseCursor, array $hints) { if ($baseCursor instanceof BaseLoggableCursor) { $cursor = new LoggableCursor($this->dm->getConnection(), $this->collection, $this->dm->getUnitOfWork(), $this->class, $baseCursor, $baseCursor->getQuery(), $baseCursor->getFields(), $this->dm->getConfiguration()->getRetryQuery(), $baseCursor->getLoggerCallable()); } else { $cursor = new Cursor($this->dm->getConnection(), $this->collection, $this->dm->getUnitOfWork(), $this->class, $baseCursor, $baseCursor->getQuery(), $baseCursor->getFields(), $this->dm->getConfiguration()->getRetryQuery()); } $cursor->hydrate($this->hydrate); $cursor->setHints($hints); return $cursor; }
public function getQuery() { if ($this->type === self::TYPE_MAP_REDUCE) { $this->hydrate = false; } $query = $this->expr->getQuery(); $query = $this->dm->getUnitOfWork()->getDocumentPersister($this->class->name)->prepareQuery($query); $this->expr->setQuery($query); $query = parent::getQuery(); return new Query($query, $this->dm, $this->class, $this->hydrate); }
private function wrapCursor(BaseCursor $baseCursor) { $mongoCursor = $baseCursor->getMongoCursor(); if ($baseCursor instanceof BaseLoggableCursor) { $cursor = new LoggableCursor($mongoCursor, $this->dm->getUnitOfWork(), $this->class, $baseCursor->getLoggerCallable(), $baseCursor->getQuery(), $baseCursor->getFields()); } else { $cursor = new Cursor($mongoCursor, $this->dm->getUnitOfWork(), $this->class); } $cursor->hydrate($this->hydrate); $cursor->refresh($this->refresh); return $cursor; }
/** * Extracts identifiers from object or proxy * * @param DocumentManager $dm * @param object $object * @param bool $single * @return mixed - array or single identifier */ public function extractIdentifier(DocumentManager $dm, $object, $single = true) { $meta = $dm->getClassMetadata(get_class($object)); if ($object instanceof Proxy) { $id = $dm->getUnitOfWork()->getDocumentIdentifier($object); } else { $id = $meta->getReflectionProperty($meta->identifier)->getValue($object); } if ($single || !$id) { return $id; } else { return array($meta->identifier => $id); } }
/** * Gets the Query executable. * * @param array $options * @return QueryInterface $query */ public function getQuery(array $options = array()) { if ($this->query['type'] === Query::TYPE_MAP_REDUCE) { $this->hydrate = false; } $query = $this->query; $query['query'] = $this->expr->getQuery(); $query['newObj'] = $this->expr->getNewObj(); $query['sort'] = $this->dm->getUnitOfWork()->getDocumentPersister($this->class->name)->prepareSort($query['sort']); if ($this->class->slaveOkay) { $query['slaveOkay'] = $this->class->slaveOkay; } return new Query($this->dm, $this->class, $this->database, $this->collection, $query, $options, $this->cmd, $this->hydrate, $this->refresh, $this->primers, $this->requireIndexes); }
/** * Prepare the Cursor returned by {@link Query::execute()}. * * This method will wrap the base Cursor with an ODM Cursor or EagerCursor, * and set the hydrate option and UnitOfWork hints. This occurs in addition * to any preparation done by the base Query class. * * @see \Doctrine\MongoDB\Cursor::prepareCursor() * @param BaseCursor $cursor * @return CursorInterface */ protected function prepareCursor(BaseCursor $cursor) { $cursor = parent::prepareCursor($cursor); // Convert the base Cursor into an ODM Cursor $cursorClass = !empty($this->query['eagerCursor']) ? 'Doctrine\\ODM\\MongoDB\\EagerCursor' : 'Doctrine\\ODM\\MongoDB\\Cursor'; $cursor = new $cursorClass($cursor, $this->dm->getUnitOfWork(), $this->class); $cursor->hydrate($this->hydrate); $cursor->setHints($this->unitOfWorkHints); if (!empty($this->primers)) { $referencePrimer = new ReferencePrimer($this->dm, $this->dm->getUnitOfWork()); $cursor->enableReferencePriming($this->primers, $referencePrimer); } return $cursor; }
/** * {@inheritdoc} */ public function clear() { if ($this->initialized && $this->isEmpty()) { return; } if ($this->mapping !== null && isset($this->mapping['embedded'])) { foreach ($this->coll as $element) { $this->dm->getUnitOfWork()->scheduleOrphanRemoval($element); } } $this->coll->clear(); $this->changed(); $this->dm->getUnitOfWork()->scheduleCollectionDeletion($this); $this->takeSnapshot(); }
/** * Prepare the Cursor returned by {@link Query::execute()}. * * This method will wrap the base Cursor with an ODM Cursor or EagerCursor, * and set the hydrate option and UnitOfWork hints. This occurs in addition * to any preparation done by the base Query class. * * @see \Doctrine\MongoDB\Cursor::prepareCursor() * @param BaseCursor $cursor * @return Cursor|EagerCursor */ protected function prepareCursor(BaseCursor $cursor) { $cursor = parent::prepareCursor($cursor); // Unwrap a base EagerCursor if ($cursor instanceof BaseEagerCursor) { $cursor = $cursor->getCursor(); } // Convert the base Cursor into an ODM Cursor $cursor = new Cursor($cursor, $this->dm->getUnitOfWork(), $this->class); // Wrap ODM Cursor with EagerCursor if (!empty($this->query['eagerCursor'])) { $cursor = new EagerCursor($cursor, $this->dm->getUnitOfWork(), $this->class); } $cursor->hydrate($this->hydrate); $cursor->setHints($this->unitOfWorkHints); return $cursor; }
/** * @param ClassMetadata $class * @return array */ private function prepareIndexes(ClassMetadata $class) { $persister = $this->dm->getUnitOfWork()->getDocumentPersister($class->name); $indexes = $class->getIndexes(); $newIndexes = array(); foreach ($indexes as $index) { $newIndex = array('keys' => array(), 'options' => $index['options']); foreach ($index['keys'] as $key => $value) { $key = $persister->prepareFieldName($key); if ($class->hasField($key)) { $mapping = $class->getFieldMapping($key); $newIndex['keys'][$mapping['name']] = $value; } else { $newIndex['keys'][$key] = $value; } } $newIndexes[] = $newIndex; } return $newIndexes; }
/** * Gets the Query executable. * * @param array $options * @return Query $query */ public function getQuery(array $options = array()) { if ($this->query['type'] === Query::TYPE_MAP_REDUCE) { $this->hydrate = false; } $documentPersister = $this->dm->getUnitOfWork()->getDocumentPersister($this->class->name); $query = $this->query; $query['query'] = $this->expr->getQuery(); $query['query'] = $documentPersister->addDiscriminatorToPreparedQuery($query['query']); $query['query'] = $documentPersister->addFilterToPreparedQuery($query['query']); $query['newObj'] = $this->expr->getNewObj(); if (isset($query['distinct'])) { $query['distinct'] = $documentPersister->prepareFieldName($query['distinct']); } if ($this->class->inheritanceType === ClassMetadataInfo::INHERITANCE_TYPE_SINGLE_COLLECTION && !empty($query['upsert']) && (empty($query['query'][$this->class->discriminatorField]) || is_array($query['query'][$this->class->discriminatorField]))) { throw new \InvalidArgumentException('Upsert query that is to be performed on discriminated document does not have single ' . 'discriminator. Either not use base class or set \'' . $this->class->discriminatorField . '\' field manually.'); } if (!empty($query['select'])) { $query['select'] = $documentPersister->prepareSortOrProjection($query['select']); if ($this->hydrate && $this->class->inheritanceType === ClassMetadataInfo::INHERITANCE_TYPE_SINGLE_COLLECTION && !isset($query['select'][$this->class->discriminatorField])) { $includeMode = 0 < count(array_filter($query['select'], function ($mode) { return $mode == 1; })); if ($includeMode && !isset($query['select'][$this->class->discriminatorField])) { $query['select'][$this->class->discriminatorField] = 1; } } } if (isset($query['sort'])) { $query['sort'] = $documentPersister->prepareSortOrProjection($query['sort']); } if ($this->class->slaveOkay) { $query['slaveOkay'] = $this->class->slaveOkay; } return new Query($this->dm, $this->class, $this->collection, $query, $options, $this->hydrate, $this->refresh, $this->primers, $this->requireIndexes); }
/** {@inheritdoc} */ public function setDocumentManager(DocumentManager $dm) { $this->dm = $dm; $this->uow = $dm->getUnitOfWork(); }
public function setUp() { $this->dm = $this->createTestDocumentManager(); $this->uow = $this->dm->getUnitOfWork(); }
/** * Wraps the supplied base cursor in the corresponding ODM class. * * @param CursorInterface $baseCursor * @return Cursor */ private function wrapCursor(CursorInterface $baseCursor) { return new Cursor($baseCursor, $this->dm->getUnitOfWork(), $this->class); }