@@ -167,8 +167,6 @@ public function loadMetadataForClass($className, ClassMetadata $class)
167
167
168
168
private function addFieldMapping (ClassMetadataInfo $ class , $ mapping )
169
169
{
170
- $ keys = null ;
171
-
172
170
if (isset ($ mapping ['name ' ])) {
173
171
$ name = $ mapping ['name ' ];
174
172
} elseif (isset ($ mapping ['fieldName ' ])) {
@@ -181,39 +179,37 @@ private function addFieldMapping(ClassMetadataInfo $class, $mapping)
181
179
// Note: this strategy is not actually used
182
180
$ mapping ['strategy ' ] = isset ($ mapping ['strategy ' ]) ? $ mapping ['strategy ' ] : 'pushAll ' ;
183
181
}
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 ;
188
188
}
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 ' ];
193
195
}
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 ' ];
215
198
}
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 );
217
213
}
218
214
219
215
private function addEmbedMapping (ClassMetadataInfo $ class , $ embed , $ type )
@@ -312,41 +308,47 @@ private function addReferenceMapping(ClassMetadataInfo $class, $reference, $type
312
308
private function addIndex (ClassMetadataInfo $ class , \SimpleXmlElement $ xmlIndex )
313
309
{
314
310
$ 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
+
315
318
$ 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 ' ]);
318
322
}
319
323
if (isset ($ attributes ['drop-dups ' ])) {
320
- $ options ['dropDups ' ] = ((string ) $ attributes ['dropDups ' ] == ' false ' ) ? false : true ;
324
+ $ options ['dropDups ' ] = (' true ' === (string ) $ attributes ['drop-dups ' ]) ;
321
325
}
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 ' ] ;
324
328
}
325
329
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 ' ]);
330
331
}
331
332
if (isset ($ attributes ['sparse ' ])) {
332
- $ options ['sparse ' ] = ((string ) $ attributes ['sparse ' ] == ' false ' ) ? false : true ;
333
+ $ options ['sparse ' ] = (' true ' === (string ) $ attributes ['sparse ' ]) ;
333
334
}
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 ' ]);
340
337
}
338
+
341
339
if (isset ($ xmlIndex ->{'option ' })) {
342
340
foreach ($ xmlIndex ->{'option ' } as $ option ) {
343
341
$ 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 ;
347
348
}
348
349
}
349
- $ class ->addIndex ($ index ['keys ' ], $ index ['options ' ]);
350
+
351
+ $ class ->addIndex ($ keys , $ options );
350
352
}
351
353
352
354
/**
0 commit comments