Skip to content

Commit 9983a15

Browse files
committed
Refactor XML index parsing (make field/index tags consistent)
Ensure that we're parsing the same attributes in the XSD. Make boolean checks consistent with other parsing (check for "true" instead of "false").
1 parent 30a890b commit 9983a15

File tree

4 files changed

+61
-55
lines changed

4 files changed

+61
-55
lines changed

lib/Doctrine/ODM/MongoDB/Mapping/Driver/XmlDriver.php

Lines changed: 54 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,6 @@ public function loadMetadataForClass($className, ClassMetadata $class)
167167

168168
private function addFieldMapping(ClassMetadataInfo $class, $mapping)
169169
{
170-
$keys = null;
171-
172170
if (isset($mapping['name'])) {
173171
$name = $mapping['name'];
174172
} elseif (isset($mapping['fieldName'])) {
@@ -181,39 +179,37 @@ private function addFieldMapping(ClassMetadataInfo $class, $mapping)
181179
// Note: this strategy is not actually used
182180
$mapping['strategy'] = isset($mapping['strategy']) ? $mapping['strategy'] : 'pushAll';
183181
}
184-
if (isset($mapping['index'])) {
185-
$keys = array(
186-
$name => isset($mapping['order']) ? $mapping['order'] : 'asc'
187-
);
182+
183+
$class->mapField($mapping);
184+
185+
// Index this field if either "index", "unique", or "sparse" are set
186+
if ( ! (isset($mapping['index']) || isset($mapping['unique']) || isset($mapping['sparse']))) {
187+
return;
188188
}
189-
if (isset($mapping['unique'])) {
190-
$keys = array(
191-
$name => isset($mapping['order']) ? $mapping['order'] : 'asc'
192-
);
189+
190+
$keys = array($name => isset($mapping['order']) ? $mapping['order'] : 'asc');
191+
$options = array();
192+
193+
if (isset($mapping['background'])) {
194+
$options['background'] = (boolean) $mapping['background'];
193195
}
194-
if ($keys !== null) {
195-
$options = array();
196-
if (isset($mapping['index-name'])) {
197-
$options['name'] = (string) $mapping['index-name'];
198-
}
199-
if (isset($mapping['drop-dups'])) {
200-
$options['dropDups'] = (boolean) $mapping['drop-dups'];
201-
}
202-
if (isset($mapping['background'])) {
203-
$options['background'] = (boolean) $mapping['background'];
204-
}
205-
if (isset($mapping['safe'])) {
206-
$options['safe'] = (boolean) $mapping['safe'];
207-
}
208-
if (isset($mapping['unique'])) {
209-
$options['unique'] = (boolean) $mapping['unique'];
210-
}
211-
if (isset($mapping['sparse'])) {
212-
$options['sparse'] = (boolean) $mapping['sparse'];
213-
}
214-
$class->addIndex($keys, $options);
196+
if (isset($mapping['drop-dups'])) {
197+
$options['dropDups'] = (boolean) $mapping['drop-dups'];
215198
}
216-
$class->mapField($mapping);
199+
if (isset($mapping['index-name'])) {
200+
$options['name'] = (string) $mapping['index-name'];
201+
}
202+
if (isset($mapping['safe'])) {
203+
$options['safe'] = (boolean) $mapping['safe'];
204+
}
205+
if (isset($mapping['sparse'])) {
206+
$options['sparse'] = (boolean) $mapping['sparse'];
207+
}
208+
if (isset($mapping['unique'])) {
209+
$options['unique'] = (boolean) $mapping['unique'];
210+
}
211+
212+
$class->addIndex($keys, $options);
217213
}
218214

219215
private function addEmbedMapping(ClassMetadataInfo $class, $embed, $type)
@@ -312,41 +308,47 @@ private function addReferenceMapping(ClassMetadataInfo $class, $reference, $type
312308
private function addIndex(ClassMetadataInfo $class, \SimpleXmlElement $xmlIndex)
313309
{
314310
$attributes = $xmlIndex->attributes();
311+
312+
$keys = array();
313+
314+
foreach ($xmlIndex->{'key'} as $key) {
315+
$keys[(string) $key['name']] = isset($key['order']) ? (string) $key['order'] : 'asc';
316+
}
317+
315318
$options = array();
316-
if (isset($attributes['name'])) {
317-
$options['name'] = (string) $attributes['name'];
319+
320+
if (isset($attributes['background'])) {
321+
$options['background'] = ('true' === (string) $attributes['background']);
318322
}
319323
if (isset($attributes['drop-dups'])) {
320-
$options['dropDups'] = ((string) $attributes['dropDups'] == 'false') ? false : true;
324+
$options['dropDups'] = ('true' === (string) $attributes['drop-dups']);
321325
}
322-
if (isset($attributes['background'])) {
323-
$options['background'] = ((string) $attributes['background'] == 'false') ? false : true;
326+
if (isset($attributes['name'])) {
327+
$options['name'] = (string) $attributes['name'];
324328
}
325329
if (isset($attributes['safe'])) {
326-
$options['safe'] = ((string) $attributes['safe'] == 'false') ? false : true;
327-
}
328-
if (isset($attributes['unique'])) {
329-
$options['unique'] = ((string) $attributes['unique'] == 'false') ? false : true;
330+
$options['safe'] = ('true' === (string) $attributes['safe']);
330331
}
331332
if (isset($attributes['sparse'])) {
332-
$options['sparse'] = ((string) $attributes['sparse'] == 'false') ? false : true;
333+
$options['sparse'] = ('true' === (string) $attributes['sparse']);
333334
}
334-
$index = array(
335-
'keys' => array(),
336-
'options' => $options
337-
);
338-
foreach ($xmlIndex->{'key'} as $key) {
339-
$index['keys'][(string) $key['name']] = isset($key['order']) ? (string) $key['order'] : 'asc';
335+
if (isset($attributes['unique'])) {
336+
$options['unique'] = ('true' === (string) $attributes['unique']);
340337
}
338+
341339
if (isset($xmlIndex->{'option'})) {
342340
foreach ($xmlIndex->{'option'} as $option) {
343341
$value = (string) $option['value'];
344-
$value = $value === 'true' ? true : $value;
345-
$value = $value === 'false' ? false : $value;
346-
$index['options'][(string) $option['name']] = $value;
342+
if ($value === 'true') {
343+
$value = true;
344+
} elseif ($value === 'false') {
345+
$value = false;
346+
}
347+
$options[(string) $option['name']] = $value;
347348
}
348349
}
349-
$class->addIndex($index['keys'], $index['options']);
350+
351+
$class->addIndex($keys, $options);
350352
}
351353

352354
/**

tests/Doctrine/ODM/MongoDB/Tests/Mapping/AbstractMappingDriverTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,9 @@ public function testIndexes($class)
249249
$this->assertTrue(isset($class->indexes[0]['keys']['username']));
250250
$this->assertEquals(-1, $class->indexes[0]['keys']['username']);
251251
$this->assertTrue(isset($class->indexes[0]['options']['unique']));
252+
$this->assertEquals(true, $class->indexes[0]['options']['unique']);
253+
$this->assertTrue(isset($class->indexes[0]['options']['dropDups']));
254+
$this->assertEquals(false, $class->indexes[0]['options']['dropDups']);
252255

253256
$this->assertTrue(isset($class->indexes[1]['keys']['email']));
254257
$this->assertEquals(-1, $class->indexes[1]['keys']['email']);
@@ -285,7 +288,7 @@ class AbstractMappingDriverUser
285288

286289
/**
287290
* @ODM\String(name="username")
288-
* @ODM\Index(order="desc")
291+
* @ODM\UniqueIndex(order="desc", dropDups=false)
289292
*/
290293
public $name;
291294

@@ -426,7 +429,7 @@ public static function loadMetadata(ClassMetadata $metadata)
426429
'work' => 'WorkPhonenumber',
427430
),
428431
));
429-
$metadata->addIndex(array('username' => 'desc'), array('unique' => true));
432+
$metadata->addIndex(array('username' => 'desc'), array('unique' => true, 'dropDups' => false));
430433
$metadata->addIndex(array('email' => 'desc'), array('unique' => true, 'dropDups' => true));
431434
$metadata->addIndex(array('mysqlProfileId' => 'desc'), array('unique' => true, 'dropDups' => true));
432435
}

tests/Doctrine/ODM/MongoDB/Tests/Mapping/xml/Doctrine.ODM.MongoDB.Tests.Mapping.AbstractMappingDriverUser.dcm.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<indexes>
1818
<index unique="true">
1919
<key name="username" order="desc" />
20-
<option name="dropDups" value="true" />
20+
<option name="dropDups" value="false" />
2121
</index>
2222
</indexes>
2323
<reference-one target-document="Address" field="address">

tests/Doctrine/ODM/MongoDB/Tests/Mapping/yaml/Doctrine.ODM.MongoDB.Tests.Mapping.AbstractMappingDriverUser.dcm.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Doctrine\ODM\MongoDB\Tests\Mapping\AbstractMappingDriverUser:
2525
username: desc
2626
options:
2727
unique: true
28+
dropDups: false
2829
index2:
2930
keys:
3031
email: desc

0 commit comments

Comments
 (0)