private function addIndex(ClassMetadataInfo $class, $index, array $keys = array()) { $keys = array_merge($keys, $index->keys); $options = array(); $allowed = array('name', 'dropDups', 'background', 'safe', 'unique', 'sparse', 'expireAfterSeconds'); foreach ($allowed as $name) { if (isset($index->{$name})) { $options[$name] = $index->{$name}; } } $options = array_merge($options, $index->options); $class->addIndex($keys, $options); }
private function addFieldMapping(ClassMetadataInfo $class, $mapping) { if (isset($mapping['name'])) { $name = $mapping['name']; } elseif (isset($mapping['fieldName'])) { $name = $mapping['fieldName']; } else { throw new \InvalidArgumentException('Cannot infer a MongoDB name from the mapping'); } $class->mapField($mapping); if (!(isset($mapping['index']) || isset($mapping['unique']) || isset($mapping['sparse']))) { return; } // Multiple index specifications in one field mapping is ambiguous if ((isset($mapping['index']) && is_array($mapping['index'])) + (isset($mapping['unique']) && is_array($mapping['unique'])) + (isset($mapping['sparse']) && is_array($mapping['sparse'])) > 1) { throw new \InvalidArgumentException('Multiple index specifications found among index, unique, and/or sparse fields'); } // Index this field if either "index", "unique", or "sparse" are set $keys = array($name => 'asc'); /* The "order" option is only used in the index specification and should * not be passed along as an index option. */ if (isset($mapping['index']['order'])) { $keys[$name] = $mapping['index']['order']; unset($mapping['index']['order']); } elseif (isset($mapping['unique']['order'])) { $keys[$name] = $mapping['unique']['order']; unset($mapping['unique']['order']); } elseif (isset($mapping['sparse']['order'])) { $keys[$name] = $mapping['sparse']['order']; unset($mapping['sparse']['order']); } /* Initialize $options from any array value among index, unique, and * sparse. Any boolean values for unique or sparse should be merged into * the options afterwards to ensure consistent parsing. */ $options = array(); $unique = null; $sparse = null; if (isset($mapping['index']) && is_array($mapping['index'])) { $options = $mapping['index']; } if (isset($mapping['unique'])) { if (is_array($mapping['unique'])) { $options = $mapping['unique'] + array('unique' => true); } else { $unique = (bool) $mapping['unique']; } } if (isset($mapping['sparse'])) { if (is_array($mapping['sparse'])) { $options = $mapping['sparse'] + array('sparse' => true); } else { $sparse = (bool) $mapping['sparse']; } } if (isset($unique)) { $options['unique'] = $unique; } if (isset($sparse)) { $options['sparse'] = $sparse; } $class->addIndex($keys, $options); }
/** * {@inheritdoc} */ public function loadMetadataForClass($className, ClassMetadataInfo $class) { $element = $this->getElement($className); if ( ! $element) { return; } $element['type'] = isset($element['type']) ? $element['type'] : 'document'; if (isset($element['db'])) { $class->setDatabase($element['db']); } if (isset($element['collection'])) { $class->setCollection($element['collection']); } if ($element['type'] == 'document') { if (isset($element['repositoryClass'])) { $class->setCustomRepositoryClass($element['repositoryClass']); } } elseif ($element['type'] === 'mappedSuperclass') { $class->isMappedSuperclass = true; } elseif ($element['type'] === 'embeddedDocument') { $class->isEmbeddedDocument = true; } if (isset($element['indexes'])) { foreach($element['indexes'] as $index) { $class->addIndex($index['keys'], isset($index['options']) ? $index['options'] : array()); } } if (isset($element['inheritanceType'])) { $class->setInheritanceType(constant('Doctrine\ODM\MongoDB\Mapping\ClassMetadata::INHERITANCE_TYPE_' . strtoupper($element['inheritanceType']))); } if (isset($element['discriminatorField'])) { $discrField = $element['discriminatorField']; $class->setDiscriminatorField(array( 'name' => $discrField['name'], 'fieldName' => $discrField['fieldName'] )); } if (isset($element['discriminatorMap'])) { $class->setDiscriminatorMap($element['discriminatorMap']); } if (isset($element['changeTrackingPolicy'])) { $class->setChangeTrackingPolicy(constant('Doctrine\ODM\MongoDB\Mapping\ClassMetadata::CHANGETRACKING_' . strtoupper($element['changeTrackingPolicy']))); } if (isset($element['fields'])) { foreach ($element['fields'] as $fieldName => $mapping) { if (is_string($mapping)) { $type = $mapping; $mapping = array(); $mapping['type'] = $type; } if ( ! isset($mapping['fieldName'])) { $mapping['fieldName'] = $fieldName; } if (isset($mapping['type']) && $mapping['type'] === 'collection') { $mapping['strategy'] = isset($mapping['strategy']) ? $mapping['strategy'] : 'pushAll'; } $this->addFieldMapping($class, $mapping); } } if (isset($element['embedOne'])) { foreach ($element['embedOne'] as $fieldName => $embed) { $this->addMappingFromEmbed($class, $fieldName, $embed, 'one'); } } if (isset($element['embedMany'])) { foreach ($element['embedMany'] as $fieldName => $embed) { $this->addMappingFromEmbed($class, $fieldName, $embed, 'many'); } } if (isset($element['referenceOne'])) { foreach ($element['referenceOne'] as $fieldName => $reference) { $this->addMappingFromReference($class, $fieldName, $reference, 'one'); } } if (isset($element['referenceMany'])) { foreach ($element['referenceMany'] as $fieldName => $reference) { $this->addMappingFromReference($class, $fieldName, $reference, 'many'); } } if (isset($element['lifecycleCallbacks'])) { foreach ($element['lifecycleCallbacks'] as $type => $methods) { foreach ($methods as $method) { $class->addLifecycleCallback($method, constant('Doctrine\ODM\MongoDB\Events::' . $type)); } } } }
private function addIndex(ClassMetadataInfo $class, \SimpleXmlElement $xmlIndex) { $attributes = $xmlIndex->attributes(); $keys = array(); foreach ($xmlIndex->{'key'} as $key) { $keys[(string) $key['name']] = isset($key['order']) ? (string) $key['order'] : 'asc'; } $options = array(); if (isset($attributes['background'])) { $options['background'] = 'true' === (string) $attributes['background']; } if (isset($attributes['drop-dups'])) { $options['dropDups'] = 'true' === (string) $attributes['drop-dups']; } if (isset($attributes['name'])) { $options['name'] = (string) $attributes['name']; } if (isset($attributes['safe'])) { $options['safe'] = 'true' === (string) $attributes['safe']; } if (isset($attributes['sparse'])) { $options['sparse'] = 'true' === (string) $attributes['sparse']; } if (isset($attributes['unique'])) { $options['unique'] = 'true' === (string) $attributes['unique']; } if (isset($xmlIndex->{'option'})) { foreach ($xmlIndex->{'option'} as $option) { $value = (string) $option['value']; if ($value === 'true') { $value = true; } elseif ($value === 'false') { $value = false; } elseif (is_numeric($value)) { $value = preg_match('/^[-]?\\d+$/', $value) ? (int) $value : (double) $value; } $options[(string) $option['name']] = $value; } } $class->addIndex($keys, $options); }
private function addFieldMapping(ClassMetadataInfo $class, $mapping) { $keys = null; if (isset($mapping['name'])) { $name = $mapping['name']; } elseif (isset($mapping['fieldName'])) { $name = $mapping['fieldName']; } else { throw new \InvalidArgumentException('Cannot infer a MongoDB name from the mapping'); } if (isset($mapping['index'])) { $keys = array($name => isset($mapping['index']['order']) ? $mapping['index']['order'] : 'asc'); } if (isset($mapping['unique'])) { $keys = array($name => isset($mapping['unique']['order']) ? $mapping['unique']['order'] : 'asc'); } if ($keys !== null) { $options = array(); if (isset($mapping['index'])) { $options = $mapping['index']; } elseif (isset($mapping['unique'])) { $options = $mapping['unique']; $options['unique'] = true; } elseif (isset($mapping['sparse'])) { $options = $mapping['sparse']; $options['sparse'] = true; } $class->addIndex($keys, $options); } $class->mapField($mapping); }
private function addFieldMapping(ClassMetadataInfo $class, $mapping) { $keys = null; $name = isset($mapping['name']) ? $mapping['name'] : $mapping['fieldName']; if (isset($mapping['index'])) { $keys = array($name => isset($mapping['index']['order']) ? $mapping['index']['order'] : 'asc'); } if (isset($mapping['unique'])) { $keys = array($name => isset($mapping['unique']['order']) ? $mapping['unique']['order'] : 'asc'); } if ($keys !== null) { $options = array(); if (isset($mapping['index'])) { $options = $mapping['index']; } elseif (isset($mapping['unique'])) { $options = $mapping['unique']; $options['unique'] = true; } $class->addIndex($keys, $options); } $class->mapField($mapping); }
private function addIndex(ClassMetadataInfo $class, SimpleXmlElement $xmlIndex) { $attributes = $xmlIndex->attributes(); $options = array(); if (isset($attributes['name'])) { $options['name'] = (string) $attributes['name']; } if (isset($attributes['drop-dups'])) { $options['dropDups'] = ((string) $attributes['dropDups'] == 'false') ? false : true; } if (isset($attributes['background'])) { $options['background'] = ((string) $attributes['background'] == 'false') ? false : true; } if (isset($attributes['safe'])) { $options['safe'] = ((string) $attributes['safe'] == 'false') ? false : true; } if (isset($attributes['unique'])) { $options['unique'] = ((string) $attributes['unique'] == 'false') ? false : true; } $index = array( 'keys' => array(), 'options' => $options ); foreach ($xmlIndex->{'key'} as $key) { $index['keys'][(string) $key['name']] = isset($key['order']) ? (string) $key['order'] : 'asc'; } if (isset($xmlIndex->{'option'})) { foreach ($xmlIndex->{'option'} as $option) { $value = (string) $option['value']; $value = $value === 'true' ? true : $value; $value = $value === 'false' ? false : $value; $index['options'][(string) $option['name']] = $value; } } $class->addIndex($index['keys'], $index['options']); }
private function addIndex(ClassMetadataInfo $class, \SimpleXmlElement $xmlIndex) { $attributes = $xmlIndex->attributes(); $keys = array(); foreach ($xmlIndex->{'key'} as $key) { $keys[(string) $key['name']] = isset($key['order']) ? (string) $key['order'] : 'asc'; } $options = array(); if (isset($attributes['background'])) { $options['background'] = 'true' === (string) $attributes['background']; } if (isset($attributes['drop-dups'])) { $options['dropDups'] = 'true' === (string) $attributes['drop-dups']; } if (isset($attributes['name'])) { $options['name'] = (string) $attributes['name']; } if (isset($attributes['safe'])) { $options['safe'] = 'true' === (string) $attributes['safe']; } if (isset($attributes['sparse'])) { $options['sparse'] = 'true' === (string) $attributes['sparse']; } if (isset($attributes['unique'])) { $options['unique'] = 'true' === (string) $attributes['unique']; } if (isset($xmlIndex->{'option'})) { foreach ($xmlIndex->{'option'} as $option) { $value = (string) $option['value']; if ($value === 'true') { $value = true; } elseif ($value === 'false') { $value = false; } elseif (is_numeric($value)) { $value = preg_match('/^[-]?\\d+$/', $value) ? (int) $value : (double) $value; } $options[(string) $option['name']] = $value; } } if (isset($xmlIndex->{'partial-filter-expression'})) { $partialFilterExpressionMapping = $xmlIndex->{'partial-filter-expression'}; if (isset($partialFilterExpressionMapping->and)) { foreach ($partialFilterExpressionMapping->and as $and) { if (!isset($and->field)) { continue; } $partialFilterExpression = $this->getPartialFilterExpression($and->field); if (!$partialFilterExpression) { continue; } $options['partialFilterExpression']['$and'][] = $partialFilterExpression; } } elseif (isset($partialFilterExpressionMapping->field)) { $partialFilterExpression = $this->getPartialFilterExpression($partialFilterExpressionMapping->field); if ($partialFilterExpression) { $options['partialFilterExpression'] = $partialFilterExpression; } } } $class->addIndex($keys, $options); }
private function addFieldMapping(ClassMetadataInfo $class, $mapping) { if (isset($mapping['name'])) { $name = $mapping['name']; } elseif (isset($mapping['fieldName'])) { $name = $mapping['fieldName']; } else { throw new \InvalidArgumentException('Cannot infer a MongoDB name from the mapping'); } $class->mapField($mapping); if (!(isset($mapping['index']) || isset($mapping['unique']) || isset($mapping['sparse']))) { return; } // Index this field if either "index", "unique", or "sparse" are set $keys = array($name => 'asc'); if (isset($mapping['index']['order'])) { $keys[$name] = $mapping['index']['order']; unset($mapping['index']['order']); } elseif (isset($mapping['unique']['order'])) { $keys[$name] = $mapping['unique']['order']; unset($mapping['unique']['order']); } elseif (isset($mapping['sparse']['order'])) { $keys[$name] = $mapping['sparse']['order']; unset($mapping['sparse']['order']); } $options = array(); if (isset($mapping['index'])) { $options = is_array($mapping['index']) ? $mapping['index'] : array(); } elseif (isset($mapping['unique'])) { $options = is_array($mapping['unique']) ? $mapping['unique'] : array(); $options['unique'] = true; } elseif (isset($mapping['sparse'])) { $options = is_array($mapping['sparse']) ? $mapping['sparse'] : array(); $options['sparse'] = true; } $class->addIndex($keys, $options); }
/** * @param ClassMetadataInfo $metadata * @param string $name * @param array $index */ protected function addIndex(ClassMetadataInfo $metadata, $name, array $index) { if (empty($index['keys'])) { return; } $keys = $index['keys']; unset($index['keys']); $options = array_merge($index, ['name' => $name]); $metadata->addIndex($keys, $options); }