本文整理汇总了PHP中Doctrine\ODM\MongoDB\Mapping\ClassMetadata类的典型用法代码示例。如果您正苦于以下问题:PHP ClassMetadata类的具体用法?PHP ClassMetadata怎么用?PHP ClassMetadata使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ClassMetadata类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: fromDocument
/**
* @param ClassMetadata $classMetadata
* @return $this
*
* @throws MappingException
*/
private function fromDocument(ClassMetadata $classMetadata)
{
if ($classMetadata->isSharded()) {
throw MappingException::cannotUseShardedCollectionInOutStage($classMetadata->name);
}
return parent::out($classMetadata->getCollection());
}
示例2: fromReference
/**
* @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;
}
示例3: createResourceRepository
/**
* @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());
}
示例4: __construct
/**
* {@inheritdoc}
*/
public function __construct(DocumentManager $dm, UnitOfWork $uow, ClassMetadata $class)
{
if ($class->getReflectionClass()->isSubclassOf('Gedmo\\Translatable\\Document\\MappedSuperclass\\AbstractPersonalTranslation')) {
throw new \Gedmo\Exception\UnexpectedValueException('This repository is useless for personal translations');
}
parent::__construct($dm, $uow, $class);
}
示例5: getQueryArray
protected function getQueryArray(ClassMetadata $metadata, $document, $path)
{
$class = $metadata->name;
$field = $this->getFieldNameFromPropertyPath($path);
if (!isset($metadata->fieldMappings[$field])) {
throw new \LogicException('Mapping for \'' . $path . '\' doesn\'t exist for ' . $class);
}
$mapping = $metadata->fieldMappings[$field];
if (isset($mapping['reference']) && $mapping['reference']) {
throw new \LogicException('Cannot determine uniqueness of referenced document values');
}
switch ($mapping['type']) {
case 'one':
// TODO: implement support for embed one documents
// TODO: implement support for embed one documents
case 'many':
// TODO: implement support for embed many documents
throw new \RuntimeException('Not Implemented.');
case 'hash':
$value = $metadata->getFieldValue($document, $mapping['fieldName']);
return array($path => $this->getFieldValueRecursively($path, $value));
case 'collection':
return array($mapping['fieldName'] => array('$in' => $metadata->getFieldValue($document, $mapping['fieldName'])));
default:
return array($mapping['fieldName'] => $metadata->getFieldValue($document, $mapping['fieldName']));
}
}
示例6: hydrate
/**
* Hydrate array of MongoDB document data into the given document object
* based on the mapping information provided in the ClassMetadata instance.
*
* @param ClassMetadata $metadata The ClassMetadata instance for mapping information.
* @param string $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(ClassMetadata $metadata, $document, $data)
{
$values = array();
foreach ($metadata->fieldMappings as $mapping) {
if (!isset($data[$mapping['fieldName']])) {
continue;
}
if (isset($mapping['embedded'])) {
$embeddedMetadata = $this->_dm->getClassMetadata($mapping['targetDocument']);
$embeddedDocument = $embeddedMetadata->newInstance();
if ($mapping['type'] === 'many') {
$documents = new ArrayCollection();
foreach ($data[$mapping['fieldName']] as $docArray) {
$doc = clone $embeddedDocument;
$this->hydrate($embeddedMetadata, $doc, $docArray);
$documents->add($doc);
}
$metadata->setFieldValue($document, $mapping['fieldName'], $documents);
$value = $documents;
} else {
$value = clone $embeddedDocument;
$this->hydrate($embeddedMetadata, $value, $data[$mapping['fieldName']]);
$metadata->setFieldValue($document, $mapping['fieldName'], $value);
}
} elseif (isset($mapping['reference'])) {
$targetMetadata = $this->_dm->getClassMetadata($mapping['targetDocument']);
$targetDocument = $targetMetadata->newInstance();
$value = isset($data[$mapping['fieldName']]) ? $data[$mapping['fieldName']] : null;
if ($mapping['type'] === 'one' && isset($value['$id'])) {
$id = (string) $value['$id'];
$proxy = $this->_dm->getReference($mapping['targetDocument'], $id);
$metadata->setFieldValue($document, $mapping['fieldName'], $proxy);
} elseif ($mapping['type'] === 'many' && (is_array($value) || $value instanceof Collection)) {
$documents = new PersistentCollection($this->_dm, $targetMetadata, new ArrayCollection());
$documents->setInitialized(false);
foreach ($value as $v) {
$id = (string) $v['$id'];
$proxy = $this->_dm->getReference($mapping['targetDocument'], $id);
$documents->add($proxy);
}
$metadata->setFieldValue($document, $mapping['fieldName'], $documents);
}
} else {
$value = $data[$mapping['fieldName']];
$value = Type::getType($mapping['type'])->convertToPHPValue($value);
$metadata->setFieldValue($document, $mapping['fieldName'], $value);
}
if (isset($value)) {
$values[$mapping['fieldName']] = $value;
}
}
if (isset($data['_id'])) {
$metadata->setIdentifierValue($document, (string) $data['_id']);
}
return $values;
}
示例7: setCustomRepositoryClass
/**
* @param ClassMetadata $metadata
*/
private function setCustomRepositoryClass(ClassMetadata $metadata)
{
try {
$resourceMetadata = $this->resourceRegistry->getByClass($metadata->getName());
} catch (\InvalidArgumentException $exception) {
return;
}
if ($resourceMetadata->hasClass('repository')) {
$metadata->setCustomRepositoryClass($resourceMetadata->getClass('repository'));
}
}
示例8: testCreateQueryBuilderForCollection
public function testCreateQueryBuilderForCollection()
{
$this->classMetadata->expects($this->once())->method('getFieldNames')->will($this->returnValue([]));
$this->classMetadata->expects($this->once())->method('getAssociationTargetClass')->with($this->identicalTo('translations'))->will($this->returnValue($translationClass = TranslationTest::class));
$translationClassMetadata = $this->createClassMetadataMock();
$translationClassMetadata->expects($this->once())->method('getFieldNames')->will($this->returnValue(['locale']));
$this->documentManager->expects($this->once())->method('getClassMetadata')->with($this->identicalTo($translationClass))->will($this->returnValue($translationClassMetadata));
$this->documentManager->expects($this->once())->method('createQueryBuilder')->will($this->returnValue($queryBuilder = $this->createQueryBuilderMock()));
$queryBuilder->expects($this->once())->method('field')->with($this->identicalTo('translations.locale'))->will($this->returnSelf());
$this->localeContext->expects($this->once())->method('getLocales')->will($this->returnValue($locales = ['fr']));
$this->localeContext->expects($this->once())->method('getFallbackLocale')->will($this->returnValue($fallbackLocale = 'en'));
$queryBuilder->expects($this->once())->method('in')->with($this->identicalTo(array_merge($locales, [$fallbackLocale])))->will($this->returnSelf());
$this->assertSame($queryBuilder, $this->translatableRepository->createQueryBuilderForCollection());
}
示例9: prepareQueryValue
/**
* 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;
}
示例10: getReferenceMapping
/**
* 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;
}
示例11: prepareQueryValue
/**
* 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]);
$name = $mapping['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';
if (is_array($value)) {
foreach ($value as $k => $v) {
$value[$k] = $targetClass->getDatabaseIdentifierValue($v);
}
} else {
$value = $targetClass->getDatabaseIdentifierValue($value);
}
} else {
$targetMapping = $targetClass->getFieldMapping($e[1]);
$targetName = $targetMapping['name'];
if ($targetName !== $e[1]) {
$e[1] = $targetName;
}
}
$fieldName = $e[0] . '.' . $e[1];
}
}
// 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;
}
示例12: testGetMetadataForSingleClass
public function testGetMetadataForSingleClass()
{
// Self-made metadata
$cm1 = new ClassMetadata('Doctrine\\ODM\\MongoDB\\Tests\\Mapping\\TestDocument1');
$cm1->setCollection('group');
// Add a mapped field
$cm1->mapField(array('fieldName' => 'name', 'type' => 'string'));
// Add a mapped field
$cm1->mapField(array('fieldName' => 'id', 'id' => true));
// and a mapped association
$cm1->mapOneEmbedded(array('fieldName' => 'other', 'targetDocument' => 'Other'));
$cm1->mapOneEmbedded(array('fieldName' => 'association', 'targetDocument' => 'Other'));
// SUT
$cmf = new ClassMetadataFactoryTestSubject();
$cmf->setMetadataFor('Doctrine\\ODM\\MongoDB\\Tests\\Mapping\\TestDocument1', $cm1);
// Prechecks
$this->assertEquals(array(), $cm1->parentClasses);
$this->assertEquals(ClassMetadata::INHERITANCE_TYPE_NONE, $cm1->inheritanceType);
$this->assertTrue($cm1->hasField('name'));
$this->assertEquals(4, count($cm1->fieldMappings));
// Go
$cm1 = $cmf->getMetadataFor('Doctrine\\ODM\\MongoDB\\Tests\\Mapping\\TestDocument1');
$this->assertEquals('group', $cm1->collection);
$this->assertEquals(array(), $cm1->parentClasses);
$this->assertTrue($cm1->hasField('name'));
}
示例13: includesReferenceTo
/**
* Checks that the current field includes a reference to the supplied document.
*/
public function includesReferenceTo($document)
{
if ($this->currentField) {
$this->query[$this->currentField][$this->cmd . 'elemMatch'] = $this->class ? $this->dm->createDBRef($document, $this->class->getFieldMapping($this->currentField)) : $this->dm->createDBRef($document);
} else {
$this->query[$this->cmd . 'elemMatch'] = $this->dm->createDBRef($document);
}
return $this;
}
示例14: getId
/** {@inheritdoc} */
public function getId($object)
{
if (!is_a($object, $this->getClass())) {
throw new MetadataException("Object isn't subclass of '{$this->getClass()}', given '" . (is_object($object) ? get_class($object) : gettype($object)) . "'.");
}
if ($this->classMetadata->isEmbeddedDocument) {
throw new MetadataException("Embedded document '{$this->getClass()}' hasn't ID.");
}
$identifier = $this->classMetadata->getIdentifierValue($object);
return empty($identifier) ? NULL : $identifier;
}
示例15: getFieldsMetadata
public function getFieldsMetadata($class)
{
$result = array();
foreach ($this->odmMetadata->getReflectionProperties() as $property) {
$name = $property->getName();
$mapping = $this->odmMetadata->getFieldMapping($name);
$values = array('title' => $name, 'source' => true);
if (isset($mapping['fieldName'])) {
$values['field'] = $mapping['fieldName'];
}
if (isset($mapping['id']) && $mapping['id'] == 'id') {
$values['primary'] = true;
}
switch ($mapping['type']) {
case 'id':
case 'int':
case 'string':
case 'float':
case 'many':
$values['type'] = 'text';
break;
case 'boolean':
$values['type'] = 'boolean';
break;
case 'date':
$values['type'] = 'date';
break;
}
$result[$name] = $values;
}
return $result;
}