Completed
Push — master ( ffd146...ab4b76 )
by Marco
17s
created

ClassMetadataInfo   D

Complexity

Total Complexity 419

Size/Duplication

Total Lines 3343
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 89.26%

Importance

Changes 11
Bugs 1 Features 0
Metric Value
wmc 419
c 11
b 1
f 0
lcom 1
cbo 10
dl 0
loc 3343
ccs 806
cts 903
cp 0.8926
rs 4.4102

136 Methods

Rating   Name   Duplication   Size   Complexity  
A getSingleIdReflectionProperty() 0 8 2
A __construct() 0 7 2
A getReflectionProperties() 0 4 1
A getReflectionProperty() 0 4 1
B getIdentifierValues() 0 25 5
A setIdentifierValues() 0 6 2
A setFieldValue() 0 4 1
A getFieldValue() 0 4 1
A __toString() 0 4 1
F __sleep() 0 94 18
A newInstance() 0 4 1
C wakeupReflection() 0 52 11
A initializeReflection() 0 11 2
B validateIdentifier() 0 15 6
A validateAssociations() 0 8 3
A validateLifecycleCallbacks() 0 10 4
A getReflectionClass() 0 4 1
A enableCache() 0 12 3
A enableAssociationCache() 0 4 1
A getAssociationCacheDefaults() 0 14 4
A setChangeTrackingPolicy() 0 4 1
A isChangeTrackingDeferredExplicit() 0 4 1
A isChangeTrackingDeferredImplicit() 0 4 1
A isChangeTrackingNotify() 0 4 1
A isIdentifier() 0 12 3
A isUniqueField() 0 10 3
A isNullable() 0 10 3
A getColumnName() 0 6 2
A getFieldMapping() 0 8 2
A getAssociationMapping() 0 8 2
A getAssociationMappings() 0 4 1
A getFieldName() 0 6 2
A getNamedQuery() 0 8 2
A getNamedQueries() 0 4 1
A getNamedNativeQuery() 0 8 2
A getNamedNativeQueries() 0 4 1
A getSqlResultSetMapping() 0 8 2
A getSqlResultSetMappings() 0 4 1
C _validateAndCompleteFieldMapping() 0 54 19
F _validateAndCompleteAssociationMapping() 0 110 34
D _validateAndCompleteOneToOneMapping() 0 82 23
A _validateAndCompleteOneToManyMapping() 0 16 4
D _validateAndCompleteManyToManyMapping() 0 96 24
A getIdentifierFieldNames() 0 4 1
A getSingleIdentifierFieldName() 0 8 2
A getSingleIdentifierColumnName() 0 4 1
A setIdentifier() 0 5 1
A getIdentifier() 0 4 1
A hasField() 0 4 1
A getColumnNames() 0 8 2
A getIdentifierColumnNames() 0 20 3
A setIdGeneratorType() 0 4 1
A usesIdGenerator() 0 4 1
A isInheritanceTypeNone() 0 4 1
A isInheritanceTypeJoined() 0 4 1
A isInheritanceTypeSingleTable() 0 4 1
A isInheritanceTypeTablePerClass() 0 4 1
A isIdGeneratorIdentity() 0 4 1
A isIdGeneratorSequence() 0 4 1
A isIdGeneratorTable() 0 4 1
A isIdentifierNatural() 0 4 1
A isIdentifierUuid() 0 4 1
A getTypeOfField() 0 6 2
A getTypeOfColumn() 0 4 1
A getTableName() 0 4 1
A getSchemaName() 0 4 2
A getTemporaryIdTableName() 0 5 1
A setSubclasses() 0 6 2
A setParentClasses() 0 8 2
A setInheritanceType() 0 8 2
D setAssociationOverride() 0 43 9
B setAttributeOverride() 0 32 6
A isInheritedField() 0 4 1
A isRootEntity() 0 4 1
A isInheritedAssociation() 0 4 1
A isInheritedEmbeddedClass() 0 4 1
A setTableName() 0 4 1
C setPrimaryTable() 0 32 8
A _isInheritanceType() 0 7 4
A mapField() 0 7 1
A addInheritedAssociationMapping() 0 7 2
A addInheritedFieldMapping() 0 6 1
B addNamedQuery() 0 24 4
C addNamedNativeQuery() 0 33 8
C addSqlResultSetMapping() 0 50 12
A mapOneToOne() 0 8 1
A mapOneToMany() 0 8 1
A mapManyToOne() 0 9 1
A mapManyToMany() 0 8 1
A _storeAssociationMapping() 0 8 1
A setCustomRepositoryClass() 0 4 1
A invokeLifecycleCallbacks() 0 6 2
A hasLifecycleCallbacks() 0 4 1
A getLifecycleCallbacks() 0 4 2
A addLifecycleCallback() 0 8 3
A setLifecycleCallbacks() 0 4 1
B addEntityListener() 0 23 5
C setDiscriminatorColumn() 0 26 7
A setDiscriminatorMap() 0 6 2
B addDiscriminatorMapClass() 0 21 6
A hasNamedQuery() 0 4 1
A hasNamedNativeQuery() 0 4 1
A hasSqlResultSetMapping() 0 4 1
A hasAssociation() 0 4 1
A isSingleValuedAssociation() 0 5 2
A isCollectionValuedAssociation() 0 5 2
A isAssociationWithSingleJoinColumn() 0 6 3
A getSingleAssociationJoinColumnName() 0 8 2
A getSingleAssociationReferencedJoinColumnName() 0 8 2
B getFieldForColumn() 0 16 5
A setIdGenerator() 0 4 1
A setCustomGeneratorDefinition() 0 4 1
A setSequenceGeneratorDefinition() 0 13 3
A setVersionMapping() 0 15 4
A setVersioned() 0 4 1
A setVersionField() 0 4 1
A markReadOnly() 0 4 1
A getFieldNames() 0 4 1
A getAssociationNames() 0 4 1
A getAssociationTargetClass() 0 8 2
A getName() 0 4 1
B getQuotedIdentifierColumnNames() 0 29 5
A getQuotedColumnName() 0 6 2
A getQuotedTableName() 0 6 2
A getQuotedJoinTableName() 0 6 2
A isAssociationInverseSide() 0 5 2
A getAssociationMappedByTargetField() 0 4 1
A getAssociationsByTargetClass() 0 12 3
B fullyQualifiedClassName() 0 12 5
A getMetadataValue() 0 9 2
A mapEmbedded() 0 11 3
C inlineEmbeddable() 0 29 7
A assertFieldNotMapped() 0 9 4
A getSequenceName() 0 8 1
A getSequencePrefix() 0 16 4
A assertMappingOrderBy() 0 6 3

How to fix   Complexity   

Complex Class

Complex classes like ClassMetadataInfo often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ClassMetadataInfo, and based on these observations, apply Extract Interface, too.

1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\ORM\Mapping;
21
22
use BadMethodCallException;
23
use Doctrine\Instantiator\Instantiator;
24
use InvalidArgumentException;
25
use RuntimeException;
26
use Doctrine\DBAL\Types\Type;
27
use Doctrine\DBAL\Platforms\AbstractPlatform;
28
use ReflectionClass;
29
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Doctrine\ORM\Mapping\ClassMetadata.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
30
use Doctrine\Common\ClassLoader;
31
use Doctrine\ORM\Cache\CacheException;
32
33
/**
34
 * A <tt>ClassMetadata</tt> instance holds all the object-relational mapping metadata
35
 * of an entity and its associations.
36
 *
37
 * Once populated, ClassMetadata instances are usually cached in a serialized form.
38
 *
39
 * <b>IMPORTANT NOTE:</b>
40
 *
41
 * The fields of this class are only public for 2 reasons:
42
 * 1) To allow fast READ access.
43
 * 2) To drastically reduce the size of a serialized instance (private/protected members
44
 *    get the whole class name, namespace inclusive, prepended to every property in
45
 *    the serialized representation).
46
 *
47
 * @author Roman Borschel <[email protected]>
48
 * @author Jonathan H. Wage <[email protected]>
49
 * @since 2.0
50
 */
51
class ClassMetadataInfo implements ClassMetadata
52
{
53
    /* The inheritance mapping types */
54
    /**
55
     * NONE means the class does not participate in an inheritance hierarchy
56
     * and therefore does not need an inheritance mapping type.
57
     */
58
    const INHERITANCE_TYPE_NONE = 1;
59
60
    /**
61
     * JOINED means the class will be persisted according to the rules of
62
     * <tt>Class Table Inheritance</tt>.
63
     */
64
    const INHERITANCE_TYPE_JOINED = 2;
65
66
    /**
67
     * SINGLE_TABLE means the class will be persisted according to the rules of
68
     * <tt>Single Table Inheritance</tt>.
69
     */
70
    const INHERITANCE_TYPE_SINGLE_TABLE = 3;
71
72
    /**
73
     * TABLE_PER_CLASS means the class will be persisted according to the rules
74
     * of <tt>Concrete Table Inheritance</tt>.
75
     */
76
    const INHERITANCE_TYPE_TABLE_PER_CLASS = 4;
77
78
    /* The Id generator types. */
79
    /**
80
     * AUTO means the generator type will depend on what the used platform prefers.
81
     * Offers full portability.
82
     */
83
    const GENERATOR_TYPE_AUTO = 1;
84
85
    /**
86
     * SEQUENCE means a separate sequence object will be used. Platforms that do
87
     * not have native sequence support may emulate it. Full portability is currently
88
     * not guaranteed.
89
     */
90
    const GENERATOR_TYPE_SEQUENCE = 2;
91
92
    /**
93
     * TABLE means a separate table is used for id generation.
94
     * Offers full portability.
95
     */
96
    const GENERATOR_TYPE_TABLE = 3;
97
98
    /**
99
     * IDENTITY means an identity column is used for id generation. The database
100
     * will fill in the id column on insertion. Platforms that do not support
101
     * native identity columns may emulate them. Full portability is currently
102
     * not guaranteed.
103
     */
104
    const GENERATOR_TYPE_IDENTITY = 4;
105
106
    /**
107
     * NONE means the class does not have a generated id. That means the class
108
     * must have a natural, manually assigned id.
109
     */
110
    const GENERATOR_TYPE_NONE = 5;
111
112
    /**
113
     * UUID means that a UUID/GUID expression is used for id generation. Full
114
     * portability is currently not guaranteed.
115
     */
116
    const GENERATOR_TYPE_UUID = 6;
117
118
    /**
119
     * CUSTOM means that customer will use own ID generator that supposedly work
120
     */
121
    const GENERATOR_TYPE_CUSTOM = 7;
122
123
    /**
124
     * DEFERRED_IMPLICIT means that changes of entities are calculated at commit-time
125
     * by doing a property-by-property comparison with the original data. This will
126
     * be done for all entities that are in MANAGED state at commit-time.
127
     *
128
     * This is the default change tracking policy.
129
     */
130
    const CHANGETRACKING_DEFERRED_IMPLICIT = 1;
131
132
    /**
133
     * DEFERRED_EXPLICIT means that changes of entities are calculated at commit-time
134
     * by doing a property-by-property comparison with the original data. This will
135
     * be done only for entities that were explicitly saved (through persist() or a cascade).
136
     */
137
    const CHANGETRACKING_DEFERRED_EXPLICIT = 2;
138
139
    /**
140
     * NOTIFY means that Doctrine relies on the entities sending out notifications
141
     * when their properties change. Such entity classes must implement
142
     * the <tt>NotifyPropertyChanged</tt> interface.
143
     */
144
    const CHANGETRACKING_NOTIFY = 3;
145
146
    /**
147
     * Specifies that an association is to be fetched when it is first accessed.
148
     */
149
    const FETCH_LAZY = 2;
150
151
    /**
152
     * Specifies that an association is to be fetched when the owner of the
153
     * association is fetched.
154
     */
155
    const FETCH_EAGER = 3;
156
157
    /**
158
     * Specifies that an association is to be fetched lazy (on first access) and that
159
     * commands such as Collection#count, Collection#slice are issued directly against
160
     * the database if the collection is not yet initialized.
161
     */
162
    const FETCH_EXTRA_LAZY = 4;
163
164
    /**
165
     * Identifies a one-to-one association.
166
     */
167
    const ONE_TO_ONE = 1;
168
169
    /**
170
     * Identifies a many-to-one association.
171
     */
172
    const MANY_TO_ONE = 2;
173
174
    /**
175
     * Identifies a one-to-many association.
176
     */
177
    const ONE_TO_MANY = 4;
178
179
    /**
180
     * Identifies a many-to-many association.
181
     */
182
    const MANY_TO_MANY = 8;
183
184
    /**
185
     * Combined bitmask for to-one (single-valued) associations.
186
     */
187
    const TO_ONE = 3;
188
189
    /**
190
     * Combined bitmask for to-many (collection-valued) associations.
191
     */
192
    const TO_MANY = 12;
193
194
    /**
195
     * ReadOnly cache can do reads, inserts and deletes, cannot perform updates or employ any locks,
196
     */
197
    const CACHE_USAGE_READ_ONLY = 1;
198
199
    /**
200
     * Nonstrict Read Write Cache doesn’t employ any locks but can do inserts, update and deletes.
201
     */
202
    const CACHE_USAGE_NONSTRICT_READ_WRITE = 2;
203
204
    /**
205
     * Read Write Attempts to lock the entity before update/delete.
206
     */
207
    const CACHE_USAGE_READ_WRITE = 3;
208
209
    /**
210
     * READ-ONLY: The name of the entity class.
211
     *
212
     * @var string
213
     */
214
    public $name;
215
216
    /**
217
     * READ-ONLY: The namespace the entity class is contained in.
218
     *
219
     * @var string
220
     *
221
     * @todo Not really needed. Usage could be localized.
222
     */
223
    public $namespace;
224
225
    /**
226
     * READ-ONLY: The name of the entity class that is at the root of the mapped entity inheritance
227
     * hierarchy. If the entity is not part of a mapped inheritance hierarchy this is the same
228
     * as {@link $name}.
229
     *
230
     * @var string
231
     */
232
    public $rootEntityName;
233
234
    /**
235
     * READ-ONLY: The definition of custom generator. Only used for CUSTOM
236
     * generator type
237
     *
238
     * The definition has the following structure:
239
     * <code>
240
     * array(
241
     *     'class' => 'ClassName',
242
     * )
243
     * </code>
244
     *
245
     * @var array
246
     *
247
     * @todo Merge with tableGeneratorDefinition into generic generatorDefinition
248
     */
249
    public $customGeneratorDefinition;
250
251
    /**
252
     * The name of the custom repository class used for the entity class.
253
     * (Optional).
254
     *
255
     * @var string
256
     */
257
    public $customRepositoryClassName;
258
259
    /**
260
     * READ-ONLY: Whether this class describes the mapping of a mapped superclass.
261
     *
262
     * @var boolean
263
     */
264
    public $isMappedSuperclass = false;
265
266
    /**
267
     * READ-ONLY: Whether this class describes the mapping of an embeddable class.
268
     *
269
     * @var boolean
270
     */
271
    public $isEmbeddedClass = false;
272
273
    /**
274
     * READ-ONLY: The names of the parent classes (ancestors).
275
     *
276
     * @var array
277
     */
278
    public $parentClasses = array();
279
280
    /**
281
     * READ-ONLY: The names of all subclasses (descendants).
282
     *
283
     * @var array
284
     */
285
    public $subClasses = array();
286
287
    /**
288
     * READ-ONLY: The names of all embedded classes based on properties.
289
     *
290
     * @var array
291
     */
292
    public $embeddedClasses = array();
293
294
    /**
295
     * READ-ONLY: The named queries allowed to be called directly from Repository.
296
     *
297
     * @var array
298
     */
299
    public $namedQueries = array();
300
301
    /**
302
     * READ-ONLY: The named native queries allowed to be called directly from Repository.
303
     *
304
     * A native SQL named query definition has the following structure:
305
     * <pre>
306
     * array(
307
     *     'name'               => <query name>,
308
     *     'query'              => <sql query>,
309
     *     'resultClass'        => <class of the result>,
310
     *     'resultSetMapping'   => <name of a SqlResultSetMapping>
311
     * )
312
     * </pre>
313
     *
314
     * @var array
315
     */
316
    public $namedNativeQueries = array();
317
318
    /**
319
     * READ-ONLY: The mappings of the results of native SQL queries.
320
     *
321
     * A native result mapping definition has the following structure:
322
     * <pre>
323
     * array(
324
     *     'name'               => <result name>,
325
     *     'entities'           => array(<entity result mapping>),
326
     *     'columns'            => array(<column result mapping>)
327
     * )
328
     * </pre>
329
     *
330
     * @var array
331
     */
332
    public $sqlResultSetMappings = array();
333
334
    /**
335
     * READ-ONLY: The field names of all fields that are part of the identifier/primary key
336
     * of the mapped entity class.
337
     *
338
     * @var array
339
     */
340
    public $identifier = array();
341
342
    /**
343
     * READ-ONLY: The inheritance mapping type used by the class.
344
     *
345
     * @var integer
346
     */
347
    public $inheritanceType = self::INHERITANCE_TYPE_NONE;
348
349
    /**
350
     * READ-ONLY: The Id generator type used by the class.
351
     *
352
     * @var int
353
     */
354
    public $generatorType = self::GENERATOR_TYPE_NONE;
355
356
    /**
357
     * READ-ONLY: The field mappings of the class.
358
     * Keys are field names and values are mapping definitions.
359
     *
360
     * The mapping definition array has the following values:
361
     *
362
     * - <b>fieldName</b> (string)
363
     * The name of the field in the Entity.
364
     *
365
     * - <b>type</b> (string)
366
     * The type name of the mapped field. Can be one of Doctrine's mapping types
367
     * or a custom mapping type.
368
     *
369
     * - <b>columnName</b> (string, optional)
370
     * The column name. Optional. Defaults to the field name.
371
     *
372
     * - <b>length</b> (integer, optional)
373
     * The database length of the column. Optional. Default value taken from
374
     * the type.
375
     *
376
     * - <b>id</b> (boolean, optional)
377
     * Marks the field as the primary key of the entity. Multiple fields of an
378
     * entity can have the id attribute, forming a composite key.
379
     *
380
     * - <b>nullable</b> (boolean, optional)
381
     * Whether the column is nullable. Defaults to FALSE.
382
     *
383
     * - <b>columnDefinition</b> (string, optional, schema-only)
384
     * The SQL fragment that is used when generating the DDL for the column.
385
     *
386
     * - <b>precision</b> (integer, optional, schema-only)
387
     * The precision of a decimal column. Only valid if the column type is decimal.
388
     *
389
     * - <b>scale</b> (integer, optional, schema-only)
390
     * The scale of a decimal column. Only valid if the column type is decimal.
391
     *
392
     * - <b>'unique'</b> (string, optional, schema-only)
393
     * Whether a unique constraint should be generated for the column.
394
     *
395
     * @var array
396
     */
397
    public $fieldMappings = array();
398
399
    /**
400
     * READ-ONLY: An array of field names. Used to look up field names from column names.
401
     * Keys are column names and values are field names.
402
     *
403
     * @var array
404
     */
405
    public $fieldNames = array();
406
407
    /**
408
     * READ-ONLY: A map of field names to column names. Keys are field names and values column names.
409
     * Used to look up column names from field names.
410
     * This is the reverse lookup map of $_fieldNames.
411
     *
412
     * @var array
413
     *
414
     * @deprecated 3.0 Remove this.
415
     */
416
    public $columnNames = array();
417
418
    /**
419
     * READ-ONLY: The discriminator value of this class.
420
     *
421
     * <b>This does only apply to the JOINED and SINGLE_TABLE inheritance mapping strategies
422
     * where a discriminator column is used.</b>
423
     *
424
     * @var mixed
425
     *
426
     * @see discriminatorColumn
427
     */
428
    public $discriminatorValue;
429
430
    /**
431
     * READ-ONLY: The discriminator map of all mapped classes in the hierarchy.
432
     *
433
     * <b>This does only apply to the JOINED and SINGLE_TABLE inheritance mapping strategies
434
     * where a discriminator column is used.</b>
435
     *
436
     * @var mixed
437
     *
438
     * @see discriminatorColumn
439
     */
440
    public $discriminatorMap = array();
441
442
    /**
443
     * READ-ONLY: The definition of the discriminator column used in JOINED and SINGLE_TABLE
444
     * inheritance mappings.
445
     *
446
     * @var array
447
     */
448
    public $discriminatorColumn;
449
450
    /**
451
     * READ-ONLY: The primary table definition. The definition is an array with the
452
     * following entries:
453
     *
454
     * name => <tableName>
455
     * schema => <schemaName>
456
     * indexes => array
457
     * uniqueConstraints => array
458
     *
459
     * @var array
460
     */
461
    public $table;
462
463
    /**
464
     * READ-ONLY: The registered lifecycle callbacks for entities of this class.
465
     *
466
     * @var array
467
     */
468
    public $lifecycleCallbacks = array();
469
470
    /**
471
     * READ-ONLY: The registered entity listeners.
472
     *
473
     * @var array
474
     */
475
    public $entityListeners = array();
476
477
    /**
478
     * READ-ONLY: The association mappings of this class.
479
     *
480
     * The mapping definition array supports the following keys:
481
     *
482
     * - <b>fieldName</b> (string)
483
     * The name of the field in the entity the association is mapped to.
484
     *
485
     * - <b>targetEntity</b> (string)
486
     * The class name of the target entity. If it is fully-qualified it is used as is.
487
     * If it is a simple, unqualified class name the namespace is assumed to be the same
488
     * as the namespace of the source entity.
489
     *
490
     * - <b>mappedBy</b> (string, required for bidirectional associations)
491
     * The name of the field that completes the bidirectional association on the owning side.
492
     * This key must be specified on the inverse side of a bidirectional association.
493
     *
494
     * - <b>inversedBy</b> (string, required for bidirectional associations)
495
     * The name of the field that completes the bidirectional association on the inverse side.
496
     * This key must be specified on the owning side of a bidirectional association.
497
     *
498
     * - <b>cascade</b> (array, optional)
499
     * The names of persistence operations to cascade on the association. The set of possible
500
     * values are: "persist", "remove", "detach", "merge", "refresh", "all" (implies all others).
501
     *
502
     * - <b>orderBy</b> (array, one-to-many/many-to-many only)
503
     * A map of field names (of the target entity) to sorting directions (ASC/DESC).
504
     * Example: array('priority' => 'desc')
505
     *
506
     * - <b>fetch</b> (integer, optional)
507
     * The fetching strategy to use for the association, usually defaults to FETCH_LAZY.
508
     * Possible values are: ClassMetadata::FETCH_EAGER, ClassMetadata::FETCH_LAZY.
509
     *
510
     * - <b>joinTable</b> (array, optional, many-to-many only)
511
     * Specification of the join table and its join columns (foreign keys).
512
     * Only valid for many-to-many mappings. Note that one-to-many associations can be mapped
513
     * through a join table by simply mapping the association as many-to-many with a unique
514
     * constraint on the join table.
515
     *
516
     * - <b>indexBy</b> (string, optional, to-many only)
517
     * Specification of a field on target-entity that is used to index the collection by.
518
     * This field HAS to be either the primary key or a unique column. Otherwise the collection
519
     * does not contain all the entities that are actually related.
520
     *
521
     * A join table definition has the following structure:
522
     * <pre>
523
     * array(
524
     *     'name' => <join table name>,
525
     *      'joinColumns' => array(<join column mapping from join table to source table>),
526
     *      'inverseJoinColumns' => array(<join column mapping from join table to target table>)
527
     * )
528
     * </pre>
529
     *
530
     * @var array
531
     */
532
    public $associationMappings = array();
533
534
    /**
535
     * READ-ONLY: Flag indicating whether the identifier/primary key of the class is composite.
536
     *
537
     * @var boolean
538
     */
539
    public $isIdentifierComposite = false;
540
541
    /**
542
     * READ-ONLY: Flag indicating whether the identifier/primary key contains at least one foreign key association.
543
     *
544
     * This flag is necessary because some code blocks require special treatment of this cases.
545
     *
546
     * @var boolean
547
     */
548
    public $containsForeignIdentifier = false;
549
550
    /**
551
     * READ-ONLY: The ID generator used for generating IDs for this class.
552
     *
553
     * @var \Doctrine\ORM\Id\AbstractIdGenerator
554
     *
555
     * @todo Remove!
556
     */
557
    public $idGenerator;
558
559
    /**
560
     * READ-ONLY: The definition of the sequence generator of this class. Only used for the
561
     * SEQUENCE generation strategy.
562
     *
563
     * The definition has the following structure:
564
     * <code>
565
     * array(
566
     *     'sequenceName' => 'name',
567
     *     'allocationSize' => 20,
568
     *     'initialValue' => 1
569
     * )
570
     * </code>
571
     *
572
     * @var array
573
     *
574
     * @todo Merge with tableGeneratorDefinition into generic generatorDefinition
575
     */
576
    public $sequenceGeneratorDefinition;
577
578
    /**
579
     * READ-ONLY: The definition of the table generator of this class. Only used for the
580
     * TABLE generation strategy.
581
     *
582
     * @var array
583
     *
584
     * @todo Merge with tableGeneratorDefinition into generic generatorDefinition
585
     */
586
    public $tableGeneratorDefinition;
587
588
    /**
589
     * READ-ONLY: The policy used for change-tracking on entities of this class.
590
     *
591
     * @var integer
592
     */
593
    public $changeTrackingPolicy = self::CHANGETRACKING_DEFERRED_IMPLICIT;
594
595
    /**
596
     * READ-ONLY: A flag for whether or not instances of this class are to be versioned
597
     * with optimistic locking.
598
     *
599
     * @var boolean
600
     */
601
    public $isVersioned;
602
603
    /**
604
     * READ-ONLY: The name of the field which is used for versioning in optimistic locking (if any).
605
     *
606
     * @var mixed
607
     */
608
    public $versionField;
609
610
    /**
611
     * @var array
612
     */
613
    public $cache = null;
614
615
    /**
616
     * The ReflectionClass instance of the mapped class.
617
     *
618
     * @var ReflectionClass
619
     */
620
    public $reflClass;
621
622
    /**
623
     * Is this entity marked as "read-only"?
624
     *
625
     * That means it is never considered for change-tracking in the UnitOfWork. It is a very helpful performance
626
     * optimization for entities that are immutable, either in your domain or through the relation database
627
     * (coming from a view, or a history table for example).
628
     *
629
     * @var bool
630
     */
631
    public $isReadOnly = false;
632
633
    /**
634
     * NamingStrategy determining the default column and table names.
635
     *
636
     * @var \Doctrine\ORM\Mapping\NamingStrategy
637
     */
638
    protected $namingStrategy;
639
640
    /**
641
     * The ReflectionProperty instances of the mapped class.
642
     *
643
     * @var \ReflectionProperty[]
644
     */
645
    public $reflFields = array();
646
647
    /**
648
     * @var \Doctrine\Instantiator\InstantiatorInterface|null
649
     */
650
    private $instantiator;
651
652
    /**
653
     * Initializes a new ClassMetadata instance that will hold the object-relational mapping
654
     * metadata of the class with the given name.
655
     *
656
     * @param string              $entityName     The name of the entity class the new instance is used for.
657
     * @param NamingStrategy|null $namingStrategy
658
     */
659 647
    public function __construct($entityName, NamingStrategy $namingStrategy = null)
660
    {
661 647
        $this->name = $entityName;
662 647
        $this->rootEntityName = $entityName;
663 647
        $this->namingStrategy = $namingStrategy ?: new DefaultNamingStrategy();
664 647
        $this->instantiator   = new Instantiator();
665 647
    }
666
667
    /**
668
     * Gets the ReflectionProperties of the mapped class.
669
     *
670
     * @return array An array of ReflectionProperty instances.
671
     */
672 224
    public function getReflectionProperties()
673
    {
674 224
        return $this->reflFields;
675
    }
676
677
    /**
678
     * Gets a ReflectionProperty for a specific field of the mapped class.
679
     *
680
     * @param string $name
681
     *
682
     * @return \ReflectionProperty
683
     */
684 1
    public function getReflectionProperty($name)
685
    {
686 1
        return $this->reflFields[$name];
687
    }
688
689
    /**
690
     * Gets the ReflectionProperty for the single identifier field.
691
     *
692
     * @return \ReflectionProperty
693
     *
694
     * @throws BadMethodCallException If the class has a composite identifier.
695
     */
696
    public function getSingleIdReflectionProperty()
697
    {
698
        if ($this->isIdentifierComposite) {
699
            throw new BadMethodCallException("Class " . $this->name . " has a composite identifier.");
700
        }
701
702
        return $this->reflFields[$this->identifier[0]];
703
    }
704
705
    /**
706
     * Extracts the identifier values of an entity of this class.
707
     *
708
     * For composite identifiers, the identifier values are returned as an array
709
     * with the same order as the field order in {@link identifier}.
710
     *
711
     * @param object $entity
712
     *
713
     * @return array
714
     */
715 465
    public function getIdentifierValues($entity)
716
    {
717 465
        if ($this->isIdentifierComposite) {
718 90
            $id = array();
719
720 90
            foreach ($this->identifier as $idField) {
721 90
                $value = $this->reflFields[$idField]->getValue($entity);
722
723 90
                if ($value !== null) {
724 90
                    $id[$idField] = $value;
725
                }
726
            }
727
728 90
            return $id;
729
        }
730
731 446
        $id = $this->identifier[0];
732 446
        $value = $this->reflFields[$id]->getValue($entity);
733
734 446
        if (null === $value) {
735 28
            return array();
736
        }
737
738 423
        return array($id => $value);
739
    }
740
741
    /**
742
     * Populates the entity identifier of an entity.
743
     *
744
     * @param object $entity
745
     * @param array  $id
746
     *
747
     * @return void
748
     *
749
     * @todo Rename to assignIdentifier()
750
     */
751 6
    public function setIdentifierValues($entity, array $id)
752
    {
753 6
        foreach ($id as $idField => $idValue) {
754 6
            $this->reflFields[$idField]->setValue($entity, $idValue);
755
        }
756 6
    }
757
758
    /**
759
     * Sets the specified field to the specified value on the given entity.
760
     *
761
     * @param object $entity
762
     * @param string $field
763
     * @param mixed  $value
764
     *
765
     * @return void
766
     */
767 231
    public function setFieldValue($entity, $field, $value)
768
    {
769 231
        $this->reflFields[$field]->setValue($entity, $value);
770 231
    }
771
772
    /**
773
     * Gets the specified field's value off the given entity.
774
     *
775
     * @param object $entity
776
     * @param string $field
777
     *
778
     * @return mixed
779
     */
780 306
    public function getFieldValue($entity, $field)
781
    {
782 306
        return $this->reflFields[$field]->getValue($entity);
783
    }
784
785
    /**
786
     * Creates a string representation of this instance.
787
     *
788
     * @return string The string representation of this instance.
789
     *
790
     * @todo Construct meaningful string representation.
791
     */
792
    public function __toString()
793
    {
794
        return __CLASS__ . '@' . spl_object_hash($this);
795
    }
796
797
    /**
798
     * Determines which fields get serialized.
799
     *
800
     * It is only serialized what is necessary for best unserialization performance.
801
     * That means any metadata properties that are not set or empty or simply have
802
     * their default value are NOT serialized.
803
     *
804
     * Parts that are also NOT serialized because they can not be properly unserialized:
805
     *      - reflClass (ReflectionClass)
806
     *      - reflFields (ReflectionProperty array)
807
     *
808
     * @return array The names of all the fields that should be serialized.
809
     */
810 6
    public function __sleep()
811
    {
812
        // This metadata is always serialized/cached.
813
        $serialized = array(
814 6
            'associationMappings',
815
            'columnNames', //TODO: 3.0 Remove this. Can use fieldMappings[$fieldName]['columnName']
816
            'fieldMappings',
817
            'fieldNames',
818
            'embeddedClasses',
819
            'identifier',
820
            'isIdentifierComposite', // TODO: REMOVE
821
            'name',
822
            'namespace', // TODO: REMOVE
823
            'table',
824
            'rootEntityName',
825
            'idGenerator', //TODO: Does not really need to be serialized. Could be moved to runtime.
826
        );
827
828
        // The rest of the metadata is only serialized if necessary.
829 6
        if ($this->changeTrackingPolicy != self::CHANGETRACKING_DEFERRED_IMPLICIT) {
830
            $serialized[] = 'changeTrackingPolicy';
831
        }
832
833 6
        if ($this->customRepositoryClassName) {
834 1
            $serialized[] = 'customRepositoryClassName';
835
        }
836
837 6
        if ($this->inheritanceType != self::INHERITANCE_TYPE_NONE) {
838 1
            $serialized[] = 'inheritanceType';
839 1
            $serialized[] = 'discriminatorColumn';
840 1
            $serialized[] = 'discriminatorValue';
841 1
            $serialized[] = 'discriminatorMap';
842 1
            $serialized[] = 'parentClasses';
843 1
            $serialized[] = 'subClasses';
844
        }
845
846 6
        if ($this->generatorType != self::GENERATOR_TYPE_NONE) {
847 1
            $serialized[] = 'generatorType';
848 1
            if ($this->generatorType == self::GENERATOR_TYPE_SEQUENCE) {
849
                $serialized[] = 'sequenceGeneratorDefinition';
850
            }
851
        }
852
853 6
        if ($this->isMappedSuperclass) {
854
            $serialized[] = 'isMappedSuperclass';
855
        }
856
857 6
        if ($this->isEmbeddedClass) {
858 1
            $serialized[] = 'isEmbeddedClass';
859
        }
860
861 6
        if ($this->containsForeignIdentifier) {
862
            $serialized[] = 'containsForeignIdentifier';
863
        }
864
865 6
        if ($this->isVersioned) {
866
            $serialized[] = 'isVersioned';
867
            $serialized[] = 'versionField';
868
        }
869
870 6
        if ($this->lifecycleCallbacks) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->lifecycleCallbacks of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
871
            $serialized[] = 'lifecycleCallbacks';
872
        }
873
874 6
        if ($this->entityListeners) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->entityListeners of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
875 1
            $serialized[] = 'entityListeners';
876
        }
877
878 6
        if ($this->namedQueries) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->namedQueries of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
879 1
            $serialized[] = 'namedQueries';
880
        }
881
882 6
        if ($this->namedNativeQueries) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->namedNativeQueries of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
883
            $serialized[] = 'namedNativeQueries';
884
        }
885
886 6
        if ($this->sqlResultSetMappings) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->sqlResultSetMappings of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
887
            $serialized[] = 'sqlResultSetMappings';
888
        }
889
890 6
        if ($this->isReadOnly) {
891 1
            $serialized[] = 'isReadOnly';
892
        }
893
894 6
        if ($this->customGeneratorDefinition) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->customGeneratorDefinition of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
895
            $serialized[] = "customGeneratorDefinition";
896
        }
897
898 6
        if ($this->cache) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->cache of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
899
            $serialized[] = 'cache';
900
        }
901
902 6
        return $serialized;
903
    }
904
905
    /**
906
     * Creates a new instance of the mapped class, without invoking the constructor.
907
     *
908
     * @return object
909
     */
910 672
    public function newInstance()
911
    {
912 672
        return $this->instantiator->instantiate($this->name);
913
    }
914
915
    /**
916
     * Restores some state that can not be serialized/unserialized.
917
     *
918
     * @param \Doctrine\Common\Persistence\Mapping\ReflectionService $reflService
919
     *
920
     * @return void
921
     */
922 2007
    public function wakeupReflection($reflService)
923
    {
924
        // Restore ReflectionClass and properties
925 2007
        $this->reflClass    = $reflService->getClass($this->name);
926 2007
        $this->instantiator = $this->instantiator ?: new Instantiator();
927
928 2007
        $parentReflFields = array();
929
930 2007
        foreach ($this->embeddedClasses as $property => $embeddedClass) {
931 21
            if (isset($embeddedClass['declaredField'])) {
932 15
                $parentReflFields[$property] = new ReflectionEmbeddedProperty(
933 15
                    $parentReflFields[$embeddedClass['declaredField']],
934 15
                    $reflService->getAccessibleProperty(
0 ignored issues
show
Bug introduced by
It seems like $reflService->getAccessi...Class['originalField']) can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
935 15
                        $this->embeddedClasses[$embeddedClass['declaredField']]['class'],
936 15
                        $embeddedClass['originalField']
937
                    ),
938 15
                    $this->embeddedClasses[$embeddedClass['declaredField']]['class']
939
                );
940
941 15
                continue;
942
            }
943
944 21
            $fieldRefl = $reflService->getAccessibleProperty(
945 21
                isset($embeddedClass['declared']) ? $embeddedClass['declared'] : $this->name,
946
                $property
947
            );
948
949 21
            $parentReflFields[$property] = $fieldRefl;
950 21
            $this->reflFields[$property] = $fieldRefl;
951
        }
952
953 2007
        foreach ($this->fieldMappings as $field => $mapping) {
954 2002
            if (isset($mapping['declaredField']) && isset($parentReflFields[$mapping['declaredField']])) {
955 20
                $this->reflFields[$field] = new ReflectionEmbeddedProperty(
956 20
                    $parentReflFields[$mapping['declaredField']],
957 20
                    $reflService->getAccessibleProperty($mapping['originalClass'], $mapping['originalField']),
0 ignored issues
show
Bug introduced by
It seems like $reflService->getAccessi...pping['originalField']) can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
958 20
                    $mapping['originalClass']
959
                );
960 20
                continue;
961
            }
962
963 2002
            $this->reflFields[$field] = isset($mapping['declared'])
964 496
                ? $reflService->getAccessibleProperty($mapping['declared'], $field)
965 2002
                : $reflService->getAccessibleProperty($this->name, $field);
966
        }
967
968 2007
        foreach ($this->associationMappings as $field => $mapping) {
969 1694
            $this->reflFields[$field] = isset($mapping['declared'])
970 403
                ? $reflService->getAccessibleProperty($mapping['declared'], $field)
971 1694
                : $reflService->getAccessibleProperty($this->name, $field);
972
        }
973 2007
    }
974
975
    /**
976
     * Initializes a new ClassMetadata instance that will hold the object-relational mapping
977
     * metadata of the class with the given name.
978
     *
979
     * @param \Doctrine\Common\Persistence\Mapping\ReflectionService $reflService The reflection service.
980
     *
981
     * @return void
982
     */
983 611
    public function initializeReflection($reflService)
984
    {
985 611
        $this->reflClass = $reflService->getClass($this->name);
986 611
        $this->namespace = $reflService->getClassNamespace($this->name);
987
988 611
        if ($this->reflClass) {
989 604
            $this->name = $this->rootEntityName = $this->reflClass->getName();
0 ignored issues
show
Bug introduced by
Consider using $this->reflClass->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
990
        }
991
992 611
        $this->table['name'] = $this->namingStrategy->classToTableName($this->name);
993 611
    }
994
995
    /**
996
     * Validates Identifier.
997
     *
998
     * @return void
999
     *
1000
     * @throws MappingException
1001
     */
1002 400
    public function validateIdentifier()
1003
    {
1004 400
        if ($this->isMappedSuperclass || $this->isEmbeddedClass) {
1005 60
            return;
1006
        }
1007
1008
        // Verify & complete identifier mapping
1009 398
        if ( ! $this->identifier) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->identifier of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
1010 6
            throw MappingException::identifierRequired($this->name);
1011
        }
1012
1013 392
        if ($this->usesIdGenerator() && $this->isIdentifierComposite) {
1014
            throw MappingException::compositeKeyAssignedIdGeneratorRequired($this->name);
1015
        }
1016 392
    }
1017
1018
    /**
1019
     * Validates association targets actually exist.
1020
     *
1021
     * @return void
1022
     *
1023
     * @throws MappingException
1024
     */
1025 401
    public function validateAssociations()
1026
    {
1027 401
        foreach ($this->associationMappings as $mapping) {
1028 267
            if ( ! ClassLoader::classExists($mapping['targetEntity']) ) {
1029 267
                throw MappingException::invalidTargetEntityClass($mapping['targetEntity'], $this->name, $mapping['fieldName']);
1030
            }
1031
        }
1032 400
    }
1033
1034
    /**
1035
     * Validates lifecycle callbacks.
1036
     *
1037
     * @param \Doctrine\Common\Persistence\Mapping\ReflectionService $reflService
1038
     *
1039
     * @return void
1040
     *
1041
     * @throws MappingException
1042
     */
1043 401
    public function validateLifecycleCallbacks($reflService)
1044
    {
1045 401
        foreach ($this->lifecycleCallbacks as $callbacks) {
1046 13
            foreach ($callbacks as $callbackFuncName) {
1047 13
                if ( ! $reflService->hasPublicMethod($this->name, $callbackFuncName)) {
1048 13
                    throw MappingException::lifecycleCallbackMethodNotFound($this->name, $callbackFuncName);
1049
                }
1050
            }
1051
        }
1052 400
    }
1053
1054
    /**
1055
     * {@inheritDoc}
1056
     */
1057 529
    public function getReflectionClass()
1058
    {
1059 529
        return $this->reflClass;
1060
    }
1061
1062
    /**
1063
     * @param array $cache
1064
     *
1065
     * @return void
1066
     */
1067 21
    public function enableCache(array $cache)
1068
    {
1069 21
        if ( ! isset($cache['usage'])) {
1070
            $cache['usage'] = self::CACHE_USAGE_READ_ONLY;
1071
        }
1072
1073 21
        if ( ! isset($cache['region'])) {
1074 21
            $cache['region'] = strtolower(str_replace('\\', '_', $this->rootEntityName));
1075
        }
1076
1077 21
        $this->cache = $cache;
1078 21
    }
1079
1080
    /**
1081
     * @param string $fieldName
1082
     * @param array  $cache
1083
     *
1084
     * @return void
1085
     */
1086 2
    public function enableAssociationCache($fieldName, array $cache)
1087
    {
1088 2
        $this->associationMappings[$fieldName]['cache'] = $this->getAssociationCacheDefaults ($fieldName, $cache);
1089 2
    }
1090
1091
    /**
1092
     * @param string $fieldName
1093
     * @param array  $cache
1094
     *
1095
     * @return array
1096
     */
1097 17
    public function getAssociationCacheDefaults($fieldName, array $cache)
1098
    {
1099 17
        if ( ! isset($cache['usage'])) {
1100 1
            $cache['usage'] = isset($this->cache['usage'])
1101 1
                ? $this->cache['usage']
1102
                : self::CACHE_USAGE_READ_ONLY;
1103
        }
1104
1105 17
        if ( ! isset($cache['region'])) {
1106 17
            $cache['region'] = strtolower(str_replace('\\', '_', $this->rootEntityName)) . '__' . $fieldName;
1107
        }
1108
1109 17
        return $cache;
1110
    }
1111
1112
    /**
1113
     * Sets the change tracking policy used by this class.
1114
     *
1115
     * @param integer $policy
1116
     *
1117
     * @return void
1118
     */
1119 139
    public function setChangeTrackingPolicy($policy)
1120
    {
1121 139
        $this->changeTrackingPolicy = $policy;
1122 139
    }
1123
1124
    /**
1125
     * Whether the change tracking policy of this class is "deferred explicit".
1126
     *
1127
     * @return boolean
1128
     */
1129 267
    public function isChangeTrackingDeferredExplicit()
1130
    {
1131 267
        return $this->changeTrackingPolicy == self::CHANGETRACKING_DEFERRED_EXPLICIT;
1132
    }
1133
1134
    /**
1135
     * Whether the change tracking policy of this class is "deferred implicit".
1136
     *
1137
     * @return boolean
1138
     */
1139 459
    public function isChangeTrackingDeferredImplicit()
1140
    {
1141 459
        return $this->changeTrackingPolicy == self::CHANGETRACKING_DEFERRED_IMPLICIT;
1142
    }
1143
1144
    /**
1145
     * Whether the change tracking policy of this class is "notify".
1146
     *
1147
     * @return boolean
1148
     */
1149 290
    public function isChangeTrackingNotify()
1150
    {
1151 290
        return $this->changeTrackingPolicy == self::CHANGETRACKING_NOTIFY;
1152
    }
1153
1154
    /**
1155
     * Checks whether a field is part of the identifier/primary key field(s).
1156
     *
1157
     * @param string $fieldName The field name.
1158
     *
1159
     * @return boolean TRUE if the field is part of the table identifier/primary key field(s),
1160
     *                 FALSE otherwise.
1161
     */
1162 1070
    public function isIdentifier($fieldName)
1163
    {
1164 1070
        if ( ! $this->identifier) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->identifier of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
1165 1
            return false;
1166
        }
1167
1168 1069
        if ( ! $this->isIdentifierComposite) {
1169 1064
            return $fieldName === $this->identifier[0];
1170
        }
1171
1172 93
        return in_array($fieldName, $this->identifier);
1173
    }
1174
1175
    /**
1176
     * Checks if the field is unique.
1177
     *
1178
     * @param string $fieldName The field name.
1179
     *
1180
     * @return boolean TRUE if the field is unique, FALSE otherwise.
1181
     */
1182
    public function isUniqueField($fieldName)
1183
    {
1184
        $mapping = $this->getFieldMapping($fieldName);
1185
1186
        if ($mapping !== false) {
1187
            return isset($mapping['unique']) && $mapping['unique'] == true;
1188
        }
1189
1190
        return false;
1191
    }
1192
1193
    /**
1194
     * Checks if the field is not null.
1195
     *
1196
     * @param string $fieldName The field name.
1197
     *
1198
     * @return boolean TRUE if the field is not null, FALSE otherwise.
1199
     */
1200 1
    public function isNullable($fieldName)
1201
    {
1202 1
        $mapping = $this->getFieldMapping($fieldName);
1203
1204 1
        if ($mapping !== false) {
1205 1
            return isset($mapping['nullable']) && $mapping['nullable'] == true;
1206
        }
1207
1208
        return false;
1209
    }
1210
1211
    /**
1212
     * Gets a column name for a field name.
1213
     * If the column name for the field cannot be found, the given field name
1214
     * is returned.
1215
     *
1216
     * @param string $fieldName The field name.
1217
     *
1218
     * @return string The column name.
1219
     */
1220 16
    public function getColumnName($fieldName)
1221
    {
1222 16
        return isset($this->columnNames[$fieldName])
0 ignored issues
show
Deprecated Code introduced by
The property Doctrine\ORM\Mapping\Cla...adataInfo::$columnNames has been deprecated with message: 3.0 Remove this.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
1223 16
            ? $this->columnNames[$fieldName]
0 ignored issues
show
Deprecated Code introduced by
The property Doctrine\ORM\Mapping\Cla...adataInfo::$columnNames has been deprecated with message: 3.0 Remove this.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
1224 16
            : $fieldName;
1225
    }
1226
1227
    /**
1228
     * Gets the mapping of a (regular) field that holds some data but not a
1229
     * reference to another object.
1230
     *
1231
     * @param string $fieldName The field name.
1232
     *
1233
     * @return array The field mapping.
1234
     *
1235
     * @throws MappingException
1236
     */
1237 199
    public function getFieldMapping($fieldName)
1238
    {
1239 199
        if ( ! isset($this->fieldMappings[$fieldName])) {
1240 1
            throw MappingException::mappingNotFound($this->name, $fieldName);
1241
        }
1242
1243 198
        return $this->fieldMappings[$fieldName];
1244
    }
1245
1246
    /**
1247
     * Gets the mapping of an association.
1248
     *
1249
     * @see ClassMetadataInfo::$associationMappings
1250
     *
1251
     * @param string $fieldName The field name that represents the association in
1252
     *                          the object model.
1253
     *
1254
     * @return array The mapping.
1255
     *
1256
     * @throws MappingException
1257
     */
1258 485
    public function getAssociationMapping($fieldName)
1259
    {
1260 485
        if ( ! isset($this->associationMappings[$fieldName])) {
1261
            throw MappingException::mappingNotFound($this->name, $fieldName);
1262
        }
1263
1264 485
        return $this->associationMappings[$fieldName];
1265
    }
1266
1267
    /**
1268
     * Gets all association mappings of the class.
1269
     *
1270
     * @return array
1271
     */
1272
    public function getAssociationMappings()
1273
    {
1274
        return $this->associationMappings;
1275
    }
1276
1277
    /**
1278
     * Gets the field name for a column name.
1279
     * If no field name can be found the column name is returned.
1280
     *
1281
     * @param string $columnName The column name.
1282
     *
1283
     * @return string The column alias.
1284
     */
1285 237
    public function getFieldName($columnName)
1286
    {
1287 237
        return isset($this->fieldNames[$columnName])
1288 237
            ? $this->fieldNames[$columnName]
1289 237
            : $columnName;
1290
    }
1291
1292
    /**
1293
     * Gets the named query.
1294
     *
1295
     * @see ClassMetadataInfo::$namedQueries
1296
     *
1297
     * @param string $queryName The query name.
1298
     *
1299
     * @return string
1300
     *
1301
     * @throws MappingException
1302
     */
1303 4
    public function getNamedQuery($queryName)
1304
    {
1305 4
        if ( ! isset($this->namedQueries[$queryName])) {
1306 1
            throw MappingException::queryNotFound($this->name, $queryName);
1307
        }
1308
1309 3
        return $this->namedQueries[$queryName]['dql'];
1310
    }
1311
1312
    /**
1313
     * Gets all named queries of the class.
1314
     *
1315
     * @return array
1316
     */
1317 7
    public function getNamedQueries()
1318
    {
1319 7
        return $this->namedQueries;
1320
    }
1321
1322
    /**
1323
     * Gets the named native query.
1324
     *
1325
     * @see ClassMetadataInfo::$namedNativeQueries
1326
     *
1327
     * @param string $queryName The query name.
1328
     *
1329
     * @return array
1330
     *
1331
     * @throws MappingException
1332
     */
1333 17
    public function getNamedNativeQuery($queryName)
1334
    {
1335 17
        if ( ! isset($this->namedNativeQueries[$queryName])) {
1336
            throw MappingException::queryNotFound($this->name, $queryName);
1337
        }
1338
1339 17
        return $this->namedNativeQueries[$queryName];
1340
    }
1341
1342
    /**
1343
     * Gets all named native queries of the class.
1344
     *
1345
     * @return array
1346
     */
1347 2
    public function getNamedNativeQueries()
1348
    {
1349 2
        return $this->namedNativeQueries;
1350
    }
1351
1352
    /**
1353
     * Gets the result set mapping.
1354
     *
1355
     * @see ClassMetadataInfo::$sqlResultSetMappings
1356
     *
1357
     * @param string $name The result set mapping name.
1358
     *
1359
     * @return array
1360
     *
1361
     * @throws MappingException
1362
     */
1363 21
    public function getSqlResultSetMapping($name)
1364
    {
1365 21
        if ( ! isset($this->sqlResultSetMappings[$name])) {
1366
            throw MappingException::resultMappingNotFound($this->name, $name);
1367
        }
1368
1369 21
        return $this->sqlResultSetMappings[$name];
1370
    }
1371
1372
    /**
1373
     * Gets all sql result set mappings of the class.
1374
     *
1375
     * @return array
1376
     */
1377 8
    public function getSqlResultSetMappings()
1378
    {
1379 8
        return $this->sqlResultSetMappings;
1380
    }
1381
1382
    /**
1383
     * Validates & completes the given field mapping.
1384
     *
1385
     * @param array $mapping The field mapping to validate & complete.
1386
     *
1387
     * @return array The validated and completed field mapping.
1388
     *
1389
     * @throws MappingException
1390
     */
1391 528
    protected function _validateAndCompleteFieldMapping(array &$mapping)
1392
    {
1393
        // Check mandatory fields
1394 528
        if ( ! isset($mapping['fieldName']) || strlen($mapping['fieldName']) == 0) {
1395 1
            throw MappingException::missingFieldName($this->name);
1396
        }
1397
1398 527
        if ( ! isset($mapping['type'])) {
1399
            // Default to string
1400 64
            $mapping['type'] = 'string';
1401
        }
1402
1403
        // Complete fieldName and columnName mapping
1404 527
        if ( ! isset($mapping['columnName'])) {
1405 435
            $mapping['columnName'] = $this->namingStrategy->propertyToColumnName($mapping['fieldName'], $this->name);
1406
        }
1407
1408 527
        if ($mapping['columnName'][0] === '`') {
1409 11
            $mapping['columnName']  = trim($mapping['columnName'], '`');
1410 11
            $mapping['quoted']      = true;
1411
        }
1412
1413 527
        $this->columnNames[$mapping['fieldName']] = $mapping['columnName'];
0 ignored issues
show
Deprecated Code introduced by
The property Doctrine\ORM\Mapping\Cla...adataInfo::$columnNames has been deprecated with message: 3.0 Remove this.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
1414
1415 527
        if (isset($this->fieldNames[$mapping['columnName']]) || ($this->discriminatorColumn != null && $this->discriminatorColumn['name'] == $mapping['columnName'])) {
1416 2
            throw MappingException::duplicateColumnName($this->name, $mapping['columnName']);
1417
        }
1418
1419 526
        $this->fieldNames[$mapping['columnName']] = $mapping['fieldName'];
1420
1421
        // Complete id mapping
1422 526
        if (isset($mapping['id']) && $mapping['id'] === true) {
1423 488
            if ($this->versionField == $mapping['fieldName']) {
1424
                throw MappingException::cannotVersionIdField($this->name, $mapping['fieldName']);
1425
            }
1426
1427 488
            if ( ! in_array($mapping['fieldName'], $this->identifier)) {
1428 488
                $this->identifier[] = $mapping['fieldName'];
1429
            }
1430
1431
            // Check for composite key
1432 488
            if ( ! $this->isIdentifierComposite && count($this->identifier) > 1) {
1433 17
                $this->isIdentifierComposite = true;
1434
            }
1435
        }
1436
1437 526
        if (Type::hasType($mapping['type']) && Type::getType($mapping['type'])->canRequireSQLConversion()) {
1438 5
            if (isset($mapping['id']) && $mapping['id'] === true) {
1439
                 throw MappingException::sqlConversionNotAllowedForIdentifiers($this->name, $mapping['fieldName'], $mapping['type']);
1440
            }
1441
1442 5
            $mapping['requireSQLConversion'] = true;
1443
        }
1444 526
    }
1445
1446
    /**
1447
     * Validates & completes the basic mapping information that is common to all
1448
     * association mappings (one-to-one, many-ot-one, one-to-many, many-to-many).
1449
     *
1450
     * @param array $mapping The mapping.
1451
     *
1452
     * @return array The updated mapping.
1453
     *
1454
     * @throws MappingException If something is wrong with the mapping.
1455
     */
1456 350
    protected function _validateAndCompleteAssociationMapping(array $mapping)
1457
    {
1458 350
        if ( ! isset($mapping['mappedBy'])) {
1459 336
            $mapping['mappedBy'] = null;
1460
        }
1461
1462 350
        if ( ! isset($mapping['inversedBy'])) {
1463 323
            $mapping['inversedBy'] = null;
1464
        }
1465
1466 350
        $mapping['isOwningSide'] = true; // assume owning side until we hit mappedBy
1467
1468 350
        if (empty($mapping['indexBy'])) {
1469 347
            unset($mapping['indexBy']);
1470
        }
1471
1472
        // If targetEntity is unqualified, assume it is in the same namespace as
1473
        // the sourceEntity.
1474 350
        $mapping['sourceEntity'] = $this->name;
1475
1476 350
        if (isset($mapping['targetEntity'])) {
1477 350
            $mapping['targetEntity'] = $this->fullyQualifiedClassName($mapping['targetEntity']);
1478 350
            $mapping['targetEntity'] = ltrim($mapping['targetEntity'], '\\');
1479
        }
1480
1481 350
        if (($mapping['type'] & self::MANY_TO_ONE) > 0 && isset($mapping['orphanRemoval']) && $mapping['orphanRemoval'] == true) {
1482 1
            throw MappingException::illegalOrphanRemoval($this->name, $mapping['fieldName']);
1483
        }
1484
1485
        // Complete id mapping
1486 349
        if (isset($mapping['id']) && $mapping['id'] === true) {
1487 53
            if (isset($mapping['orphanRemoval']) && $mapping['orphanRemoval'] == true) {
1488 1
                throw MappingException::illegalOrphanRemovalOnIdentifierAssociation($this->name, $mapping['fieldName']);
1489
            }
1490
1491 52
            if ( ! in_array($mapping['fieldName'], $this->identifier)) {
1492 52
                if (isset($mapping['joinColumns']) && count($mapping['joinColumns']) >= 2) {
1493
                    throw MappingException::cannotMapCompositePrimaryKeyEntitiesAsForeignId(
1494
                        $mapping['targetEntity'], $this->name, $mapping['fieldName']
1495
                    );
1496
                }
1497
1498 52
                $this->identifier[] = $mapping['fieldName'];
1499 52
                $this->containsForeignIdentifier = true;
1500
            }
1501
1502
            // Check for composite key
1503 52
            if ( ! $this->isIdentifierComposite && count($this->identifier) > 1) {
1504 24
                $this->isIdentifierComposite = true;
1505
            }
1506
1507 52
            if ($this->cache && !isset($mapping['cache'])) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->cache of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
1508 3
                throw CacheException::nonCacheableEntityAssociation($this->name, $mapping['fieldName']);
1509
            }
1510
        }
1511
1512
        // Mandatory attributes for both sides
1513
        // Mandatory: fieldName, targetEntity
1514 345
        if ( ! isset($mapping['fieldName']) || strlen($mapping['fieldName']) == 0) {
1515
            throw MappingException::missingFieldName($this->name);
1516
        }
1517
1518 345
        if ( ! isset($mapping['targetEntity'])) {
1519
            throw MappingException::missingTargetEntity($mapping['fieldName']);
1520
        }
1521
1522
        // Mandatory and optional attributes for either side
1523 345
        if ( ! $mapping['mappedBy']) {
1524 331
            if (isset($mapping['joinTable']) && $mapping['joinTable']) {
1525 119
                if (isset($mapping['joinTable']['name']) && $mapping['joinTable']['name'][0] === '`') {
1526 4
                    $mapping['joinTable']['name']   = trim($mapping['joinTable']['name'], '`');
1527 331
                    $mapping['joinTable']['quoted'] = true;
1528
                }
1529
            }
1530
        } else {
1531 182
            $mapping['isOwningSide'] = false;
1532
        }
1533
1534 345
        if (isset($mapping['id']) && $mapping['id'] === true && $mapping['type'] & self::TO_MANY) {
1535 3
            throw MappingException::illegalToManyIdentifierAssociation($this->name, $mapping['fieldName']);
1536
        }
1537
1538
        // Fetch mode. Default fetch mode to LAZY, if not set.
1539 342
        if ( ! isset($mapping['fetch'])) {
1540 95
            $mapping['fetch'] = self::FETCH_LAZY;
1541
        }
1542
1543
        // Cascades
1544 342
        $cascades = isset($mapping['cascade']) ? array_map('strtolower', $mapping['cascade']) : array();
1545
1546 342
        $allCascades = array('remove', 'persist', 'refresh', 'merge', 'detach');
1547 342
        if (in_array('all', $cascades)) {
1548 36
            $cascades = $allCascades;
1549 335
        } elseif (count($cascades) !== count(array_intersect($cascades, $allCascades))) {
1550 1
            throw MappingException::invalidCascadeOption(
1551
                array_diff($cascades, $allCascades),
1552 1
                $this->name,
1553 1
                $mapping['fieldName']
1554
            );
1555
        }
1556
1557 341
        $mapping['cascade'] = $cascades;
1558 341
        $mapping['isCascadeRemove'] = in_array('remove', $cascades);
1559 341
        $mapping['isCascadePersist'] = in_array('persist', $cascades);
1560 341
        $mapping['isCascadeRefresh'] = in_array('refresh', $cascades);
1561 341
        $mapping['isCascadeMerge'] = in_array('merge', $cascades);
1562 341
        $mapping['isCascadeDetach'] = in_array('detach', $cascades);
1563
1564 341
        return $mapping;
1565
    }
1566
1567
    /**
1568
     * Validates & completes a one-to-one association mapping.
1569
     *
1570
     * @param array $mapping The mapping to validate & complete.
1571
     *
1572
     * @return array The validated & completed mapping.
1573
     *
1574
     * @throws RuntimeException
1575
     * @throws MappingException
1576
     */
1577 297
    protected function _validateAndCompleteOneToOneMapping(array $mapping)
1578
    {
1579 297
        $mapping = $this->_validateAndCompleteAssociationMapping($mapping);
1580
1581 291
        if (isset($mapping['joinColumns']) && $mapping['joinColumns']) {
1582 210
            $mapping['isOwningSide'] = true;
1583
        }
1584
1585 291
        if ($mapping['isOwningSide']) {
1586 278
            if (empty($mapping['joinColumns'])) {
1587
                // Apply default join column
1588 89
                $mapping['joinColumns'] = array(
1589
                    array(
1590 89
                        'name' => $this->namingStrategy->joinColumnName($mapping['fieldName'], $this->name),
0 ignored issues
show
Unused Code introduced by
The call to NamingStrategy::joinColumnName() has too many arguments starting with $this->name.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
1591 89
                        'referencedColumnName' => $this->namingStrategy->referenceColumnName()
1592
                    )
1593
                );
1594
            }
1595
1596 278
            $uniqueConstraintColumns = array();
1597
1598 278
            foreach ($mapping['joinColumns'] as &$joinColumn) {
1599 278
                if ($mapping['type'] === self::ONE_TO_ONE && ! $this->isInheritanceTypeSingleTable()) {
1600 150
                    if (count($mapping['joinColumns']) === 1) {
1601 148
                        if (empty($mapping['id'])) {
1602 148
                            $joinColumn['unique'] = true;
1603
                        }
1604
                    } else {
1605 2
                        $uniqueConstraintColumns[] = $joinColumn['name'];
1606
                    }
1607
                }
1608
1609 278
                if (empty($joinColumn['name'])) {
1610 29
                    $joinColumn['name'] = $this->namingStrategy->joinColumnName($mapping['fieldName'], $this->name);
0 ignored issues
show
Unused Code introduced by
The call to NamingStrategy::joinColumnName() has too many arguments starting with $this->name.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
1611
                }
1612
1613 278
                if (empty($joinColumn['referencedColumnName'])) {
1614 5
                    $joinColumn['referencedColumnName'] = $this->namingStrategy->referenceColumnName();
1615
                }
1616
1617 278
                if ($joinColumn['name'][0] === '`') {
1618 7
                    $joinColumn['name']   = trim($joinColumn['name'], '`');
1619 7
                    $joinColumn['quoted'] = true;
1620
                }
1621
1622 278
                if ($joinColumn['referencedColumnName'][0] === '`') {
1623 4
                    $joinColumn['referencedColumnName'] = trim($joinColumn['referencedColumnName'], '`');
1624 4
                    $joinColumn['quoted']               = true;
1625
                }
1626
1627 278
                $mapping['sourceToTargetKeyColumns'][$joinColumn['name']] = $joinColumn['referencedColumnName'];
1628 278
                $mapping['joinColumnFieldNames'][$joinColumn['name']] = isset($joinColumn['fieldName'])
1629
                    ? $joinColumn['fieldName']
1630 278
                    : $joinColumn['name'];
1631
            }
1632
1633 278
            if ($uniqueConstraintColumns) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $uniqueConstraintColumns of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
1634 2
                if ( ! $this->table) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->table of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
1635
                    throw new RuntimeException("ClassMetadataInfo::setTable() has to be called before defining a one to one relationship.");
1636
                }
1637
1638 2
                $this->table['uniqueConstraints'][$mapping['fieldName'] . "_uniq"] = array(
1639 2
                    'columns' => $uniqueConstraintColumns
1640
                );
1641
            }
1642
1643 278
            $mapping['targetToSourceKeyColumns'] = array_flip($mapping['sourceToTargetKeyColumns']);
1644
        }
1645
1646 291
        $mapping['orphanRemoval']   = isset($mapping['orphanRemoval']) && $mapping['orphanRemoval'];
1647 291
        $mapping['isCascadeRemove'] = $mapping['orphanRemoval'] || $mapping['isCascadeRemove'];
1648
1649 291
        if ($mapping['orphanRemoval']) {
1650 21
            unset($mapping['unique']);
1651
        }
1652
1653 291
        if (isset($mapping['id']) && $mapping['id'] === true && !$mapping['isOwningSide']) {
1654 2
            throw MappingException::illegalInverseIdentifierAssociation($this->name, $mapping['fieldName']);
1655
        }
1656
1657 289
        return $mapping;
1658
    }
1659
1660
    /**
1661
     * Validates & completes a one-to-many association mapping.
1662
     *
1663
     * @param array $mapping The mapping to validate and complete.
1664
     *
1665
     * @return array The validated and completed mapping.
1666
     *
1667
     * @throws MappingException
1668
     * @throws InvalidArgumentException
1669
     */
1670 128
    protected function _validateAndCompleteOneToManyMapping(array $mapping)
1671
    {
1672 128
        $mapping = $this->_validateAndCompleteAssociationMapping($mapping);
1673
1674
        // OneToMany-side MUST be inverse (must have mappedBy)
1675 127
        if ( ! isset($mapping['mappedBy'])) {
1676
            throw MappingException::oneToManyRequiresMappedBy($mapping['fieldName']);
1677
        }
1678
1679 127
        $mapping['orphanRemoval']   = isset($mapping['orphanRemoval']) && $mapping['orphanRemoval'];
1680 127
        $mapping['isCascadeRemove'] = $mapping['orphanRemoval'] || $mapping['isCascadeRemove'];
1681
1682 127
        $this->assertMappingOrderBy($mapping);
1683
1684 127
        return $mapping;
1685
    }
1686
1687
    /**
1688
     * Validates & completes a many-to-many association mapping.
1689
     *
1690
     * @param array $mapping The mapping to validate & complete.
1691
     *
1692
     * @return array The validated & completed mapping.
1693
     *
1694
     * @throws \InvalidArgumentException
1695
     */
1696 149
    protected function _validateAndCompleteManyToManyMapping(array $mapping)
1697
    {
1698 149
        $mapping = $this->_validateAndCompleteAssociationMapping($mapping);
1699
1700 147
        if ($mapping['isOwningSide']) {
1701
            // owning side MUST have a join table
1702 129
            if ( ! isset($mapping['joinTable']['name'])) {
1703 22
                $mapping['joinTable']['name'] = $this->namingStrategy->joinTableName($mapping['sourceEntity'], $mapping['targetEntity'], $mapping['fieldName']);
1704
            }
1705
1706 129
            $selfReferencingEntityWithoutJoinColumns = $mapping['sourceEntity'] == $mapping['targetEntity']
1707 129
                && (! (isset($mapping['joinTable']['joinColumns']) || isset($mapping['joinTable']['inverseJoinColumns'])));
1708
1709 129
            if ( ! isset($mapping['joinTable']['joinColumns'])) {
1710 21
                $mapping['joinTable']['joinColumns'] = array(
1711
                    array(
1712 21
                        'name' => $this->namingStrategy->joinKeyColumnName($mapping['sourceEntity'], $selfReferencingEntityWithoutJoinColumns ? 'source' : null),
1713 21
                        'referencedColumnName' => $this->namingStrategy->referenceColumnName(),
1714 21
                        'onDelete' => 'CASCADE'
1715
                    )
1716
                );
1717
            }
1718
1719 129
            if ( ! isset($mapping['joinTable']['inverseJoinColumns'])) {
1720 22
                $mapping['joinTable']['inverseJoinColumns'] = array(
1721
                    array(
1722 22
                        'name' => $this->namingStrategy->joinKeyColumnName($mapping['targetEntity'], $selfReferencingEntityWithoutJoinColumns ? 'target' : null),
1723 22
                        'referencedColumnName' => $this->namingStrategy->referenceColumnName(),
1724 22
                        'onDelete' => 'CASCADE'
1725
                    )
1726
                );
1727
            }
1728
1729 129
            $mapping['joinTableColumns'] = array();
1730
1731 129
            foreach ($mapping['joinTable']['joinColumns'] as &$joinColumn) {
1732 129
                if (empty($joinColumn['name'])) {
1733 2
                    $joinColumn['name'] = $this->namingStrategy->joinKeyColumnName($mapping['sourceEntity'], $joinColumn['referencedColumnName']);
0 ignored issues
show
Documentation introduced by
$mapping['sourceEntity'] is of type array|boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1734
                }
1735
1736 129
                if (empty($joinColumn['referencedColumnName'])) {
1737 6
                    $joinColumn['referencedColumnName'] = $this->namingStrategy->referenceColumnName();
1738
                }
1739
1740 129
                if ($joinColumn['name'][0] === '`') {
1741 3
                    $joinColumn['name']   = trim($joinColumn['name'], '`');
1742 3
                    $joinColumn['quoted'] = true;
1743
                }
1744
1745 129
                if ($joinColumn['referencedColumnName'][0] === '`') {
1746 3
                    $joinColumn['referencedColumnName'] = trim($joinColumn['referencedColumnName'], '`');
1747 3
                    $joinColumn['quoted']               = true;
1748
                }
1749
1750 129
                if (isset($joinColumn['onDelete']) && strtolower($joinColumn['onDelete']) == 'cascade') {
1751 30
                    $mapping['isOnDeleteCascade'] = true;
1752
                }
1753
1754 129
                $mapping['relationToSourceKeyColumns'][$joinColumn['name']] = $joinColumn['referencedColumnName'];
1755 129
                $mapping['joinTableColumns'][] = $joinColumn['name'];
1756
            }
1757
1758 129
            foreach ($mapping['joinTable']['inverseJoinColumns'] as &$inverseJoinColumn) {
1759 129
                if (empty($inverseJoinColumn['name'])) {
1760 2
                    $inverseJoinColumn['name'] = $this->namingStrategy->joinKeyColumnName($mapping['targetEntity'], $inverseJoinColumn['referencedColumnName']);
0 ignored issues
show
Documentation introduced by
$mapping['targetEntity'] is of type array|boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1761
                }
1762
1763 129
                if (empty($inverseJoinColumn['referencedColumnName'])) {
1764 6
                    $inverseJoinColumn['referencedColumnName'] = $this->namingStrategy->referenceColumnName();
1765
                }
1766
1767 129
                if ($inverseJoinColumn['name'][0] === '`') {
1768 3
                    $inverseJoinColumn['name']   = trim($inverseJoinColumn['name'], '`');
1769 3
                    $inverseJoinColumn['quoted'] = true;
1770
                }
1771
1772 129
                if ($inverseJoinColumn['referencedColumnName'][0] === '`') {
1773 3
                    $inverseJoinColumn['referencedColumnName']  = trim($inverseJoinColumn['referencedColumnName'], '`');
1774 3
                    $inverseJoinColumn['quoted']                = true;
1775
                }
1776
1777 129
                if (isset($inverseJoinColumn['onDelete']) && strtolower($inverseJoinColumn['onDelete']) == 'cascade') {
1778 26
                    $mapping['isOnDeleteCascade'] = true;
1779
                }
1780
1781 129
                $mapping['relationToTargetKeyColumns'][$inverseJoinColumn['name']] = $inverseJoinColumn['referencedColumnName'];
1782 129
                $mapping['joinTableColumns'][] = $inverseJoinColumn['name'];
1783
            }
1784
        }
1785
1786 147
        $mapping['orphanRemoval'] = isset($mapping['orphanRemoval']) && $mapping['orphanRemoval'];
1787
1788 147
        $this->assertMappingOrderBy($mapping);
1789
1790 147
        return $mapping;
1791
    }
1792
1793
    /**
1794
     * {@inheritDoc}
1795
     */
1796 596
    public function getIdentifierFieldNames()
1797
    {
1798 596
        return $this->identifier;
1799
    }
1800
1801
    /**
1802
     * Gets the name of the single id field. Note that this only works on
1803
     * entity classes that have a single-field pk.
1804
     *
1805
     * @return string
1806
     *
1807
     * @throws MappingException If the class has a composite primary key.
1808
     */
1809 396
    public function getSingleIdentifierFieldName()
1810
    {
1811 396
        if ($this->isIdentifierComposite) {
1812 1
            throw MappingException::singleIdNotAllowedOnCompositePrimaryKey($this->name);
1813
        }
1814
1815 395
        return $this->identifier[0];
1816
    }
1817
1818
    /**
1819
     * Gets the column name of the single id column. Note that this only works on
1820
     * entity classes that have a single-field pk.
1821
     *
1822
     * @return string
1823
     *
1824
     * @throws MappingException If the class has a composite primary key.
1825
     */
1826 3
    public function getSingleIdentifierColumnName()
1827
    {
1828 3
        return $this->getColumnName($this->getSingleIdentifierFieldName());
1829
    }
1830
1831
    /**
1832
     * INTERNAL:
1833
     * Sets the mapped identifier/primary key fields of this class.
1834
     * Mainly used by the ClassMetadataFactory to assign inherited identifiers.
1835
     *
1836
     * @param array $identifier
1837
     *
1838
     * @return void
1839
     */
1840 124
    public function setIdentifier(array $identifier)
1841
    {
1842 124
        $this->identifier = $identifier;
1843 124
        $this->isIdentifierComposite = (count($this->identifier) > 1);
1844 124
    }
1845
1846
    /**
1847
     * {@inheritDoc}
1848
     */
1849 62
    public function getIdentifier()
1850
    {
1851 62
        return $this->identifier;
1852
    }
1853
1854
    /**
1855
     * {@inheritDoc}
1856
     */
1857 292
    public function hasField($fieldName)
1858
    {
1859 292
        return isset($this->fieldMappings[$fieldName]);
1860
    }
1861
1862
    /**
1863
     * Gets an array containing all the column names.
1864
     *
1865
     * @param array|null $fieldNames
1866
     *
1867
     * @return array
1868
     */
1869 43
    public function getColumnNames(array $fieldNames = null)
1870
    {
1871 43
        if (null === $fieldNames) {
1872 42
            return array_keys($this->fieldNames);
1873
        }
1874
1875 1
        return array_values(array_map([$this, 'getColumnName'], $fieldNames));
1876
    }
1877
1878
    /**
1879
     * Returns an array with all the identifier column names.
1880
     *
1881
     * @return array
1882
     */
1883 322
    public function getIdentifierColumnNames()
1884
    {
1885 322
        $columnNames = array();
1886
1887 322
        foreach ($this->identifier as $idProperty) {
1888 322
            if (isset($this->fieldMappings[$idProperty])) {
1889 318
                $columnNames[] = $this->fieldMappings[$idProperty]['columnName'];
1890
1891 318
                continue;
1892
            }
1893
1894
            // Association defined as Id field
1895 22
            $joinColumns      = $this->associationMappings[$idProperty]['joinColumns'];
1896
            $assocColumnNames = array_map(function ($joinColumn) { return $joinColumn['name']; }, $joinColumns);
1897
1898 22
            $columnNames = array_merge($columnNames, $assocColumnNames);
1899
        }
1900
1901 322
        return $columnNames;
1902
    }
1903
1904
    /**
1905
     * Sets the type of Id generator to use for the mapped class.
1906
     *
1907
     * @param int $generatorType
1908
     *
1909
     * @return void
1910
     */
1911 457
    public function setIdGeneratorType($generatorType)
1912
    {
1913 457
        $this->generatorType = $generatorType;
1914 457
    }
1915
1916
    /**
1917
     * Checks whether the mapped class uses an Id generator.
1918
     *
1919
     * @return boolean TRUE if the mapped class uses an Id generator, FALSE otherwise.
1920
     */
1921 392
    public function usesIdGenerator()
1922
    {
1923 392
        return $this->generatorType != self::GENERATOR_TYPE_NONE;
1924
    }
1925
1926
    /**
1927
     * @return boolean
1928
     */
1929 1324
    public function isInheritanceTypeNone()
1930
    {
1931 1324
        return $this->inheritanceType == self::INHERITANCE_TYPE_NONE;
1932
    }
1933
1934
    /**
1935
     * Checks whether the mapped class uses the JOINED inheritance mapping strategy.
1936
     *
1937
     * @return boolean TRUE if the class participates in a JOINED inheritance mapping,
1938
     *                 FALSE otherwise.
1939
     */
1940 1052
    public function isInheritanceTypeJoined()
1941
    {
1942 1052
        return $this->inheritanceType == self::INHERITANCE_TYPE_JOINED;
1943
    }
1944
1945
    /**
1946
     * Checks whether the mapped class uses the SINGLE_TABLE inheritance mapping strategy.
1947
     *
1948
     * @return boolean TRUE if the class participates in a SINGLE_TABLE inheritance mapping,
1949
     *                 FALSE otherwise.
1950
     */
1951 1224
    public function isInheritanceTypeSingleTable()
1952
    {
1953 1224
        return $this->inheritanceType == self::INHERITANCE_TYPE_SINGLE_TABLE;
1954
    }
1955
1956
    /**
1957
     * Checks whether the mapped class uses the TABLE_PER_CLASS inheritance mapping strategy.
1958
     *
1959
     * @return boolean TRUE if the class participates in a TABLE_PER_CLASS inheritance mapping,
1960
     *                 FALSE otherwise.
1961
     */
1962 260
    public function isInheritanceTypeTablePerClass()
1963
    {
1964 260
        return $this->inheritanceType == self::INHERITANCE_TYPE_TABLE_PER_CLASS;
1965
    }
1966
1967
    /**
1968
     * Checks whether the class uses an identity column for the Id generation.
1969
     *
1970
     * @return boolean TRUE if the class uses the IDENTITY generator, FALSE otherwise.
1971
     */
1972 1063
    public function isIdGeneratorIdentity()
1973
    {
1974 1063
        return $this->generatorType == self::GENERATOR_TYPE_IDENTITY;
1975
    }
1976
1977
    /**
1978
     * Checks whether the class uses a sequence for id generation.
1979
     *
1980
     * @return boolean TRUE if the class uses the SEQUENCE generator, FALSE otherwise.
1981
     */
1982 313
    public function isIdGeneratorSequence()
1983
    {
1984 313
        return $this->generatorType == self::GENERATOR_TYPE_SEQUENCE;
1985
    }
1986
1987
    /**
1988
     * Checks whether the class uses a table for id generation.
1989
     *
1990
     * @return boolean TRUE if the class uses the TABLE generator, FALSE otherwise.
1991
     */
1992 80
    public function isIdGeneratorTable()
1993
    {
1994 80
        return $this->generatorType == self::GENERATOR_TYPE_TABLE;
1995
    }
1996
1997
    /**
1998
     * Checks whether the class has a natural identifier/pk (which means it does
1999
     * not use any Id generator.
2000
     *
2001
     * @return boolean
2002
     */
2003 73
    public function isIdentifierNatural()
2004
    {
2005 73
        return $this->generatorType == self::GENERATOR_TYPE_NONE;
2006
    }
2007
2008
    /**
2009
     * Checks whether the class use a UUID for id generation.
2010
     *
2011
     * @return boolean
2012
     */
2013
    public function isIdentifierUuid()
2014
    {
2015
        return $this->generatorType == self::GENERATOR_TYPE_UUID;
2016
    }
2017
2018
    /**
2019
     * Gets the type of a field.
2020
     *
2021
     * @param string $fieldName
2022
     *
2023
     * @return \Doctrine\DBAL\Types\Type|string|null
2024
     *
2025
     * @todo 3.0 Remove this. PersisterHelper should fix it somehow
2026
     */
2027 38
    public function getTypeOfField($fieldName)
2028
    {
2029 38
        return isset($this->fieldMappings[$fieldName])
2030 38
            ? $this->fieldMappings[$fieldName]['type']
2031 38
            : null;
2032
    }
2033
2034
    /**
2035
     * Gets the type of a column.
2036
     *
2037
     * @param string $columnName
2038
     *
2039
     * @return \Doctrine\DBAL\Types\Type|string|null
2040
     *
2041
     * @deprecated 3.0 remove this. this method is bogous and unreliable, since it cannot resolve the type of a column
2042
     *             that is derived by a referenced field on a different entity.
2043
     */
2044
    public function getTypeOfColumn($columnName)
2045
    {
2046
        return $this->getTypeOfField($this->getFieldName($columnName));
2047
    }
2048
2049
    /**
2050
     * Gets the name of the primary table.
2051
     *
2052
     * @return string
2053
     */
2054 1465
    public function getTableName()
2055
    {
2056 1465
        return $this->table['name'];
2057
    }
2058
2059
    /**
2060
     * Gets primary table's schema name.
2061
     *
2062
     * @return string|null
2063
     */
2064 13
    public function getSchemaName()
2065
    {
2066 13
        return isset($this->table['schema']) ? $this->table['schema'] : null;
2067
    }
2068
2069
    /**
2070
     * Gets the table name to use for temporary identifier tables of this class.
2071
     *
2072
     * @return string
2073
     */
2074 7
    public function getTemporaryIdTableName()
2075
    {
2076
        // replace dots with underscores because PostgreSQL creates temporary tables in a special schema
2077 7
        return str_replace('.', '_', $this->getTableName() . '_id_tmp');
2078
    }
2079
2080
    /**
2081
     * Sets the mapped subclasses of this class.
2082
     *
2083
     * @param array $subclasses The names of all mapped subclasses.
2084
     *
2085
     * @return void
2086
     */
2087 2
    public function setSubclasses(array $subclasses)
2088
    {
2089 2
        foreach ($subclasses as $subclass) {
2090 2
            $this->subClasses[] = $this->fullyQualifiedClassName($subclass);
2091
        }
2092 2
    }
2093
2094
    /**
2095
     * Sets the parent class names.
2096
     * Assumes that the class names in the passed array are in the order:
2097
     * directParent -> directParentParent -> directParentParentParent ... -> root.
2098
     *
2099
     * @param array $classNames
2100
     *
2101
     * @return void
2102
     */
2103 409
    public function setParentClasses(array $classNames)
2104
    {
2105 409
        $this->parentClasses = $classNames;
2106
2107 409
        if (count($classNames) > 0) {
2108 79
            $this->rootEntityName = array_pop($classNames);
2109
        }
2110 409
    }
2111
2112
    /**
2113
     * Sets the inheritance type used by the class and its subclasses.
2114
     *
2115
     * @param integer $type
2116
     *
2117
     * @return void
2118
     *
2119
     * @throws MappingException
2120
     */
2121 170
    public function setInheritanceType($type)
2122
    {
2123 170
        if ( ! $this->_isInheritanceType($type)) {
2124
            throw MappingException::invalidInheritanceType($this->name, $type);
2125
        }
2126
2127 170
        $this->inheritanceType = $type;
2128 170
    }
2129
2130
    /**
2131
     * Sets the association to override association mapping of property for an entity relationship.
2132
     *
2133
     * @param string $fieldName
2134
     * @param array  $overrideMapping
2135
     *
2136
     * @return void
2137
     *
2138
     * @throws MappingException
2139
     */
2140 20
    public function setAssociationOverride($fieldName, array $overrideMapping)
2141
    {
2142 20
        if ( ! isset($this->associationMappings[$fieldName])) {
2143 1
            throw MappingException::invalidOverrideFieldName($this->name, $fieldName);
2144
        }
2145
2146 19
        $mapping = $this->associationMappings[$fieldName];
2147
2148 19
        if (isset($overrideMapping['joinColumns'])) {
2149 13
            $mapping['joinColumns'] = $overrideMapping['joinColumns'];
2150
        }
2151
2152 19
        if (isset($overrideMapping['inversedBy'])) {
2153 6
            $mapping['inversedBy'] = $overrideMapping['inversedBy'];
2154
        }
2155
2156 19
        if (isset($overrideMapping['joinTable'])) {
2157 12
            $mapping['joinTable'] = $overrideMapping['joinTable'];
2158
        }
2159
2160 19
        $mapping['joinColumnFieldNames']        = null;
2161 19
        $mapping['joinTableColumns']            = null;
2162 19
        $mapping['sourceToTargetKeyColumns']    = null;
2163 19
        $mapping['relationToSourceKeyColumns']  = null;
2164 19
        $mapping['relationToTargetKeyColumns']  = null;
2165
2166 19
        switch ($mapping['type']) {
2167 19
            case self::ONE_TO_ONE:
2168 1
                $mapping = $this->_validateAndCompleteOneToOneMapping($mapping);
2169 1
                break;
2170 18
            case self::ONE_TO_MANY:
2171
                $mapping = $this->_validateAndCompleteOneToManyMapping($mapping);
2172
                break;
2173 18
            case self::MANY_TO_ONE:
2174 12
                $mapping = $this->_validateAndCompleteOneToOneMapping($mapping);
2175 12
                break;
2176 18
            case self::MANY_TO_MANY:
2177 18
                $mapping = $this->_validateAndCompleteManyToManyMapping($mapping);
2178 18
                break;
2179
        }
2180
2181 19
        $this->associationMappings[$fieldName] = $mapping;
2182 19
    }
2183
2184
    /**
2185
     * Sets the override for a mapped field.
2186
     *
2187
     * @param string $fieldName
2188
     * @param array  $overrideMapping
2189
     *
2190
     * @return void
2191
     *
2192
     * @throws MappingException
2193
     */
2194 15
    public function setAttributeOverride($fieldName, array $overrideMapping)
2195
    {
2196 15
        if ( ! isset($this->fieldMappings[$fieldName])) {
2197 1
            throw MappingException::invalidOverrideFieldName($this->name, $fieldName);
2198
        }
2199
2200 14
        $mapping = $this->fieldMappings[$fieldName];
2201
2202 14
        if (isset($mapping['id'])) {
2203 12
            $overrideMapping['id'] = $mapping['id'];
2204
        }
2205
2206 14
        if ( ! isset($overrideMapping['type'])) {
2207 6
            $overrideMapping['type'] = $mapping['type'];
2208
        }
2209
2210 14
        if ( ! isset($overrideMapping['fieldName'])) {
2211 5
            $overrideMapping['fieldName'] = $mapping['fieldName'];
2212
        }
2213
2214 14
        if ($overrideMapping['type'] !== $mapping['type']) {
2215 1
            throw MappingException::invalidOverrideFieldType($this->name, $fieldName);
2216
        }
2217
2218 13
        unset($this->fieldMappings[$fieldName]);
2219 13
        unset($this->fieldNames[$mapping['columnName']]);
2220 13
        unset($this->columnNames[$mapping['fieldName']]);
2221
2222 13
        $this->_validateAndCompleteFieldMapping($overrideMapping);
2223
2224 13
        $this->fieldMappings[$fieldName] = $overrideMapping;
2225 13
    }
2226
2227
    /**
2228
     * Checks whether a mapped field is inherited from an entity superclass.
2229
     *
2230
     * @param string $fieldName
2231
     *
2232
     * @return bool TRUE if the field is inherited, FALSE otherwise.
2233
     */
2234 369
    public function isInheritedField($fieldName)
2235
    {
2236 369
        return isset($this->fieldMappings[$fieldName]['inherited']);
2237
    }
2238
2239
    /**
2240
     * Checks if this entity is the root in any entity-inheritance-hierarchy.
2241
     *
2242
     * @return bool
2243
     */
2244 408
    public function isRootEntity()
2245
    {
2246 408
        return $this->name == $this->rootEntityName;
2247
    }
2248
2249
    /**
2250
     * Checks whether a mapped association field is inherited from a superclass.
2251
     *
2252
     * @param string $fieldName
2253
     *
2254
     * @return boolean TRUE if the field is inherited, FALSE otherwise.
2255
     */
2256 348
    public function isInheritedAssociation($fieldName)
2257
    {
2258 348
        return isset($this->associationMappings[$fieldName]['inherited']);
2259
    }
2260
2261 348
    public function isInheritedEmbeddedClass($fieldName)
2262
    {
2263 348
        return isset($this->embeddedClasses[$fieldName]['inherited']);
2264
    }
2265
2266
    /**
2267
     * Sets the name of the primary table the class is mapped to.
2268
     *
2269
     * @param string $tableName The table name.
2270
     *
2271
     * @return void
2272
     *
2273
     * @deprecated Use {@link setPrimaryTable}.
2274
     */
2275 5
    public function setTableName($tableName)
2276
    {
2277 5
        $this->table['name'] = $tableName;
2278 5
    }
2279
2280
    /**
2281
     * Sets the primary table definition. The provided array supports the
2282
     * following structure:
2283
     *
2284
     * name => <tableName> (optional, defaults to class name)
2285
     * indexes => array of indexes (optional)
2286
     * uniqueConstraints => array of constraints (optional)
2287
     *
2288
     * If a key is omitted, the current value is kept.
2289
     *
2290
     * @param array $table The table description.
2291
     *
2292
     * @return void
2293
     */
2294 324
    public function setPrimaryTable(array $table)
2295
    {
2296 324
        if (isset($table['name'])) {
2297
            // Split schema and table name from a table name like "myschema.mytable"
2298 261
            if (strpos($table['name'], '.') !== false) {
2299 9
                list($this->table['schema'], $table['name']) = explode('.', $table['name'], 2);
2300
            }
2301
2302 261
            if ($table['name'][0] === '`') {
2303 17
                $table['name']          = trim($table['name'], '`');
2304 17
                $this->table['quoted']  = true;
2305
            }
2306
2307 261
            $this->table['name'] = $table['name'];
2308
        }
2309
2310 324
        if (isset($table['schema'])) {
2311 6
            $this->table['schema'] = $table['schema'];
2312
        }
2313
2314 324
        if (isset($table['indexes'])) {
2315 15
            $this->table['indexes'] = $table['indexes'];
2316
        }
2317
2318 324
        if (isset($table['uniqueConstraints'])) {
2319 8
            $this->table['uniqueConstraints'] = $table['uniqueConstraints'];
2320
        }
2321
2322 324
        if (isset($table['options'])) {
2323 9
            $this->table['options'] = $table['options'];
2324
        }
2325 324
    }
2326
2327
    /**
2328
     * Checks whether the given type identifies an inheritance type.
2329
     *
2330
     * @param integer $type
2331
     *
2332
     * @return boolean TRUE if the given type identifies an inheritance type, FALSe otherwise.
2333
     */
2334 170
    private function _isInheritanceType($type)
2335
    {
2336 170
        return $type == self::INHERITANCE_TYPE_NONE ||
2337 105
                $type == self::INHERITANCE_TYPE_SINGLE_TABLE ||
2338 50
                $type == self::INHERITANCE_TYPE_JOINED ||
2339 170
                $type == self::INHERITANCE_TYPE_TABLE_PER_CLASS;
2340
    }
2341
2342
    /**
2343
     * Adds a mapped field to the class.
2344
     *
2345
     * @param array $mapping The field mapping.
2346
     *
2347
     * @return void
2348
     *
2349
     * @throws MappingException
2350
     */
2351 528
    public function mapField(array $mapping)
2352
    {
2353 528
        $this->_validateAndCompleteFieldMapping($mapping);
2354 526
        $this->assertFieldNotMapped($mapping['fieldName']);
2355
2356 525
        $this->fieldMappings[$mapping['fieldName']] = $mapping;
2357 525
    }
2358
2359
    /**
2360
     * INTERNAL:
2361
     * Adds an association mapping without completing/validating it.
2362
     * This is mainly used to add inherited association mappings to derived classes.
2363
     *
2364
     * @param array $mapping
2365
     *
2366
     * @return void
2367
     *
2368
     * @throws MappingException
2369
     */
2370 48
    public function addInheritedAssociationMapping(array $mapping/*, $owningClassName = null*/)
2371
    {
2372 48
        if (isset($this->associationMappings[$mapping['fieldName']])) {
2373 1
            throw MappingException::duplicateAssociationMapping($this->name, $mapping['fieldName']);
2374
        }
2375 48
        $this->associationMappings[$mapping['fieldName']] = $mapping;
2376 48
    }
2377
2378
    /**
2379
     * INTERNAL:
2380
     * Adds a field mapping without completing/validating it.
2381
     * This is mainly used to add inherited field mappings to derived classes.
2382
     *
2383
     * @param array $fieldMapping
2384
     *
2385
     * @return void
2386
     */
2387 108
    public function addInheritedFieldMapping(array $fieldMapping)
2388
    {
2389 108
        $this->fieldMappings[$fieldMapping['fieldName']] = $fieldMapping;
2390 108
        $this->columnNames[$fieldMapping['fieldName']] = $fieldMapping['columnName'];
0 ignored issues
show
Deprecated Code introduced by
The property Doctrine\ORM\Mapping\Cla...adataInfo::$columnNames has been deprecated with message: 3.0 Remove this.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
2391 108
        $this->fieldNames[$fieldMapping['columnName']] = $fieldMapping['fieldName'];
2392 108
    }
2393
2394
    /**
2395
     * INTERNAL:
2396
     * Adds a named query to this class.
2397
     *
2398
     * @param array $queryMapping
2399
     *
2400
     * @return void
2401
     *
2402
     * @throws MappingException
2403
     */
2404 29
    public function addNamedQuery(array $queryMapping)
2405
    {
2406 29
        if (!isset($queryMapping['name'])) {
2407 2
            throw MappingException::nameIsMandatoryForQueryMapping($this->name);
2408
        }
2409
2410 27
        if (isset($this->namedQueries[$queryMapping['name']])) {
2411 1
            throw MappingException::duplicateQueryMapping($this->name, $queryMapping['name']);
2412
        }
2413
2414 27
        if (!isset($queryMapping['query'])) {
2415
            throw MappingException::emptyQueryMapping($this->name, $queryMapping['name']);
2416
        }
2417
2418 27
        $name   = $queryMapping['name'];
2419 27
        $query  = $queryMapping['query'];
2420 27
        $dql    = str_replace('__CLASS__', $this->name, $query);
2421
2422 27
        $this->namedQueries[$name] = array(
2423 27
            'name'  => $name,
2424 27
            'query' => $query,
2425 27
            'dql'   => $dql,
2426
        );
2427 27
    }
2428
2429
    /**
2430
     * INTERNAL:
2431
     * Adds a named native query to this class.
2432
     *
2433
     * @param array $queryMapping
2434
     *
2435
     * @return void
2436
     *
2437
     * @throws MappingException
2438
     */
2439 39
    public function addNamedNativeQuery(array $queryMapping)
2440
    {
2441 39
        if (!isset($queryMapping['name'])) {
2442
            throw MappingException::nameIsMandatoryForQueryMapping($this->name);
2443
        }
2444
2445 39
        if (isset($this->namedNativeQueries[$queryMapping['name']])) {
2446 1
            throw MappingException::duplicateQueryMapping($this->name, $queryMapping['name']);
2447
        }
2448
2449 39
        if (!isset($queryMapping['query'])) {
2450
            throw MappingException::emptyQueryMapping($this->name, $queryMapping['name']);
2451
        }
2452
2453 39
        if (!isset($queryMapping['resultClass']) && !isset($queryMapping['resultSetMapping'])) {
2454
            throw MappingException::missingQueryMapping($this->name, $queryMapping['name']);
2455
        }
2456
2457 39
        $queryMapping['isSelfClass'] = false;
2458
2459 39
        if (isset($queryMapping['resultClass'])) {
2460 37
            if ($queryMapping['resultClass'] === '__CLASS__') {
2461
2462 11
                $queryMapping['isSelfClass'] = true;
2463 11
                $queryMapping['resultClass'] = $this->name;
2464
            }
2465
2466 37
            $queryMapping['resultClass'] = $this->fullyQualifiedClassName($queryMapping['resultClass']);
0 ignored issues
show
Bug introduced by
It seems like $queryMapping['resultClass'] can also be of type boolean; however, Doctrine\ORM\Mapping\Cla...llyQualifiedClassName() does only seem to accept string|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
2467 37
            $queryMapping['resultClass'] = ltrim($queryMapping['resultClass'], '\\');
2468
        }
2469
2470 39
        $this->namedNativeQueries[$queryMapping['name']] = $queryMapping;
2471 39
    }
2472
2473
    /**
2474
     * INTERNAL:
2475
     * Adds a sql result set mapping to this class.
2476
     *
2477
     * @param array $resultMapping
2478
     *
2479
     * @return void
2480
     *
2481
     * @throws MappingException
2482
     */
2483 39
    public function addSqlResultSetMapping(array $resultMapping)
2484
    {
2485 39
        if (!isset($resultMapping['name'])) {
2486
            throw MappingException::nameIsMandatoryForSqlResultSetMapping($this->name);
2487
        }
2488
2489 39
        if (isset($this->sqlResultSetMappings[$resultMapping['name']])) {
2490 1
            throw MappingException::duplicateResultSetMapping($this->name, $resultMapping['name']);
2491
        }
2492
2493 39
        if (isset($resultMapping['entities'])) {
2494 39
            foreach ($resultMapping['entities'] as $key => $entityResult) {
2495 39
                if (!isset($entityResult['entityClass'])) {
2496 1
                    throw MappingException::missingResultSetMappingEntity($this->name, $resultMapping['name']);
2497
                }
2498
2499 38
                $entityResult['isSelfClass'] = false;
2500 38
                if ($entityResult['entityClass'] === '__CLASS__') {
2501
2502 20
                    $entityResult['isSelfClass'] = true;
2503 20
                    $entityResult['entityClass'] = $this->name;
2504
2505
                }
2506
2507 38
                $entityResult['entityClass'] = $this->fullyQualifiedClassName($entityResult['entityClass']);
0 ignored issues
show
Bug introduced by
It seems like $entityResult['entityClass'] can also be of type boolean; however, Doctrine\ORM\Mapping\Cla...llyQualifiedClassName() does only seem to accept string|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
2508
2509 38
                $resultMapping['entities'][$key]['entityClass'] = ltrim($entityResult['entityClass'], '\\');
2510 38
                $resultMapping['entities'][$key]['isSelfClass'] = $entityResult['isSelfClass'];
2511
2512 38
                if (isset($entityResult['fields'])) {
2513 34
                    foreach ($entityResult['fields'] as $k => $field) {
0 ignored issues
show
Bug introduced by
The expression $entityResult['fields'] of type boolean|string is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
2514 34
                        if (!isset($field['name'])) {
2515
                            throw MappingException::missingResultSetMappingFieldName($this->name, $resultMapping['name']);
2516
                        }
2517
2518 34
                        if (!isset($field['column'])) {
2519 14
                            $fieldName = $field['name'];
2520 14
                            if (strpos($fieldName, '.')) {
2521 9
                                list(, $fieldName) = explode('.', $fieldName);
2522
                            }
2523
2524 38
                            $resultMapping['entities'][$key]['fields'][$k]['column'] = $fieldName;
2525
                        }
2526
                    }
2527
                }
2528
            }
2529
        }
2530
2531 38
        $this->sqlResultSetMappings[$resultMapping['name']] = $resultMapping;
2532 38
    }
2533
2534
    /**
2535
     * Adds a one-to-one mapping.
2536
     *
2537
     * @param array $mapping The mapping.
2538
     *
2539
     * @return void
2540
     */
2541 167
    public function mapOneToOne(array $mapping)
2542
    {
2543 167
        $mapping['type'] = self::ONE_TO_ONE;
2544
2545 167
        $mapping = $this->_validateAndCompleteOneToOneMapping($mapping);
2546
2547 164
        $this->_storeAssociationMapping($mapping);
2548 163
    }
2549
2550
    /**
2551
     * Adds a one-to-many mapping.
2552
     *
2553
     * @param array $mapping The mapping.
2554
     *
2555
     * @return void
2556
     */
2557 128
    public function mapOneToMany(array $mapping)
2558
    {
2559 128
        $mapping['type'] = self::ONE_TO_MANY;
2560
2561 128
        $mapping = $this->_validateAndCompleteOneToManyMapping($mapping);
2562
2563 127
        $this->_storeAssociationMapping($mapping);
2564 127
    }
2565
2566
    /**
2567
     * Adds a many-to-one mapping.
2568
     *
2569
     * @param array $mapping The mapping.
2570
     *
2571
     * @return void
2572
     */
2573 162
    public function mapManyToOne(array $mapping)
2574
    {
2575 162
        $mapping['type'] = self::MANY_TO_ONE;
2576
2577
        // A many-to-one mapping is essentially a one-one backreference
2578 162
        $mapping = $this->_validateAndCompleteOneToOneMapping($mapping);
2579
2580 157
        $this->_storeAssociationMapping($mapping);
2581 157
    }
2582
2583
    /**
2584
     * Adds a many-to-many mapping.
2585
     *
2586
     * @param array $mapping The mapping.
2587
     *
2588
     * @return void
2589
     */
2590 149
    public function mapManyToMany(array $mapping)
2591
    {
2592 149
        $mapping['type'] = self::MANY_TO_MANY;
2593
2594 149
        $mapping = $this->_validateAndCompleteManyToManyMapping($mapping);
2595
2596 147
        $this->_storeAssociationMapping($mapping);
2597 147
    }
2598
2599
    /**
2600
     * Stores the association mapping.
2601
     *
2602
     * @param array $assocMapping
2603
     *
2604
     * @return void
2605
     *
2606
     * @throws MappingException
2607
     */
2608 339
    protected function _storeAssociationMapping(array $assocMapping)
2609
    {
2610 339
        $sourceFieldName = $assocMapping['fieldName'];
2611
2612 339
        $this->assertFieldNotMapped($sourceFieldName);
2613
2614 338
        $this->associationMappings[$sourceFieldName] = $assocMapping;
2615 338
    }
2616
2617
    /**
2618
     * Registers a custom repository class for the entity class.
2619
     *
2620
     * @param string $repositoryClassName The class name of the custom mapper.
2621
     *
2622
     * @return void
2623
     */
2624 63
    public function setCustomRepositoryClass($repositoryClassName)
2625
    {
2626 63
        $this->customRepositoryClassName = $this->fullyQualifiedClassName($repositoryClassName);
2627 63
    }
2628
2629
    /**
2630
     * Dispatches the lifecycle event of the given entity to the registered
2631
     * lifecycle callbacks and lifecycle listeners.
2632
     *
2633
     * @deprecated Deprecated since version 2.4 in favor of \Doctrine\ORM\Event\ListenersInvoker
2634
     *
2635
     * @param string $lifecycleEvent The lifecycle event.
2636
     * @param object $entity         The Entity on which the event occurred.
2637
     *
2638
     * @return void
2639
     */
2640 1
    public function invokeLifecycleCallbacks($lifecycleEvent, $entity)
2641
    {
2642 1
        foreach ($this->lifecycleCallbacks[$lifecycleEvent] as $callback) {
2643 1
            $entity->$callback();
2644
        }
2645 1
    }
2646
2647
    /**
2648
     * Whether the class has any attached lifecycle listeners or callbacks for a lifecycle event.
2649
     *
2650
     * @param string $lifecycleEvent
2651
     *
2652
     * @return boolean
2653
     */
2654
    public function hasLifecycleCallbacks($lifecycleEvent)
2655
    {
2656
        return isset($this->lifecycleCallbacks[$lifecycleEvent]);
2657
    }
2658
2659
    /**
2660
     * Gets the registered lifecycle callbacks for an event.
2661
     *
2662
     * @param string $event
2663
     *
2664
     * @return array
2665
     */
2666
    public function getLifecycleCallbacks($event)
2667
    {
2668
        return isset($this->lifecycleCallbacks[$event]) ? $this->lifecycleCallbacks[$event] : array();
2669
    }
2670
2671
    /**
2672
     * Adds a lifecycle callback for entities of this class.
2673
     *
2674
     * @param string $callback
2675
     * @param string $event
2676
     *
2677
     * @return void
2678
     */
2679 41
    public function addLifecycleCallback($callback, $event)
2680
    {
2681 41
        if (isset($this->lifecycleCallbacks[$event]) && in_array($callback, $this->lifecycleCallbacks[$event])) {
2682 3
            return;
2683
        }
2684
2685 41
        $this->lifecycleCallbacks[$event][] = $callback;
2686 41
    }
2687
2688
    /**
2689
     * Sets the lifecycle callbacks for entities of this class.
2690
     * Any previously registered callbacks are overwritten.
2691
     *
2692
     * @param array $callbacks
2693
     *
2694
     * @return void
2695
     */
2696 123
    public function setLifecycleCallbacks(array $callbacks)
2697
    {
2698 123
        $this->lifecycleCallbacks = $callbacks;
2699 123
    }
2700
2701
    /**
2702
     * Adds a entity listener for entities of this class.
2703
     *
2704
     * @param string $eventName The entity lifecycle event.
2705
     * @param string $class     The listener class.
2706
     * @param string $method    The listener callback method.
2707
     *
2708
     * @throws \Doctrine\ORM\Mapping\MappingException
2709
     */
2710 35
    public function addEntityListener($eventName, $class, $method)
2711
    {
2712 35
        $class    = $this->fullyQualifiedClassName($class);
2713
2714
        $listener = array(
2715 35
            'class'  => $class,
2716 35
            'method' => $method,
2717
        );
2718
2719 35
        if ( ! class_exists($class)) {
2720 1
            throw MappingException::entityListenerClassNotFound($class, $this->name);
2721
        }
2722
2723 34
        if ( ! method_exists($class, $method)) {
2724 1
            throw MappingException::entityListenerMethodNotFound($class, $method, $this->name);
2725
        }
2726
2727 33
        if (isset($this->entityListeners[$eventName]) && in_array($listener, $this->entityListeners[$eventName])) {
2728 1
            throw MappingException::duplicateEntityListener($class, $method, $this->name);
2729
        }
2730
2731 33
        $this->entityListeners[$eventName][] = $listener;
2732 33
    }
2733
2734
    /**
2735
     * Sets the discriminator column definition.
2736
     *
2737
     * @param array $columnDef
2738
     *
2739
     * @return void
2740
     *
2741
     * @throws MappingException
2742
     *
2743
     * @see getDiscriminatorColumn()
2744
     */
2745 164
    public function setDiscriminatorColumn($columnDef)
2746
    {
2747 164
        if ($columnDef !== null) {
2748 109
            if ( ! isset($columnDef['name'])) {
2749 1
                throw MappingException::nameIsMandatoryForDiscriminatorColumns($this->name);
2750
            }
2751
2752 108
            if (isset($this->fieldNames[$columnDef['name']])) {
2753 1
                throw MappingException::duplicateColumnName($this->name, $columnDef['name']);
2754
            }
2755
2756 107
            if ( ! isset($columnDef['fieldName'])) {
2757 102
                $columnDef['fieldName'] = $columnDef['name'];
2758
            }
2759
2760 107
            if ( ! isset($columnDef['type'])) {
2761 2
                $columnDef['type'] = "string";
2762
            }
2763
2764 107
            if (in_array($columnDef['type'], array("boolean", "array", "object", "datetime", "time", "date"))) {
2765
                throw MappingException::invalidDiscriminatorColumnType($this->name, $columnDef['type']);
2766
            }
2767
2768 107
            $this->discriminatorColumn = $columnDef;
2769
        }
2770 162
    }
2771
2772
    /**
2773
     * Sets the discriminator values used by this class.
2774
     * Used for JOINED and SINGLE_TABLE inheritance mapping strategies.
2775
     *
2776
     * @param array $map
2777
     *
2778
     * @return void
2779
     */
2780 157
    public function setDiscriminatorMap(array $map)
2781
    {
2782 157
        foreach ($map as $value => $className) {
2783 103
            $this->addDiscriminatorMapClass($value, $className);
2784
        }
2785 157
    }
2786
2787
    /**
2788
     * Adds one entry of the discriminator map with a new class and corresponding name.
2789
     *
2790
     * @param string $name
2791
     * @param string $className
2792
     *
2793
     * @return void
2794
     *
2795
     * @throws MappingException
2796
     */
2797 104
    public function addDiscriminatorMapClass($name, $className)
2798
    {
2799 104
        $className = $this->fullyQualifiedClassName($className);
2800 104
        $className = ltrim($className, '\\');
2801
2802 104
        $this->discriminatorMap[$name] = $className;
2803
2804 104
        if ($this->name === $className) {
2805 75
            $this->discriminatorValue = $name;
2806
2807 75
            return;
2808
        }
2809
2810 103
        if ( ! (class_exists($className) || interface_exists($className))) {
2811
            throw MappingException::invalidClassInDiscriminatorMap($className, $this->name);
2812
        }
2813
2814 103
        if (is_subclass_of($className, $this->name) && ! in_array($className, $this->subClasses)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if $this->name can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
2815 94
            $this->subClasses[] = $className;
2816
        }
2817 103
    }
2818
2819
    /**
2820
     * Checks whether the class has a named query with the given query name.
2821
     *
2822
     * @param string $queryName
2823
     *
2824
     * @return boolean
2825
     */
2826 1
    public function hasNamedQuery($queryName)
2827
    {
2828 1
        return isset($this->namedQueries[$queryName]);
2829
    }
2830
2831
    /**
2832
     * Checks whether the class has a named native query with the given query name.
2833
     *
2834
     * @param string $queryName
2835
     *
2836
     * @return boolean
2837
     */
2838 1
    public function hasNamedNativeQuery($queryName)
2839
    {
2840 1
        return isset($this->namedNativeQueries[$queryName]);
2841
    }
2842
2843
    /**
2844
     * Checks whether the class has a named native query with the given query name.
2845
     *
2846
     * @param string $name
2847
     *
2848
     * @return boolean
2849
     */
2850 1
    public function hasSqlResultSetMapping($name)
2851
    {
2852 1
        return isset($this->sqlResultSetMappings[$name]);
2853
    }
2854
2855
    /**
2856
     * {@inheritDoc}
2857
     */
2858 342
    public function hasAssociation($fieldName)
2859
    {
2860 342
        return isset($this->associationMappings[$fieldName]);
2861
    }
2862
2863
    /**
2864
     * {@inheritDoc}
2865
     */
2866 1
    public function isSingleValuedAssociation($fieldName)
2867
    {
2868 1
        return isset($this->associationMappings[$fieldName])
2869 1
            && ($this->associationMappings[$fieldName]['type'] & self::TO_ONE);
2870
    }
2871
2872
    /**
2873
     * {@inheritDoc}
2874
     */
2875 1026
    public function isCollectionValuedAssociation($fieldName)
2876
    {
2877 1026
        return isset($this->associationMappings[$fieldName])
2878 1026
            && ! ($this->associationMappings[$fieldName]['type'] & self::TO_ONE);
2879
    }
2880
2881
    /**
2882
     * Is this an association that only has a single join column?
2883
     *
2884
     * @param string $fieldName
2885
     *
2886
     * @return bool
2887
     */
2888 35
    public function isAssociationWithSingleJoinColumn($fieldName)
2889
    {
2890 35
        return isset($this->associationMappings[$fieldName])
2891 35
            && isset($this->associationMappings[$fieldName]['joinColumns'][0])
2892 35
            && ! isset($this->associationMappings[$fieldName]['joinColumns'][1]);
2893
    }
2894
2895
    /**
2896
     * Returns the single association join column (if any).
2897
     *
2898
     * @param string $fieldName
2899
     *
2900
     * @return string
2901
     *
2902
     * @throws MappingException
2903
     */
2904 9
    public function getSingleAssociationJoinColumnName($fieldName)
2905
    {
2906 9
        if ( ! $this->isAssociationWithSingleJoinColumn($fieldName)) {
2907
            throw MappingException::noSingleAssociationJoinColumnFound($this->name, $fieldName);
2908
        }
2909
2910 9
        return $this->associationMappings[$fieldName]['joinColumns'][0]['name'];
2911
    }
2912
2913
    /**
2914
     * Returns the single association referenced join column name (if any).
2915
     *
2916
     * @param string $fieldName
2917
     *
2918
     * @return string
2919
     *
2920
     * @throws MappingException
2921
     */
2922 9
    public function getSingleAssociationReferencedJoinColumnName($fieldName)
2923
    {
2924 9
        if ( ! $this->isAssociationWithSingleJoinColumn($fieldName)) {
2925
            throw MappingException::noSingleAssociationJoinColumnFound($this->name, $fieldName);
2926
        }
2927
2928 9
        return $this->associationMappings[$fieldName]['joinColumns'][0]['referencedColumnName'];
2929
    }
2930
2931
    /**
2932
     * Used to retrieve a fieldname for either field or association from a given column.
2933
     *
2934
     * This method is used in foreign-key as primary-key contexts.
2935
     *
2936
     * @param string $columnName
2937
     *
2938
     * @return string
2939
     *
2940
     * @throws MappingException
2941
     */
2942 634
    public function getFieldForColumn($columnName)
2943
    {
2944 634
        if (isset($this->fieldNames[$columnName])) {
2945 634
            return $this->fieldNames[$columnName];
2946
        }
2947
2948 33
        foreach ($this->associationMappings as $assocName => $mapping) {
2949 33
            if ($this->isAssociationWithSingleJoinColumn($assocName) &&
2950 33
                $this->associationMappings[$assocName]['joinColumns'][0]['name'] == $columnName) {
2951
2952 33
                return $assocName;
2953
            }
2954
        }
2955
2956
        throw MappingException::noFieldNameFoundForColumn($this->name, $columnName);
2957
    }
2958
2959
    /**
2960
     * Sets the ID generator used to generate IDs for instances of this class.
2961
     *
2962
     * @param \Doctrine\ORM\Id\AbstractIdGenerator $generator
2963
     *
2964
     * @return void
2965
     */
2966 411
    public function setIdGenerator($generator)
2967
    {
2968 411
        $this->idGenerator = $generator;
2969 411
    }
2970
2971
    /**
2972
     * Sets definition.
2973
     *
2974
     * @param array $definition
2975
     *
2976
     * @return void
2977
     */
2978 12
    public function setCustomGeneratorDefinition(array $definition)
2979
    {
2980 12
        $this->customGeneratorDefinition = $definition;
2981 12
    }
2982
2983
    /**
2984
     * Sets the definition of the sequence ID generator for this class.
2985
     *
2986
     * The definition must have the following structure:
2987
     * <code>
2988
     * array(
2989
     *     'sequenceName'   => 'name',
2990
     *     'allocationSize' => 20,
2991
     *     'initialValue'   => 1
2992
     *     'quoted'         => 1
2993
     * )
2994
     * </code>
2995
     *
2996
     * @param array $definition
2997
     *
2998
     * @return void
2999
     *
3000
     * @throws MappingException
3001
     */
3002 23
    public function setSequenceGeneratorDefinition(array $definition)
3003
    {
3004 23
        if ( ! isset($definition['sequenceName'])) {
3005 1
            throw MappingException::missingSequenceName($this->name);
3006
        }
3007
3008 22
        if ($definition['sequenceName'][0] == '`') {
3009 1
            $definition['sequenceName']   = trim($definition['sequenceName'], '`');
3010 1
            $definition['quoted'] = true;
3011
        }
3012
3013 22
        $this->sequenceGeneratorDefinition = $definition;
3014 22
    }
3015
3016
    /**
3017
     * Sets the version field mapping used for versioning. Sets the default
3018
     * value to use depending on the column type.
3019
     *
3020
     * @param array $mapping The version field mapping array.
3021
     *
3022
     * @return void
3023
     *
3024
     * @throws MappingException
3025
     */
3026 25
    public function setVersionMapping(array &$mapping)
3027
    {
3028 25
        $this->isVersioned = true;
3029 25
        $this->versionField = $mapping['fieldName'];
3030
3031 25
        if ( ! isset($mapping['default'])) {
3032 25
            if (in_array($mapping['type'], array('integer', 'bigint', 'smallint'))) {
3033 24
                $mapping['default'] = 1;
3034 2
            } else if ($mapping['type'] == 'datetime') {
3035 1
                $mapping['default'] = 'CURRENT_TIMESTAMP';
3036
            } else {
3037 1
                throw MappingException::unsupportedOptimisticLockingType($this->name, $mapping['fieldName'], $mapping['type']);
3038
            }
3039
        }
3040 24
    }
3041
3042
    /**
3043
     * Sets whether this class is to be versioned for optimistic locking.
3044
     *
3045
     * @param boolean $bool
3046
     *
3047
     * @return void
3048
     */
3049 123
    public function setVersioned($bool)
3050
    {
3051 123
        $this->isVersioned = $bool;
3052 123
    }
3053
3054
    /**
3055
     * Sets the name of the field that is to be used for versioning if this class is
3056
     * versioned for optimistic locking.
3057
     *
3058
     * @param string $versionField
3059
     *
3060
     * @return void
3061
     */
3062 123
    public function setVersionField($versionField)
3063
    {
3064 123
        $this->versionField = $versionField;
3065 123
    }
3066
3067
    /**
3068
     * Marks this class as read only, no change tracking is applied to it.
3069
     *
3070
     * @return void
3071
     */
3072 3
    public function markReadOnly()
3073
    {
3074 3
        $this->isReadOnly = true;
3075 3
    }
3076
3077
    /**
3078
     * {@inheritDoc}
3079
     */
3080
    public function getFieldNames()
3081
    {
3082
        return array_keys($this->fieldMappings);
3083
    }
3084
3085
    /**
3086
     * {@inheritDoc}
3087
     */
3088
    public function getAssociationNames()
3089
    {
3090
        return array_keys($this->associationMappings);
3091
    }
3092
3093
    /**
3094
     * {@inheritDoc}
3095
     *
3096
     * @throws InvalidArgumentException
3097
     */
3098 1
    public function getAssociationTargetClass($assocName)
3099
    {
3100 1
        if ( ! isset($this->associationMappings[$assocName])) {
3101
            throw new InvalidArgumentException("Association name expected, '" . $assocName ."' is not an association.");
3102
        }
3103
3104 1
        return $this->associationMappings[$assocName]['targetEntity'];
3105
    }
3106
3107
    /**
3108
     * {@inheritDoc}
3109
     */
3110 706
    public function getName()
3111
    {
3112 706
        return $this->name;
3113
    }
3114
3115
    /**
3116
     * Gets the (possibly quoted) identifier column names for safe use in an SQL statement.
3117
     *
3118
     * @deprecated Deprecated since version 2.3 in favor of \Doctrine\ORM\Mapping\QuoteStrategy
3119
     *
3120
     * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
3121
     *
3122
     * @return array
3123
     */
3124
    public function getQuotedIdentifierColumnNames($platform)
3125
    {
3126
        $quotedColumnNames = array();
3127
3128
        foreach ($this->identifier as $idProperty) {
3129
            if (isset($this->fieldMappings[$idProperty])) {
3130
                $quotedColumnNames[] = isset($this->fieldMappings[$idProperty]['quoted'])
3131
                    ? $platform->quoteIdentifier($this->fieldMappings[$idProperty]['columnName'])
3132
                    : $this->fieldMappings[$idProperty]['columnName'];
3133
3134
                continue;
3135
            }
3136
3137
            // Association defined as Id field
3138
            $joinColumns            = $this->associationMappings[$idProperty]['joinColumns'];
3139
            $assocQuotedColumnNames = array_map(
3140
                function ($joinColumn) use ($platform) {
3141
                    return isset($joinColumn['quoted'])
3142
                        ? $platform->quoteIdentifier($joinColumn['name'])
3143
                        : $joinColumn['name'];
3144
                },
3145
                $joinColumns
3146
            );
3147
3148
            $quotedColumnNames = array_merge($quotedColumnNames, $assocQuotedColumnNames);
3149
        }
3150
3151
        return $quotedColumnNames;
3152
    }
3153
3154
    /**
3155
     * Gets the (possibly quoted) column name of a mapped field for safe use  in an SQL statement.
3156
     *
3157
     * @deprecated Deprecated since version 2.3 in favor of \Doctrine\ORM\Mapping\QuoteStrategy
3158
     *
3159
     * @param string                                    $field
3160
     * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
3161
     *
3162
     * @return string
3163
     */
3164
    public function getQuotedColumnName($field, $platform)
3165
    {
3166
        return isset($this->fieldMappings[$field]['quoted'])
3167
            ? $platform->quoteIdentifier($this->fieldMappings[$field]['columnName'])
3168
            : $this->fieldMappings[$field]['columnName'];
3169
    }
3170
3171
    /**
3172
     * Gets the (possibly quoted) primary table name of this class for safe use in an SQL statement.
3173
     *
3174
     * @deprecated Deprecated since version 2.3 in favor of \Doctrine\ORM\Mapping\QuoteStrategy
3175
     *
3176
     * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
3177
     *
3178
     * @return string
3179
     */
3180
    public function getQuotedTableName($platform)
3181
    {
3182
        return isset($this->table['quoted'])
3183
            ? $platform->quoteIdentifier($this->table['name'])
3184
            : $this->table['name'];
3185
    }
3186
3187
    /**
3188
     * Gets the (possibly quoted) name of the join table.
3189
     *
3190
     * @deprecated Deprecated since version 2.3 in favor of \Doctrine\ORM\Mapping\QuoteStrategy
3191
     *
3192
     * @param array                                     $assoc
3193
     * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
3194
     *
3195
     * @return string
3196
     */
3197
    public function getQuotedJoinTableName(array $assoc, $platform)
3198
    {
3199
        return isset($assoc['joinTable']['quoted'])
3200
            ? $platform->quoteIdentifier($assoc['joinTable']['name'])
3201
            : $assoc['joinTable']['name'];
3202
    }
3203
3204
    /**
3205
     * {@inheritDoc}
3206
     */
3207 12
    public function isAssociationInverseSide($fieldName)
3208
    {
3209 12
        return isset($this->associationMappings[$fieldName])
3210 12
            && ! $this->associationMappings[$fieldName]['isOwningSide'];
3211
    }
3212
3213
    /**
3214
     * {@inheritDoc}
3215
     */
3216
    public function getAssociationMappedByTargetField($fieldName)
3217
    {
3218
        return $this->associationMappings[$fieldName]['mappedBy'];
3219
    }
3220
3221
    /**
3222
     * @param string $targetClass
3223
     *
3224
     * @return array
3225
     */
3226 2
    public function getAssociationsByTargetClass($targetClass)
3227
    {
3228 2
        $relations = array();
3229
3230 2
        foreach ($this->associationMappings as $mapping) {
3231 2
            if ($mapping['targetEntity'] == $targetClass) {
3232 2
                $relations[$mapping['fieldName']] = $mapping;
3233
            }
3234
        }
3235
3236 2
        return $relations;
3237
    }
3238
3239
    /**
3240
     * @param  string|null $className
3241
     *
3242
     * @return string|null null if the input value is null
3243
     */
3244 477
    public function fullyQualifiedClassName($className)
3245
    {
3246 477
        if (empty($className)) {
3247 47
            return $className;
3248
        }
3249
3250 461
        if ($className !== null && strpos($className, '\\') === false && strlen($this->namespace) > 0) {
3251 350
            return $this->namespace . '\\' . $className;
3252
        }
3253
3254 229
        return $className;
3255
    }
3256
3257
    /**
3258
     * @param string $name
3259
     *
3260
     * @return mixed
3261
     */
3262 2
    public function getMetadataValue($name)
3263
    {
3264
3265 2
        if (isset($this->$name)) {
3266 2
            return $this->$name;
3267
        }
3268
3269
        return null;
3270
    }
3271
3272
    /**
3273
     * Map Embedded Class
3274
     *
3275
     * @param array $mapping
3276
     *
3277
     * @throws MappingException
3278
     * @return void
3279
     */
3280 27
    public function mapEmbedded(array $mapping)
3281
    {
3282 27
        $this->assertFieldNotMapped($mapping['fieldName']);
3283
3284 27
        $this->embeddedClasses[$mapping['fieldName']] = array(
3285 27
            'class' => $this->fullyQualifiedClassName($mapping['class']),
3286 27
            'columnPrefix' => $mapping['columnPrefix'],
3287 27
            'declaredField' => isset($mapping['declaredField']) ? $mapping['declaredField'] : null,
3288 27
            'originalField' => isset($mapping['originalField']) ? $mapping['originalField'] : null,
3289
        );
3290 27
    }
3291
3292
    /**
3293
     * Inline the embeddable class
3294
     *
3295
     * @param string            $property
3296
     * @param ClassMetadataInfo $embeddable
3297
     */
3298 11
    public function inlineEmbeddable($property, ClassMetadataInfo $embeddable)
3299
    {
3300 11
        foreach ($embeddable->fieldMappings as $fieldMapping) {
3301 11
            $fieldMapping['originalClass'] = isset($fieldMapping['originalClass'])
3302 4
                ? $fieldMapping['originalClass']
3303 11
                : $embeddable->name;
3304 11
            $fieldMapping['declaredField'] = isset($fieldMapping['declaredField'])
3305 4
                ? $property . '.' . $fieldMapping['declaredField']
3306 11
                : $property;
3307 11
            $fieldMapping['originalField'] = isset($fieldMapping['originalField'])
3308 4
                ? $fieldMapping['originalField']
3309 11
                : $fieldMapping['fieldName'];
3310 11
            $fieldMapping['fieldName'] = $property . "." . $fieldMapping['fieldName'];
3311
3312 11
            if (! empty($this->embeddedClasses[$property]['columnPrefix'])) {
3313 2
                $fieldMapping['columnName'] = $this->embeddedClasses[$property]['columnPrefix'] . $fieldMapping['columnName'];
3314 10
            } elseif ($this->embeddedClasses[$property]['columnPrefix'] !== false) {
3315 7
                $fieldMapping['columnName'] = $this->namingStrategy
3316 7
                    ->embeddedFieldToColumnName(
3317
                        $property,
3318 7
                        $fieldMapping['columnName'],
3319 7
                        $this->reflClass->name,
3320 7
                        $embeddable->reflClass->name
3321
                    );
3322
            }
3323
3324 11
            $this->mapField($fieldMapping);
3325
        }
3326 11
    }
3327
3328
    /**
3329
     * @param string $fieldName
3330
     * @throws MappingException
3331
     */
3332 563
    private function assertFieldNotMapped($fieldName)
3333
    {
3334 563
        if (isset($this->fieldMappings[$fieldName]) ||
3335 563
            isset($this->associationMappings[$fieldName]) ||
3336 563
            isset($this->embeddedClasses[$fieldName])) {
3337
3338 2
            throw MappingException::duplicateFieldMapping($this->name, $fieldName);
3339
        }
3340 563
    }
3341
3342
    /**
3343
     * Gets the sequence name based on class metadata.
3344
     *
3345
     * @param AbstractPlatform $platform
3346
     * @return string
3347
     *
3348
     * @todo Sequence names should be computed in DBAL depending on the platform
3349
     */
3350 3
    public function getSequenceName(AbstractPlatform $platform)
3351
    {
3352 3
        $sequencePrefix = $this->getSequencePrefix($platform);
3353 3
        $columnName     = $this->getSingleIdentifierColumnName();
3354 3
        $sequenceName   = $sequencePrefix . '_' . $columnName . '_seq';
3355
3356 3
        return $sequenceName;
3357
    }
3358
3359
    /**
3360
     * Gets the sequence name prefix based on class metadata.
3361
     *
3362
     * @param AbstractPlatform $platform
3363
     * @return string
3364
     *
3365
     * @todo Sequence names should be computed in DBAL depending on the platform
3366
     */
3367 3
    public function getSequencePrefix(AbstractPlatform $platform)
3368
    {
3369 3
        $tableName      = $this->getTableName();
3370 3
        $sequencePrefix = $tableName;
3371
3372
        // Prepend the schema name to the table name if there is one
3373 3
        if ($schemaName = $this->getSchemaName()) {
3374 3
            $sequencePrefix = $schemaName . '.' . $tableName;
3375
3376 3
            if ( ! $platform->supportsSchemas() && $platform->canEmulateSchemas()) {
3377 3
                $sequencePrefix = $schemaName . '__' . $tableName;
3378
            }
3379
        }
3380
3381 3
        return $sequencePrefix;
3382
    }
3383
3384
    /**
3385
     * @param array $mapping
3386
     */
3387 208
    private function assertMappingOrderBy(array $mapping)
3388
    {
3389 208
        if (isset($mapping['orderBy']) && !is_array($mapping['orderBy'])) {
3390
            throw new InvalidArgumentException("'orderBy' is expected to be an array, not " . gettype($mapping['orderBy']));
3391
        }
3392 208
    }
3393
}
3394