Set shard key for this Document.
public setShardKey ( array $keys, array $options = [] ) | ||
$keys | array | Array of document keys. |
$options | array | Array of sharding options. |
/** * @param ClassMetadataInfo $class * @param ODM\ShardKey $shardKey * * @throws MappingException */ private function setShardKey(ClassMetadataInfo $class, ODM\ShardKey $shardKey) { $options = array(); $allowed = array('unique', 'numInitialChunks'); foreach ($allowed as $name) { if (isset($shardKey->{$name})) { $options[$name] = $shardKey->{$name}; } } $class->setShardKey($shardKey->keys, $options); }
private function setShardKey(ClassMetadataInfo $class, \SimpleXmlElement $xmlShardkey) { $attributes = $xmlShardkey->attributes(); $keys = array(); $options = array(); foreach ($xmlShardkey->{'key'} as $key) { $keys[(string) $key['name']] = isset($key['order']) ? (string) $key['order'] : 'asc'; } if (isset($attributes['unique'])) { $options['unique'] = 'true' === (string) $attributes['unique']; } if (isset($attributes['numInitialChunks'])) { $options['numInitialChunks'] = (int) $attributes['numInitialChunks']; } if (isset($xmlShardkey->{'option'})) { foreach ($xmlShardkey->{'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->setShardKey($keys, $options); }
/** * @expectedException \Doctrine\ODM\MongoDB\Mapping\MappingException * @expectedExceptionMessage No multikey indexes are allowed in the shard key */ public function testNoReferenceManyInShardKey() { $cm = new ClassMetadataInfo('stdClass'); $cm->mapManyEmbedded(['fieldName' => 'referenceMany']); $cm->setShardKey(array('referenceMany' => 1)); }
private function setShardKey(ClassMetadataInfo $class, array $shardKey) { $keys = $shardKey['keys']; $options = array(); if (isset($shardKey['options'])) { $allowed = array('unique', 'numInitialChunks'); foreach ($shardKey['options'] as $name => $value) { if (!in_array($name, $allowed, true)) { continue; } $options[$name] = $value; } } $class->setShardKey($keys, $options); }
/** * @expectedException \Doctrine\ODM\MongoDB\Mapping\MappingException * @expectedExceptionMessage Embedded document can't have shard key: stdClass */ public function testEmbeddedDocumentCantHaveShardKey() { $cm = new ClassMetadataInfo('stdClass'); $cm->isEmbeddedDocument = true; $cm->setShardKey(array('id' => 'asc')); }