Returns the metadata for a class.
public getClassMetadata ( string $className ) : |
||
$className | string | The class name. |
리턴 |
public function doCascadeRemovePersistentCollections($document, array &$visited) { $oid = spl_object_hash($document); if (isset($visited[$oid])) { return; // Prevent infinite recursion } $visited[$oid] = $document; // mark visited $class = $this->dm->getClassMetadata(get_class($document)); $associationMappings = $class->associationMappings; foreach ($associationMappings as $assoc) { $relatedDocuments = $class->reflFields[$assoc['fieldName']]->getValue($document); if ($relatedDocuments instanceof PersistentCollection) { $relatedDocuments = $relatedDocuments->getValues(); } if (is_array($relatedDocuments)) { $class->reflFields[$assoc['fieldName']]->setValue($document, $relatedDocuments); foreach ($relatedDocuments as $relatedDocument) { $this->doCascadeRemovePersistentCollections($relatedDocument, $visited); } } elseif (is_object($relatedDocuments)) { $this->doCascadeRemovePersistentCollections($relatedDocuments, $visited); } } }
/** * Constructor. * * @param DocumentManager $dm * @param string $class * @param string $metaClass */ public function __construct(DocumentManager $dm, $class, $metaClass) { $this->dm = $dm; $this->repository = $dm->getRepository($class); $this->class = $dm->getClassMetadata($class)->name; $this->metaClass = $dm->getClassMetadata($metaClass)->name; }
public function initialise($container) { $this->manager = $container->get('doctrine.odm.mongodb.document_manager'); $this->odmMetadata = $this->manager->getClassMetadata($this->documentName); $this->class = $this->odmMetadata->getReflectionClass()->name; $mapping = $container->get('grid.mapping.manager'); $mapping->addDriver($this, -1); $this->metadata = $mapping->getMetadata($this->class); }
/** * {@inheritdoc} */ public function out($collection) { try { $class = $this->dm->getClassMetadata($collection); } catch (BaseMappingException $e) { return parent::out($collection); } return $this->fromDocument($class); }
public function references($document) { $class = $this->dm->getClassMetadata(get_class($document)); $reference = array($this->cmd . 'ref' => $class->getCollection(), $this->cmd . 'id' => $class->getDatabaseIdentifierValue($class->getIdentifierValue($document)), $this->cmd . 'db' => $class->getDatabase()); if ($this->currentField) { $this->query[$this->currentField][$this->cmd . 'elemMatch'] = $reference; } else { $this->query[$this->cmd . 'elemMatch'] = $reference; } return $this; }
public function testResolveTargetDocumentListenerCanResolveTargetDocument() { $evm = $this->dm->getEventManager(); $this->listener->addResolveTargetDocument('Doctrine\\ODM\\MongoDB\\Tests\\Tools\\ResolveTargetInterface', 'Doctrine\\ODM\\MongoDB\\Tests\\Tools\\ResolveTargetDocument', array()); $this->listener->addResolveTargetDocument('Doctrine\\ODM\\MongoDB\\Tests\\Tools\\TargetInterface', 'Doctrine\\ODM\\MongoDB\\Tests\\Tools\\TargetDocument', array()); $evm->addEventListener(Events::loadClassMetadata, $this->listener); $cm = $this->dm->getClassMetadata('Doctrine\\ODM\\MongoDB\\Tests\\Tools\\ResolveTargetDocument'); $meta = $cm->associationMappings; $this->assertSame('Doctrine\\ODM\\MongoDB\\Tests\\Tools\\ResolveTargetDocument', $meta['refOne']['targetDocument']); $this->assertSame('Doctrine\\ODM\\MongoDB\\Tests\\Tools\\TargetDocument', $meta['refMany']['targetDocument']); $this->assertSame('Doctrine\\ODM\\MongoDB\\Tests\\Tools\\ResolveTargetDocument', $meta['embedOne']['targetDocument']); $this->assertSame('Doctrine\\ODM\\MongoDB\\Tests\\Tools\\TargetDocument', $meta['embedMany']['targetDocument']); }
public function testYamlMapping() { $referencerMeta = $this->dm->getClassMetadata('Mapping\\Fixture\\Yaml\\Referencer'); $referenceeMeta = $this->dm->getClassMetadata('Mapping\\Fixture\\Yaml\\Referenced'); $config = $this->referenceIntegrity->getConfiguration($this->dm, $referencerMeta->name); $this->assertNotEmpty($config['referenceIntegrity']); foreach ($config['referenceIntegrity'] as $propertyName => $referenceConfiguration) { $this->assertArrayHasKey($propertyName, $referencerMeta->reflFields); foreach ($referenceConfiguration as $inversedPropertyName => $integrityType) { $this->assertArrayHasKey($inversedPropertyName, $referenceeMeta->reflFields); $this->assertTrue(in_array($integrityType, ['nullify', 'restrict'])); } } }
/** * Constructor. * * @param DocumentManager $dm * @param string $class * @param SortingFactory $factory */ public function __construct(DocumentManager $dm, $class, SortingFactory $factory) { $this->dm = $dm; $this->repository = $dm->getRepository($class); $this->class = $dm->getClassMetadata($class)->name; $this->setSortingFactory($factory); }
/** * Gets reference mapping for current field from current class or its descendants. * * @return array * @throws MappingException */ private function getReferenceMapping() { $mapping = null; try { $mapping = $this->class->getFieldMapping($this->currentField); } catch (MappingException $e) { if (empty($this->class->discriminatorMap)) { throw $e; } $foundIn = null; foreach ($this->class->discriminatorMap as $child) { $childClass = $this->dm->getClassMetadata($child); if ($childClass->hasAssociation($this->currentField)) { if ($mapping !== null && $mapping !== $childClass->getFieldMapping($this->currentField)) { throw MappingException::referenceFieldConflict($this->currentField, $foundIn->name, $childClass->name); } $mapping = $childClass->getFieldMapping($this->currentField); $foundIn = $childClass; } } if ($mapping === null) { throw MappingException::mappingNotFoundInClassNorDescendants($this->class->name, $this->currentField); } } return $mapping; }
/** * Executes the queued restorations. */ private function executeRestores() { $deletedFieldName = $this->configuration->getDeletedFieldName(); $persisters = array(); foreach ($this->restoreBy as $className => $criterias) { $persister = $this->getDocumentPersister($className); $persisters[$className] = $persister; foreach ($criterias as $criteria) { $persister->addRestoreBy($criteria); } } $documentRestores = array(); foreach ($this->documentRestores as $document) { $className = get_class($document); $documentRestores[$className][] = $document; $persister = $this->getDocumentPersister($className); $persisters[$className] = $persister; $persister->addRestore($document); } foreach ($persisters as $className => $persister) { $persister->executeRestores(); $class = $this->dm->getClassMetadata($className); if (isset($documentRestores[$className])) { $documents = $documentRestores[$className]; foreach ($documents as $document) { $class->setFieldValue($document, $deletedFieldName, null); if ($this->eventManager->hasListeners(Events::postRestore)) { $this->eventManager->dispatchEvent(Events::postRestore, new Event\LifecycleEventArgs($document, $this)); } } } } }
/** * @param string $fieldName * @return $this * @throws MappingException */ private function fromReference($fieldName) { if (!$this->class->hasReference($fieldName)) { MappingException::referenceMappingNotFound($this->class->name, $fieldName); } $referenceMapping = $this->class->getFieldMapping($fieldName); $targetMapping = $this->dm->getClassMetadata($referenceMapping['targetDocument']); parent::from($targetMapping->getCollection()); if ($referenceMapping['isOwningSide']) { if ($referenceMapping['storeAs'] !== ClassMetadataInfo::REFERENCE_STORE_AS_ID) { throw MappingException::cannotLookupNonIdReference($this->class->name, $fieldName); } $this->foreignField('_id')->localField($referenceMapping['name']); } else { if (isset($referenceMapping['repositoryMethod'])) { throw MappingException::repositoryMethodLookupNotAllowed($this->class->name, $fieldName); } $mappedByMapping = $targetMapping->getFieldMapping($referenceMapping['mappedBy']); if ($mappedByMapping['storeAs'] !== ClassMetadataInfo::REFERENCE_STORE_AS_ID) { throw MappingException::cannotLookupNonIdReference($this->class->name, $fieldName); } $this->localField('_id')->foreignField($mappedByMapping['name']); } return $this; }
/** * Prepares a query value and converts the php value to the database value if it is an identifier. * It also handles converting $fieldName to the database name if they are different. * * @param string $fieldName * @param string $value * @return mixed $value */ private function prepareQueryValue(&$fieldName, $value) { // Process "association.fieldName" if (strpos($fieldName, '.') !== false) { $e = explode('.', $fieldName); $mapping = $this->class->getFieldMapping($e[0]); if ($this->class->hasField($e[0])) { $name = $this->class->fieldMappings[$e[0]]['name']; if ($name !== $e[0]) { $e[0] = $name; } } if (isset($mapping['targetDocument'])) { $targetClass = $this->dm->getClassMetadata($mapping['targetDocument']); if ($targetClass->hasField($e[1])) { if ($targetClass->identifier === $e[1] || $e[1] === '$id') { $fieldName = $e[0] . '.$id'; $value = $targetClass->getDatabaseIdentifierValue($value); } } } // Process all non identifier fields } elseif ($this->class->hasField($fieldName) && !$this->class->isIdentifier($fieldName)) { $name = $this->class->fieldMappings[$fieldName]['name']; $mapping = $this->class->fieldMappings[$fieldName]; if ($name !== $fieldName) { $fieldName = $name; } // Process identifier } elseif ($fieldName === $this->class->identifier || $fieldName === '_id') { $fieldName = '_id'; $value = $this->class->getDatabaseIdentifierValue($value); } return $value; }
/** * Hydrate array of MongoDB document data into the given document object. * * @param object $document The document object to hydrate the data into. * @param array $data The array of document data. * @return array $values The array of hydrated values. */ public function hydrate($document, $data) { $metadata = $this->dm->getClassMetadata(get_class($document)); // Invoke preLoad lifecycle events and listeners if (isset($metadata->lifecycleCallbacks[Events::preLoad])) { $args = array(&$data); $metadata->invokeLifecycleCallbacks(Events::preLoad, $document, $args); } if ($this->evm->hasListeners(Events::preLoad)) { $this->evm->dispatchEvent(Events::preLoad, new PreLoadEventArgs($document, $this->dm, $data)); } // Use the alsoLoadMethods on the document object to transform the data before hydration if (isset($metadata->alsoLoadMethods)) { foreach ($metadata->alsoLoadMethods as $fieldName => $method) { if (isset($data[$fieldName])) { $document->$method($data[$fieldName]); } } } $data = $this->getHydratorFor($metadata->name)->hydrate($document, $data); // Invoke the postLoad lifecycle callbacks and listeners if (isset($metadata->lifecycleCallbacks[Events::postLoad])) { $metadata->invokeLifecycleCallbacks(Events::postLoad, $document); } if ($this->evm->hasListeners(Events::postLoad)) { $this->evm->dispatchEvent(Events::postLoad, new LifecycleEventArgs($document, $this->dm)); } return $data; }
/** * Prepares a query value and converts the php value to the database value if it is an identifier. * It also handles converting $fieldName to the database name if they are different. * * @param string $fieldName * @param string $value * @return mixed $value */ private function prepareQueryValue(&$fieldName, $value) { // Process "association.fieldName" if (strpos($fieldName, '.') !== false) { $e = explode('.', $fieldName); $mapping = $this->class->getFieldMapping($e[0]); if ($this->class->hasField($e[0])) { $name = $this->class->fieldMappings[$e[0]]['name']; if ($name !== $e[0]) { $e[0] = $name; } } if (isset($mapping['targetDocument'])) { $targetClass = $this->dm->getClassMetadata($mapping['targetDocument']); if ($targetClass->hasField($e[1])) { if ($targetClass->identifier === $e[1]) { $fieldName = $e[0] . '.$id'; if (is_array($value)) { foreach ($value as $k => $v) { $value[$k] = $targetClass->getDatabaseIdentifierValue($v); } } else { $value = $targetClass->getDatabaseIdentifierValue($value); } } } } // Process all non identifier fields // We only change the field names here to the mongodb field name used for persistence } elseif ($this->class->hasField($fieldName) && ! $this->class->isIdentifier($fieldName)) { $name = $this->class->fieldMappings[$fieldName]['name']; $mapping = $this->class->fieldMappings[$fieldName]; if ($name !== $fieldName) { $fieldName = $name; } // Process identifier } elseif (($this->class->hasField($fieldName) && $this->class->isIdentifier($fieldName)) || $fieldName === '_id') { $fieldName = '_id'; if (is_array($value)) { foreach ($value as $k => $v) { if ($k[0] === '$' && is_array($v)) { foreach ($v as $k2 => $v2) { $value[$k][$k2] = $this->class->getDatabaseIdentifierValue($v2); } } else { $value[$k] = $this->class->getDatabaseIdentifierValue($v); } } } else { $value = $this->class->getDatabaseIdentifierValue($value); } } return $value; }
/** * Prepares array of values to be stored in mongo to represent embedded object. * * @param array $embeddedMapping * @param Document $embeddedDocument * @return array */ private function prepareEmbeddedDocValue(array $embeddedMapping, $embeddedDocument) { if (is_array($embeddedDocument) && isset($embeddedDocument['originalObject'])) { $embeddedDocument = $embeddedDocument['originalObject']; } $className = get_class($embeddedDocument); $class = $this->dm->getClassMetadata($className); $embeddedDocumentValue = array(); foreach ($class->fieldMappings as $mapping) { // Skip not saved fields if (isset($mapping['notSaved']) && $mapping['notSaved'] === true) { continue; } $rawValue = $class->getFieldValue($embeddedDocument, $mapping['fieldName']); // Don't store null values unless nullable is specified if ($rawValue === null && $mapping['nullable'] === false) { continue; } if (isset($mapping['embedded']) || isset($mapping['reference'])) { if (isset($mapping['embedded'])) { if ($mapping['type'] == 'many') { $value = array(); foreach ($rawValue as $embeddedDoc) { $value[] = $this->prepareEmbeddedDocValue($mapping, $embeddedDoc); } if (empty($value)) { $value = null; } } elseif ($mapping['type'] == 'one') { $value = $this->prepareEmbeddedDocValue($mapping, $rawValue); } } elseif (isset($mapping['reference'])) { if ($mapping['type'] == 'many') { $value = array(); foreach ($rawValue as $referencedDoc) { $value[] = $this->prepareReferencedDocValue($mapping, $referencedDoc); } if (empty($value)) { $value = null; } } else { $value = $this->prepareReferencedDocValue($mapping, $rawValue); } } } else { $value = Type::getType($mapping['type'])->convertToDatabaseValue($rawValue); } if ($value === null && $mapping['nullable'] === false) { continue; } $embeddedDocumentValue[$mapping['fieldName']] = $value; } if (!isset($embeddedMapping['targetDocument'])) { $discriminatorField = isset($embeddedMapping['discriminatorField']) ? $embeddedMapping['discriminatorField'] : '_doctrine_class_name'; $discriminatorValue = isset($embeddedMapping['discriminatorMap']) ? array_search($class->getName(), $embeddedMapping['discriminatorMap']) : $class->getName(); $embeddedDocumentValue[$discriminatorField] = $discriminatorValue; } return $embeddedDocumentValue; }
/** * Create the document database for a mapped class. * * @param string $documentName * @throws \InvalidArgumentException */ public function createDocumentDatabase($documentName) { $class = $this->dm->getClassMetadata($documentName); if ($class->isMappedSuperclass || $class->isEmbeddedDocument) { throw new \InvalidArgumentException('Cannot delete document indexes for mapped super classes or embedded documents.'); } $this->dm->getDocumentDatabase($documentName)->execute("function() { return true; }"); }
/** * Constructor. * * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher * @param \FOS\CommentBundle\Sorting\SortingFactory $factory * @param \Doctrine\ODM\MongoDB\DocumentManager $dm * @param string $class */ public function __construct(EventDispatcherInterface $dispatcher, SortingFactory $factory, DocumentManager $dm, $class) { parent::__construct($dispatcher, $factory); $this->dm = $dm; $this->repository = $dm->getRepository($class); $metadata = $dm->getClassMetadata($class); $this->class = $metadata->name; }
/** * Executes a query updating the given document. * * @param object $document * @param array $query * @param array $options */ private function executeQuery($document, array $query, array $options) { $className = get_class($document); $class = $this->dm->getClassMetadata($className); $findQuery = $this->getQueryForDocument($class, $document); $collection = $this->dm->getDocumentCollection($className); $collection->update($findQuery, $query, $options); }
/** * @param BaseCommandCursor $cursor * * @return CommandCursor */ private function prepareCursor(BaseCommandCursor $cursor) { $class = null; if ($this->hydrationClass) { $class = $this->dm->getClassMetadata($this->hydrationClass); } return new CommandCursor($cursor, $this->dm->getUnitOfWork(), $class); }
/** * Executes a query updating the given document. * * @param object $document * @param array $query * @param array $options */ private function executeQuery($document, array $query, array $options) { $className = get_class($document); $class = $this->dm->getClassMetadata($className); $id = $class->getDatabaseIdentifierValue($this->uow->getDocumentIdentifier($document)); $collection = $this->dm->getDocumentCollection($className); $collection->update(array('_id' => $id), $query, $options); }
/** * Constructor. * * @param EncoderFactoryInterface $encoderFactory * @param string $algorithm * @param CanonicalizerInterface $usernameCanonicalizer * @param CanonicalizerInterface $emailCanonicalizer * @param DocumentManager $dm * @param string $class */ public function __construct(EncoderFactoryInterface $encoderFactory, $algorithm, CanonicalizerInterface $usernameCanonicalizer, CanonicalizerInterface $emailCanonicalizer, DocumentManager $dm, $class) { parent::__construct($encoderFactory, $algorithm, $usernameCanonicalizer, $emailCanonicalizer); $this->dm = $dm; $this->repository = $dm->getRepository($class); $metadata = $dm->getClassMetadata($class); $this->class = $metadata->name; }
/** * INTERNAL: * Sets the collection's owning entity together with the AssociationMapping that * describes the association between the owner and the elements of the collection. * * @param object $document * @param array $mapping */ public function setOwner($document, array $mapping) { $this->owner = $document; $this->mapping = $mapping; if (!empty($this->mapping['targetDocument'])) { $this->typeClass = $this->dm->getClassMetadata($this->mapping['targetDocument']); } }
/** * 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); } } }
/** * {@inheritdoc} */ protected function createRepository(DocumentManager $documentManager, $documentName) { $metadata = $documentManager->getClassMetadata($documentName); $repository = $metadata->customRepositoryClassName; if ($repository === null) { $repository = $documentManager->getConfiguration()->getDefaultRepositoryClassName(); } return $this->createResourceRepository($repository, $documentManager, $metadata, $this->resolveResource($metadata->getName())); }
/** * Prepares array of values to be stored in mongo to represent embedded object. * * @param array $embeddedMapping * @param Document $embeddedDocument * @return array */ public function prepareEmbeddedDocValue(array $embeddedMapping, $embeddedDocument) { $className = get_class($embeddedDocument); $class = $this->dm->getClassMetadata($className); $embeddedDocumentValue = array(); foreach ($class->fieldMappings as $mapping) { // Skip not saved fields if (isset($mapping['notSaved']) && $mapping['notSaved'] === true) { continue; } $rawValue = $class->getFieldValue($embeddedDocument, $mapping['fieldName']); // Generate embedded document identifiers if ($class->isIdentifier($mapping['fieldName'])) { if ( ! $class->isIdGeneratorNone() && $rawValue === null) { $rawValue = $class->idGenerator->generate($this->dm, $embeddedDocument); $class->setIdentifierValue($embeddedDocument, $rawValue); } $embeddedDocumentValue['_id'] = Type::getType($mapping['type'])->convertToDatabaseValue($rawValue); continue; } // Don't store null values unless nullable is specified if ($rawValue === null && $mapping['nullable'] === false) { continue; } $value = null; if (isset($mapping['embedded']) && $mapping['type'] == 'one') { $value = $this->prepareEmbeddedDocValue($mapping, $rawValue); } elseif (isset($mapping['embedded']) && $mapping['type'] == 'many') { // do nothing for embedded many // CollectionPersister will take care of this } elseif (isset($mapping['reference']) && $mapping['type'] === 'one') { $value = $this->prepareReferencedDocValue($mapping, $rawValue); } else { $value = Type::getType($mapping['type'])->convertToDatabaseValue($rawValue); } if ($value === null && $mapping['nullable'] === false) { continue; } $embeddedDocumentValue[$mapping['name']] = $value; } // Store a discriminator value if the embedded document is not mapped explicitely to a targetDocument if ( ! isset($embeddedMapping['targetDocument'])) { $discriminatorField = isset($embeddedMapping['discriminatorField']) ? $embeddedMapping['discriminatorField'] : '_doctrine_class_name'; $discriminatorValue = isset($embeddedMapping['discriminatorMap']) ? array_search($class->getName(), $embeddedMapping['discriminatorMap']) : $class->getName(); $embeddedDocumentValue[$discriminatorField] = $discriminatorValue; } // Fix so that we can force empty embedded document to store itself as a hash instead of an array if (empty($embeddedDocumentValue)) { return (object) $embeddedDocumentValue; } return $embeddedDocumentValue; }
/** * @param $documentName * * @return array */ private function runShardCollectionCommand($documentName) { $class = $this->dm->getClassMetadata($documentName); $dbName = $this->dm->getDocumentDatabase($documentName)->getName(); $shardKey = $class->getShardKey(); $adminDb = $this->dm->getConnection()->selectDatabase('admin'); $result = $adminDb->command(array('shardCollection' => $dbName . '.' . $class->getCollection(), 'key' => $shardKey['keys']), $shardKey['options']); return $result; }
public function __construct(DocumentManager $dm, $class, $property = null) { $this->class = $class; $this->metadata = $dm->getClassMetadata($class); if (false !== strpos($this->class, ':')) { $this->class = $this->metadata->name; } $this->repository = $dm->getRepository($class); $this->property = $property; }
public function __construct($encoder, $algorithm, DocumentManager $dm, $class) { $this->dm = $dm; $this->repository = $dm->getRepository($class); $metadata = $dm->getClassMetadata($class); $this->class = $metadata->name; parent::__construct($encoder, $algorithm); }
/** * 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); }
/** * Find all common attribute ids with values from a list of product ids * Only exists for ODM repository * * @param array $productIds * * @return array */ protected function findValuesCommonAttributeIds(array $productIds) { $collection = $this->dm->getDocumentCollection($this->documentName); $expr = new Expr($this->dm); $class = $this->dm->getClassMetadata($this->documentName); $expr->setClassMetadata($class); $expr->field('_id')->in($productIds); $pipeline = [['$match' => $expr->getQuery()], ['$unwind' => '$values'], ['$group' => ['_id' => ['id' => '$_id', 'family' => '$family'], 'attribute' => ['$addToSet' => '$values.attribute']]]]; return $collection->aggregate($pipeline)->toArray(); }