PageRenderTime 57ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php

http://github.com/doctrine/doctrine2
PHP | 3745 lines | 2131 code | 314 blank | 1300 comment | 137 complexity | f316fbe6d97995a1e1bf8318d79db696 MD5 | raw file
Possible License(s): Unlicense

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\ORM\Mapping;
  4. use BadMethodCallException;
  5. use DateInterval;
  6. use DateTime;
  7. use DateTimeImmutable;
  8. use Doctrine\DBAL\Platforms\AbstractPlatform;
  9. use Doctrine\DBAL\Types\Type;
  10. use Doctrine\DBAL\Types\Types;
  11. use Doctrine\Deprecations\Deprecation;
  12. use Doctrine\Instantiator\Instantiator;
  13. use Doctrine\Instantiator\InstantiatorInterface;
  14. use Doctrine\ORM\Cache\Exception\CacheException;
  15. use Doctrine\ORM\Cache\Exception\NonCacheableEntityAssociation;
  16. use Doctrine\ORM\Id\AbstractIdGenerator;
  17. use Doctrine\Persistence\Mapping\ClassMetadata;
  18. use Doctrine\Persistence\Mapping\ReflectionService;
  19. use InvalidArgumentException;
  20. use ReflectionClass;
  21. use ReflectionNamedType;
  22. use ReflectionProperty;
  23. use RuntimeException;
  24. use function array_diff;
  25. use function array_flip;
  26. use function array_intersect;
  27. use function array_keys;
  28. use function array_map;
  29. use function array_merge;
  30. use function array_pop;
  31. use function array_values;
  32. use function assert;
  33. use function class_exists;
  34. use function count;
  35. use function explode;
  36. use function gettype;
  37. use function in_array;
  38. use function interface_exists;
  39. use function is_array;
  40. use function is_subclass_of;
  41. use function ltrim;
  42. use function method_exists;
  43. use function spl_object_id;
  44. use function str_replace;
  45. use function strpos;
  46. use function strtolower;
  47. use function trait_exists;
  48. use function trim;
  49. use const PHP_VERSION_ID;
  50. /**
  51. * A <tt>ClassMetadata</tt> instance holds all the object-relational mapping metadata
  52. * of an entity and its associations.
  53. *
  54. * Once populated, ClassMetadata instances are usually cached in a serialized form.
  55. *
  56. * <b>IMPORTANT NOTE:</b>
  57. *
  58. * The fields of this class are only public for 2 reasons:
  59. * 1) To allow fast READ access.
  60. * 2) To drastically reduce the size of a serialized instance (private/protected members
  61. * get the whole class name, namespace inclusive, prepended to every property in
  62. * the serialized representation).
  63. *
  64. * @template-covariant T of object
  65. * @template-implements ClassMetadata<T>
  66. */
  67. class ClassMetadataInfo implements ClassMetadata
  68. {
  69. /* The inheritance mapping types */
  70. /**
  71. * NONE means the class does not participate in an inheritance hierarchy
  72. * and therefore does not need an inheritance mapping type.
  73. */
  74. public const INHERITANCE_TYPE_NONE = 1;
  75. /**
  76. * JOINED means the class will be persisted according to the rules of
  77. * <tt>Class Table Inheritance</tt>.
  78. */
  79. public const INHERITANCE_TYPE_JOINED = 2;
  80. /**
  81. * SINGLE_TABLE means the class will be persisted according to the rules of
  82. * <tt>Single Table Inheritance</tt>.
  83. */
  84. public const INHERITANCE_TYPE_SINGLE_TABLE = 3;
  85. /**
  86. * TABLE_PER_CLASS means the class will be persisted according to the rules
  87. * of <tt>Concrete Table Inheritance</tt>.
  88. */
  89. public const INHERITANCE_TYPE_TABLE_PER_CLASS = 4;
  90. /* The Id generator types. */
  91. /**
  92. * AUTO means the generator type will depend on what the used platform prefers.
  93. * Offers full portability.
  94. */
  95. public const GENERATOR_TYPE_AUTO = 1;
  96. /**
  97. * SEQUENCE means a separate sequence object will be used. Platforms that do
  98. * not have native sequence support may emulate it. Full portability is currently
  99. * not guaranteed.
  100. */
  101. public const GENERATOR_TYPE_SEQUENCE = 2;
  102. /**
  103. * TABLE means a separate table is used for id generation.
  104. * Offers full portability (in that it results in an exception being thrown
  105. * no matter the platform).
  106. *
  107. * @deprecated no replacement planned
  108. */
  109. public const GENERATOR_TYPE_TABLE = 3;
  110. /**
  111. * IDENTITY means an identity column is used for id generation. The database
  112. * will fill in the id column on insertion. Platforms that do not support
  113. * native identity columns may emulate them. Full portability is currently
  114. * not guaranteed.
  115. */
  116. public const GENERATOR_TYPE_IDENTITY = 4;
  117. /**
  118. * NONE means the class does not have a generated id. That means the class
  119. * must have a natural, manually assigned id.
  120. */
  121. public const GENERATOR_TYPE_NONE = 5;
  122. /**
  123. * UUID means that a UUID/GUID expression is used for id generation. Full
  124. * portability is currently not guaranteed.
  125. *
  126. * @deprecated use an application-side generator instead
  127. */
  128. public const GENERATOR_TYPE_UUID = 6;
  129. /**
  130. * CUSTOM means that customer will use own ID generator that supposedly work
  131. */
  132. public const GENERATOR_TYPE_CUSTOM = 7;
  133. /**
  134. * DEFERRED_IMPLICIT means that changes of entities are calculated at commit-time
  135. * by doing a property-by-property comparison with the original data. This will
  136. * be done for all entities that are in MANAGED state at commit-time.
  137. *
  138. * This is the default change tracking policy.
  139. */
  140. public const CHANGETRACKING_DEFERRED_IMPLICIT = 1;
  141. /**
  142. * DEFERRED_EXPLICIT means that changes of entities are calculated at commit-time
  143. * by doing a property-by-property comparison with the original data. This will
  144. * be done only for entities that were explicitly saved (through persist() or a cascade).
  145. */
  146. public const CHANGETRACKING_DEFERRED_EXPLICIT = 2;
  147. /**
  148. * NOTIFY means that Doctrine relies on the entities sending out notifications
  149. * when their properties change. Such entity classes must implement
  150. * the <tt>NotifyPropertyChanged</tt> interface.
  151. */
  152. public const CHANGETRACKING_NOTIFY = 3;
  153. /**
  154. * Specifies that an association is to be fetched when it is first accessed.
  155. */
  156. public const FETCH_LAZY = 2;
  157. /**
  158. * Specifies that an association is to be fetched when the owner of the
  159. * association is fetched.
  160. */
  161. public const FETCH_EAGER = 3;
  162. /**
  163. * Specifies that an association is to be fetched lazy (on first access) and that
  164. * commands such as Collection#count, Collection#slice are issued directly against
  165. * the database if the collection is not yet initialized.
  166. */
  167. public const FETCH_EXTRA_LAZY = 4;
  168. /**
  169. * Identifies a one-to-one association.
  170. */
  171. public const ONE_TO_ONE = 1;
  172. /**
  173. * Identifies a many-to-one association.
  174. */
  175. public const MANY_TO_ONE = 2;
  176. /**
  177. * Identifies a one-to-many association.
  178. */
  179. public const ONE_TO_MANY = 4;
  180. /**
  181. * Identifies a many-to-many association.
  182. */
  183. public const MANY_TO_MANY = 8;
  184. /**
  185. * Combined bitmask for to-one (single-valued) associations.
  186. */
  187. public const TO_ONE = 3;
  188. /**
  189. * Combined bitmask for to-many (collection-valued) associations.
  190. */
  191. public const TO_MANY = 12;
  192. /**
  193. * ReadOnly cache can do reads, inserts and deletes, cannot perform updates or employ any locks,
  194. */
  195. public const CACHE_USAGE_READ_ONLY = 1;
  196. /**
  197. * Nonstrict Read Write Cache doesn’t employ any locks but can do inserts, update and deletes.
  198. */
  199. public const CACHE_USAGE_NONSTRICT_READ_WRITE = 2;
  200. /**
  201. * Read Write Attempts to lock the entity before update/delete.
  202. */
  203. public const CACHE_USAGE_READ_WRITE = 3;
  204. /**
  205. * READ-ONLY: The name of the entity class.
  206. *
  207. * @var string
  208. * @psalm-var class-string<T>
  209. */
  210. public $name;
  211. /**
  212. * READ-ONLY: The namespace the entity class is contained in.
  213. *
  214. * @var string
  215. * @todo Not really needed. Usage could be localized.
  216. */
  217. public $namespace;
  218. /**
  219. * READ-ONLY: The name of the entity class that is at the root of the mapped entity inheritance
  220. * hierarchy. If the entity is not part of a mapped inheritance hierarchy this is the same
  221. * as {@link $name}.
  222. *
  223. * @var string
  224. * @psalm-var class-string
  225. */
  226. public $rootEntityName;
  227. /**
  228. * READ-ONLY: The definition of custom generator. Only used for CUSTOM
  229. * generator type
  230. *
  231. * The definition has the following structure:
  232. * <code>
  233. * array(
  234. * 'class' => 'ClassName',
  235. * )
  236. * </code>
  237. *
  238. * @todo Merge with tableGeneratorDefinition into generic generatorDefinition
  239. * @var array<string, string>|null
  240. */
  241. public $customGeneratorDefinition;
  242. /**
  243. * The name of the custom repository class used for the entity class.
  244. * (Optional).
  245. *
  246. * @var string|null
  247. * @psalm-var ?class-string
  248. */
  249. public $customRepositoryClassName;
  250. /**
  251. * READ-ONLY: Whether this class describes the mapping of a mapped superclass.
  252. *
  253. * @var bool
  254. */
  255. public $isMappedSuperclass = false;
  256. /**
  257. * READ-ONLY: Whether this class describes the mapping of an embeddable class.
  258. *
  259. * @var bool
  260. */
  261. public $isEmbeddedClass = false;
  262. /**
  263. * READ-ONLY: The names of the parent classes (ancestors).
  264. *
  265. * @psalm-var list<class-string>
  266. */
  267. public $parentClasses = [];
  268. /**
  269. * READ-ONLY: The names of all subclasses (descendants).
  270. *
  271. * @psalm-var list<class-string>
  272. */
  273. public $subClasses = [];
  274. /**
  275. * READ-ONLY: The names of all embedded classes based on properties.
  276. *
  277. * @psalm-var array<string, mixed[]>
  278. */
  279. public $embeddedClasses = [];
  280. /**
  281. * READ-ONLY: The named queries allowed to be called directly from Repository.
  282. *
  283. * @psalm-var array<string, array<string, mixed>>
  284. */
  285. public $namedQueries = [];
  286. /**
  287. * READ-ONLY: The named native queries allowed to be called directly from Repository.
  288. *
  289. * A native SQL named query definition has the following structure:
  290. * <pre>
  291. * array(
  292. * 'name' => <query name>,
  293. * 'query' => <sql query>,
  294. * 'resultClass' => <class of the result>,
  295. * 'resultSetMapping' => <name of a SqlResultSetMapping>
  296. * )
  297. * </pre>
  298. *
  299. * @psalm-var array<string, array<string, mixed>>
  300. */
  301. public $namedNativeQueries = [];
  302. /**
  303. * READ-ONLY: The mappings of the results of native SQL queries.
  304. *
  305. * A native result mapping definition has the following structure:
  306. * <pre>
  307. * array(
  308. * 'name' => <result name>,
  309. * 'entities' => array(<entity result mapping>),
  310. * 'columns' => array(<column result mapping>)
  311. * )
  312. * </pre>
  313. *
  314. * @psalm-var array<string, array{
  315. * name: string,
  316. * entities: mixed[],
  317. * columns: mixed[]
  318. * }>
  319. */
  320. public $sqlResultSetMappings = [];
  321. /**
  322. * READ-ONLY: The field names of all fields that are part of the identifier/primary key
  323. * of the mapped entity class.
  324. *
  325. * @psalm-var list<string>
  326. */
  327. public $identifier = [];
  328. /**
  329. * READ-ONLY: The inheritance mapping type used by the class.
  330. *
  331. * @var int
  332. * @psalm-var self::$INHERITANCE_TYPE_*
  333. */
  334. public $inheritanceType = self::INHERITANCE_TYPE_NONE;
  335. /**
  336. * READ-ONLY: The Id generator type used by the class.
  337. *
  338. * @var int
  339. */
  340. public $generatorType = self::GENERATOR_TYPE_NONE;
  341. /**
  342. * READ-ONLY: The field mappings of the class.
  343. * Keys are field names and values are mapping definitions.
  344. *
  345. * The mapping definition array has the following values:
  346. *
  347. * - <b>fieldName</b> (string)
  348. * The name of the field in the Entity.
  349. *
  350. * - <b>type</b> (string)
  351. * The type name of the mapped field. Can be one of Doctrine's mapping types
  352. * or a custom mapping type.
  353. *
  354. * - <b>columnName</b> (string, optional)
  355. * The column name. Optional. Defaults to the field name.
  356. *
  357. * - <b>length</b> (integer, optional)
  358. * The database length of the column. Optional. Default value taken from
  359. * the type.
  360. *
  361. * - <b>id</b> (boolean, optional)
  362. * Marks the field as the primary key of the entity. Multiple fields of an
  363. * entity can have the id attribute, forming a composite key.
  364. *
  365. * - <b>nullable</b> (boolean, optional)
  366. * Whether the column is nullable. Defaults to FALSE.
  367. *
  368. * - <b>columnDefinition</b> (string, optional, schema-only)
  369. * The SQL fragment that is used when generating the DDL for the column.
  370. *
  371. * - <b>precision</b> (integer, optional, schema-only)
  372. * The precision of a decimal column. Only valid if the column type is decimal.
  373. *
  374. * - <b>scale</b> (integer, optional, schema-only)
  375. * The scale of a decimal column. Only valid if the column type is decimal.
  376. *
  377. * - <b>'unique'</b> (string, optional, schema-only)
  378. * Whether a unique constraint should be generated for the column.
  379. *
  380. * @var mixed[]
  381. * @psalm-var array<string, array{
  382. * type: string,
  383. * fieldName: string,
  384. * columnName?: string,
  385. * length?: int,
  386. * id?: bool,
  387. * nullable?: bool,
  388. * columnDefinition?: string,
  389. * precision?: int,
  390. * scale?: int,
  391. * unique?: string,
  392. * inherited?: class-string,
  393. * originalClass?: class-string,
  394. * originalField?: string,
  395. * quoted?: bool,
  396. * requireSQLConversion?: bool,
  397. * declared?: class-string,
  398. * declaredField?: string,
  399. * options: array<mixed>
  400. * }>
  401. */
  402. public $fieldMappings = [];
  403. /**
  404. * READ-ONLY: An array of field names. Used to look up field names from column names.
  405. * Keys are column names and values are field names.
  406. *
  407. * @psalm-var array<string, string>
  408. */
  409. public $fieldNames = [];
  410. /**
  411. * READ-ONLY: A map of field names to column names. Keys are field names and values column names.
  412. * Used to look up column names from field names.
  413. * This is the reverse lookup map of $_fieldNames.
  414. *
  415. * @deprecated 3.0 Remove this.
  416. *
  417. * @var mixed[]
  418. */
  419. public $columnNames = [];
  420. /**
  421. * READ-ONLY: The discriminator value of this class.
  422. *
  423. * <b>This does only apply to the JOINED and SINGLE_TABLE inheritance mapping strategies
  424. * where a discriminator column is used.</b>
  425. *
  426. * @see discriminatorColumn
  427. *
  428. * @var mixed
  429. */
  430. public $discriminatorValue;
  431. /**
  432. * READ-ONLY: The discriminator map of all mapped classes in the hierarchy.
  433. *
  434. * <b>This does only apply to the JOINED and SINGLE_TABLE inheritance mapping strategies
  435. * where a discriminator column is used.</b>
  436. *
  437. * @see discriminatorColumn
  438. *
  439. * @var mixed
  440. */
  441. public $discriminatorMap = [];
  442. /**
  443. * READ-ONLY: The definition of the discriminator column used in JOINED and SINGLE_TABLE
  444. * inheritance mappings.
  445. *
  446. * @psalm-var array<string, mixed>
  447. */
  448. public $discriminatorColumn;
  449. /**
  450. * READ-ONLY: The primary table definition. The definition is an array with the
  451. * following entries:
  452. *
  453. * name => <tableName>
  454. * schema => <schemaName>
  455. * indexes => array
  456. * uniqueConstraints => array
  457. *
  458. * @var mixed[]
  459. * @psalm-var array{
  460. * name: string,
  461. * schema: string,
  462. * indexes: array,
  463. * uniqueConstraints: array,
  464. * options: array<string, mixed>,
  465. * quoted?: bool
  466. * }
  467. */
  468. public $table;
  469. /**
  470. * READ-ONLY: The registered lifecycle callbacks for entities of this class.
  471. *
  472. * @psalm-var array<string, list<string>>
  473. */
  474. public $lifecycleCallbacks = [];
  475. /**
  476. * READ-ONLY: The registered entity listeners.
  477. *
  478. * @psalm-var array<string, list<array{class: class-string, method: string}>>
  479. */
  480. public $entityListeners = [];
  481. /**
  482. * READ-ONLY: The association mappings of this class.
  483. *
  484. * The mapping definition array supports the following keys:
  485. *
  486. * - <b>fieldName</b> (string)
  487. * The name of the field in the entity the association is mapped to.
  488. *
  489. * - <b>targetEntity</b> (string)
  490. * The class name of the target entity. If it is fully-qualified it is used as is.
  491. * If it is a simple, unqualified class name the namespace is assumed to be the same
  492. * as the namespace of the source entity.
  493. *
  494. * - <b>mappedBy</b> (string, required for bidirectional associations)
  495. * The name of the field that completes the bidirectional association on the owning side.
  496. * This key must be specified on the inverse side of a bidirectional association.
  497. *
  498. * - <b>inversedBy</b> (string, required for bidirectional associations)
  499. * The name of the field that completes the bidirectional association on the inverse side.
  500. * This key must be specified on the owning side of a bidirectional association.
  501. *
  502. * - <b>cascade</b> (array, optional)
  503. * The names of persistence operations to cascade on the association. The set of possible
  504. * values are: "persist", "remove", "detach", "merge", "refresh", "all" (implies all others).
  505. *
  506. * - <b>orderBy</b> (array, one-to-many/many-to-many only)
  507. * A map of field names (of the target entity) to sorting directions (ASC/DESC).
  508. * Example: array('priority' => 'desc')
  509. *
  510. * - <b>fetch</b> (integer, optional)
  511. * The fetching strategy to use for the association, usually defaults to FETCH_LAZY.
  512. * Possible values are: ClassMetadata::FETCH_EAGER, ClassMetadata::FETCH_LAZY.
  513. *
  514. * - <b>joinTable</b> (array, optional, many-to-many only)
  515. * Specification of the join table and its join columns (foreign keys).
  516. * Only valid for many-to-many mappings. Note that one-to-many associations can be mapped
  517. * through a join table by simply mapping the association as many-to-many with a unique
  518. * constraint on the join table.
  519. *
  520. * - <b>indexBy</b> (string, optional, to-many only)
  521. * Specification of a field on target-entity that is used to index the collection by.
  522. * This field HAS to be either the primary key or a unique column. Otherwise the collection
  523. * does not contain all the entities that are actually related.
  524. *
  525. * A join table definition has the following structure:
  526. * <pre>
  527. * array(
  528. * 'name' => <join table name>,
  529. * 'joinColumns' => array(<join column mapping from join table to source table>),
  530. * 'inverseJoinColumns' => array(<join column mapping from join table to target table>)
  531. * )
  532. * </pre>
  533. *
  534. * @psalm-var array<string, array<string, mixed>>
  535. */
  536. public $associationMappings = [];
  537. /**
  538. * READ-ONLY: Flag indicating whether the identifier/primary key of the class is composite.
  539. *
  540. * @var bool
  541. */
  542. public $isIdentifierComposite = false;
  543. /**
  544. * READ-ONLY: Flag indicating whether the identifier/primary key contains at least one foreign key association.
  545. *
  546. * This flag is necessary because some code blocks require special treatment of this cases.
  547. *
  548. * @var bool
  549. */
  550. public $containsForeignIdentifier = false;
  551. /**
  552. * READ-ONLY: The ID generator used for generating IDs for this class.
  553. *
  554. * @var AbstractIdGenerator
  555. * @todo Remove!
  556. */
  557. public $idGenerator;
  558. /**
  559. * READ-ONLY: The definition of the sequence generator of this class. Only used for the
  560. * SEQUENCE generation strategy.
  561. *
  562. * The definition has the following structure:
  563. * <code>
  564. * array(
  565. * 'sequenceName' => 'name',
  566. * 'allocationSize' => '20',
  567. * 'initialValue' => '1'
  568. * )
  569. * </code>
  570. *
  571. * @var array<string, mixed>
  572. * @psalm-var array{sequenceName: string, allocationSize: string, initialValue: string, quoted?: mixed}
  573. * @todo Merge with tableGeneratorDefinition into generic generatorDefinition
  574. */
  575. public $sequenceGeneratorDefinition;
  576. /**
  577. * READ-ONLY: The definition of the table generator of this class. Only used for the
  578. * TABLE generation strategy.
  579. *
  580. * @deprecated
  581. *
  582. * @var array<string, mixed>
  583. */
  584. public $tableGeneratorDefinition;
  585. /**
  586. * READ-ONLY: The policy used for change-tracking on entities of this class.
  587. *
  588. * @var int
  589. */
  590. public $changeTrackingPolicy = self::CHANGETRACKING_DEFERRED_IMPLICIT;
  591. /**
  592. * READ-ONLY: A flag for whether or not instances of this class are to be versioned
  593. * with optimistic locking.
  594. *
  595. * @var bool
  596. */
  597. public $isVersioned;
  598. /**
  599. * READ-ONLY: The name of the field which is used for versioning in optimistic locking (if any).
  600. *
  601. * @var mixed
  602. */
  603. public $versionField;
  604. /** @var mixed[] */
  605. public $cache = null;
  606. /**
  607. * The ReflectionClass instance of the mapped class.
  608. *
  609. * @var ReflectionClass|null
  610. */
  611. public $reflClass;
  612. /**
  613. * Is this entity marked as "read-only"?
  614. *
  615. * That means it is never considered for change-tracking in the UnitOfWork. It is a very helpful performance
  616. * optimization for entities that are immutable, either in your domain or through the relation database
  617. * (coming from a view, or a history table for example).
  618. *
  619. * @var bool
  620. */
  621. public $isReadOnly = false;
  622. /**
  623. * NamingStrategy determining the default column and table names.
  624. *
  625. * @var NamingStrategy
  626. */
  627. protected $namingStrategy;
  628. /**
  629. * The ReflectionProperty instances of the mapped class.
  630. *
  631. * @var ReflectionProperty[]|null[]
  632. */
  633. public $reflFields = [];
  634. /** @var InstantiatorInterface|null */
  635. private $instantiator;
  636. /**
  637. * Initializes a new ClassMetadata instance that will hold the object-relational mapping
  638. * metadata of the class with the given name.
  639. *
  640. * @param string $entityName The name of the entity class the new instance is used for.
  641. * @psalm-param class-string<T> $entityName
  642. */
  643. public function __construct($entityName, ?NamingStrategy $namingStrategy = null)
  644. {
  645. $this->name = $entityName;
  646. $this->rootEntityName = $entityName;
  647. $this->namingStrategy = $namingStrategy ?: new DefaultNamingStrategy();
  648. $this->instantiator = new Instantiator();
  649. }
  650. /**
  651. * Gets the ReflectionProperties of the mapped class.
  652. *
  653. * @return ReflectionProperty[]|null[] An array of ReflectionProperty instances.
  654. * @psalm-return array<ReflectionProperty|null>
  655. */
  656. public function getReflectionProperties()
  657. {
  658. return $this->reflFields;
  659. }
  660. /**
  661. * Gets a ReflectionProperty for a specific field of the mapped class.
  662. *
  663. * @param string $name
  664. *
  665. * @return ReflectionProperty
  666. */
  667. public function getReflectionProperty($name)
  668. {
  669. return $this->reflFields[$name];
  670. }
  671. /**
  672. * Gets the ReflectionProperty for the single identifier field.
  673. *
  674. * @return ReflectionProperty
  675. *
  676. * @throws BadMethodCallException If the class has a composite identifier.
  677. */
  678. public function getSingleIdReflectionProperty()
  679. {
  680. if ($this->isIdentifierComposite) {
  681. throw new BadMethodCallException('Class ' . $this->name . ' has a composite identifier.');
  682. }
  683. return $this->reflFields[$this->identifier[0]];
  684. }
  685. /**
  686. * Extracts the identifier values of an entity of this class.
  687. *
  688. * For composite identifiers, the identifier values are returned as an array
  689. * with the same order as the field order in {@link identifier}.
  690. *
  691. * @param object $entity
  692. *
  693. * @return array<string, mixed>
  694. */
  695. public function getIdentifierValues($entity)
  696. {
  697. if ($this->isIdentifierComposite) {
  698. $id = [];
  699. foreach ($this->identifier as $idField) {
  700. $value = $this->reflFields[$idField]->getValue($entity);
  701. if ($value !== null) {
  702. $id[$idField] = $value;
  703. }
  704. }
  705. return $id;
  706. }
  707. $id = $this->identifier[0];
  708. $value = $this->reflFields[$id]->getValue($entity);
  709. if ($value === null) {
  710. return [];
  711. }
  712. return [$id => $value];
  713. }
  714. /**
  715. * Populates the entity identifier of an entity.
  716. *
  717. * @param object $entity
  718. * @psalm-param array<string, mixed> $id
  719. *
  720. * @return void
  721. *
  722. * @todo Rename to assignIdentifier()
  723. */
  724. public function setIdentifierValues($entity, array $id)
  725. {
  726. foreach ($id as $idField => $idValue) {
  727. $this->reflFields[$idField]->setValue($entity, $idValue);
  728. }
  729. }
  730. /**
  731. * Sets the specified field to the specified value on the given entity.
  732. *
  733. * @param object $entity
  734. * @param string $field
  735. * @param mixed $value
  736. *
  737. * @return void
  738. */
  739. public function setFieldValue($entity, $field, $value)
  740. {
  741. $this->reflFields[$field]->setValue($entity, $value);
  742. }
  743. /**
  744. * Gets the specified field's value off the given entity.
  745. *
  746. * @param object $entity
  747. * @param string $field
  748. *
  749. * @return mixed
  750. */
  751. public function getFieldValue($entity, $field)
  752. {
  753. return $this->reflFields[$field]->getValue($entity);
  754. }
  755. /**
  756. * Creates a string representation of this instance.
  757. *
  758. * @return string The string representation of this instance.
  759. *
  760. * @todo Construct meaningful string representation.
  761. */
  762. public function __toString()
  763. {
  764. return self::class . '@' . spl_object_id($this);
  765. }
  766. /**
  767. * Determines which fields get serialized.
  768. *
  769. * It is only serialized what is necessary for best unserialization performance.
  770. * That means any metadata properties that are not set or empty or simply have
  771. * their default value are NOT serialized.
  772. *
  773. * Parts that are also NOT serialized because they can not be properly unserialized:
  774. * - reflClass (ReflectionClass)
  775. * - reflFields (ReflectionProperty array)
  776. *
  777. * @return string[] The names of all the fields that should be serialized.
  778. */
  779. public function __sleep()
  780. {
  781. // This metadata is always serialized/cached.
  782. $serialized = [
  783. 'associationMappings',
  784. 'columnNames', //TODO: 3.0 Remove this. Can use fieldMappings[$fieldName]['columnName']
  785. 'fieldMappings',
  786. 'fieldNames',
  787. 'embeddedClasses',
  788. 'identifier',
  789. 'isIdentifierComposite', // TODO: REMOVE
  790. 'name',
  791. 'namespace', // TODO: REMOVE
  792. 'table',
  793. 'rootEntityName',
  794. 'idGenerator', //TODO: Does not really need to be serialized. Could be moved to runtime.
  795. ];
  796. // The rest of the metadata is only serialized if necessary.
  797. if ($this->changeTrackingPolicy !== self::CHANGETRACKING_DEFERRED_IMPLICIT) {
  798. $serialized[] = 'changeTrackingPolicy';
  799. }
  800. if ($this->customRepositoryClassName) {
  801. $serialized[] = 'customRepositoryClassName';
  802. }
  803. if ($this->inheritanceType !== self::INHERITANCE_TYPE_NONE) {
  804. $serialized[] = 'inheritanceType';
  805. $serialized[] = 'discriminatorColumn';
  806. $serialized[] = 'discriminatorValue';
  807. $serialized[] = 'discriminatorMap';
  808. $serialized[] = 'parentClasses';
  809. $serialized[] = 'subClasses';
  810. }
  811. if ($this->generatorType !== self::GENERATOR_TYPE_NONE) {
  812. $serialized[] = 'generatorType';
  813. if ($this->generatorType === self::GENERATOR_TYPE_SEQUENCE) {
  814. $serialized[] = 'sequenceGeneratorDefinition';
  815. }
  816. }
  817. if ($this->isMappedSuperclass) {
  818. $serialized[] = 'isMappedSuperclass';
  819. }
  820. if ($this->isEmbeddedClass) {
  821. $serialized[] = 'isEmbeddedClass';
  822. }
  823. if ($this->containsForeignIdentifier) {
  824. $serialized[] = 'containsForeignIdentifier';
  825. }
  826. if ($this->isVersioned) {
  827. $serialized[] = 'isVersioned';
  828. $serialized[] = 'versionField';
  829. }
  830. if ($this->lifecycleCallbacks) {
  831. $serialized[] = 'lifecycleCallbacks';
  832. }
  833. if ($this->entityListeners) {
  834. $serialized[] = 'entityListeners';
  835. }
  836. if ($this->namedQueries) {
  837. $serialized[] = 'namedQueries';
  838. }
  839. if ($this->namedNativeQueries) {
  840. $serialized[] = 'namedNativeQueries';
  841. }
  842. if ($this->sqlResultSetMappings) {
  843. $serialized[] = 'sqlResultSetMappings';
  844. }
  845. if ($this->isReadOnly) {
  846. $serialized[] = 'isReadOnly';
  847. }
  848. if ($this->customGeneratorDefinition) {
  849. $serialized[] = 'customGeneratorDefinition';
  850. }
  851. if ($this->cache) {
  852. $serialized[] = 'cache';
  853. }
  854. return $serialized;
  855. }
  856. /**
  857. * Creates a new instance of the mapped class, without invoking the constructor.
  858. *
  859. * @return object
  860. */
  861. public function newInstance()
  862. {
  863. return $this->instantiator->instantiate($this->name);
  864. }
  865. /**
  866. * Restores some state that can not be serialized/unserialized.
  867. *
  868. * @param ReflectionService $reflService
  869. *
  870. * @return void
  871. */
  872. public function wakeupReflection($reflService)
  873. {
  874. // Restore ReflectionClass and properties
  875. $this->reflClass = $reflService->getClass($this->name);
  876. $this->instantiator = $this->instantiator ?: new Instantiator();
  877. $parentReflFields = [];
  878. foreach ($this->embeddedClasses as $property => $embeddedClass) {
  879. if (isset($embeddedClass['declaredField'])) {
  880. $childProperty = $reflService->getAccessibleProperty(
  881. $this->embeddedClasses[$embeddedClass['declaredField']]['class'],
  882. $embeddedClass['originalField']
  883. );
  884. assert($childProperty !== null);
  885. $parentReflFields[$property] = new ReflectionEmbeddedProperty(
  886. $parentReflFields[$embeddedClass['declaredField']],
  887. $childProperty,
  888. $this->embeddedClasses[$embeddedClass['declaredField']]['class']
  889. );
  890. continue;
  891. }
  892. $fieldRefl = $reflService->getAccessibleProperty(
  893. $embeddedClass['declared'] ?? $this->name,
  894. $property
  895. );
  896. $parentReflFields[$property] = $fieldRefl;
  897. $this->reflFields[$property] = $fieldRefl;
  898. }
  899. foreach ($this->fieldMappings as $field => $mapping) {
  900. if (isset($mapping['declaredField']) && isset($parentReflFields[$mapping['declaredField']])) {
  901. $this->reflFields[$field] = new ReflectionEmbeddedProperty(
  902. $parentReflFields[$mapping['declaredField']],
  903. $reflService->getAccessibleProperty($mapping['originalClass'], $mapping['originalField']),
  904. $mapping['originalClass']
  905. );
  906. continue;
  907. }
  908. $this->reflFields[$field] = isset($mapping['declared'])
  909. ? $reflService->getAccessibleProperty($mapping['declared'], $field)
  910. : $reflService->getAccessibleProperty($this->name, $field);
  911. }
  912. foreach ($this->associationMappings as $field => $mapping) {
  913. $this->reflFields[$field] = isset($mapping['declared'])
  914. ? $reflService->getAccessibleProperty($mapping['declared'], $field)
  915. : $reflService->getAccessibleProperty($this->name, $field);
  916. }
  917. }
  918. /**
  919. * Initializes a new ClassMetadata instance that will hold the object-relational mapping
  920. * metadata of the class with the given name.
  921. *
  922. * @param ReflectionService $reflService The reflection service.
  923. *
  924. * @return void
  925. */
  926. public function initializeReflection($reflService)
  927. {
  928. $this->reflClass = $reflService->getClass($this->name);
  929. $this->namespace = $reflService->getClassNamespace($this->name);
  930. if ($this->reflClass) {
  931. $this->name = $this->rootEntityName = $this->reflClass->getName();
  932. }
  933. $this->table['name'] = $this->namingStrategy->classToTableName($this->name);
  934. }
  935. /**
  936. * Validates Identifier.
  937. *
  938. * @return void
  939. *
  940. * @throws MappingException
  941. */
  942. public function validateIdentifier()
  943. {
  944. if ($this->isMappedSuperclass || $this->isEmbeddedClass) {
  945. return;
  946. }
  947. // Verify & complete identifier mapping
  948. if (! $this->identifier) {
  949. throw MappingException::identifierRequired($this->name);
  950. }
  951. if ($this->usesIdGenerator() && $this->isIdentifierComposite) {
  952. throw MappingException::compositeKeyAssignedIdGeneratorRequired($this->name);
  953. }
  954. }
  955. /**
  956. * Validates association targets actually exist.
  957. *
  958. * @return void
  959. *
  960. * @throws MappingException
  961. */
  962. public function validateAssociations()
  963. {
  964. foreach ($this->associationMappings as $mapping) {
  965. if (
  966. ! class_exists($mapping['targetEntity'])
  967. && ! interface_exists($mapping['targetEntity'])
  968. && ! trait_exists($mapping['targetEntity'])
  969. ) {
  970. throw MappingException::invalidTargetEntityClass($mapping['targetEntity'], $this->name, $mapping['fieldName']);
  971. }
  972. }
  973. }
  974. /**
  975. * Validates lifecycle callbacks.
  976. *
  977. * @param ReflectionService $reflService
  978. *
  979. * @return void
  980. *
  981. * @throws MappingException
  982. */
  983. public function validateLifecycleCallbacks($reflService)
  984. {
  985. foreach ($this->lifecycleCallbacks as $callbacks) {
  986. foreach ($callbacks as $callbackFuncName) {
  987. if (! $reflService->hasPublicMethod($this->name, $callbackFuncName)) {
  988. throw MappingException::lifecycleCallbackMethodNotFound($this->name, $callbackFuncName);
  989. }
  990. }
  991. }
  992. }
  993. /**
  994. * {@inheritDoc}
  995. */
  996. public function getReflectionClass()
  997. {
  998. return $this->reflClass;
  999. }
  1000. /**
  1001. * @psalm-param array{usage?: mixed, region?: mixed} $cache
  1002. *
  1003. * @return void
  1004. */
  1005. public function enableCache(array $cache)
  1006. {
  1007. if (! isset($cache['usage'])) {
  1008. $cache['usage'] = self::CACHE_USAGE_READ_ONLY;
  1009. }
  1010. if (! isset($cache['region'])) {
  1011. $cache['region'] = strtolower(str_replace('\\', '_', $this->rootEntityName));
  1012. }
  1013. $this->cache = $cache;
  1014. }
  1015. /**
  1016. * @param string $fieldName
  1017. * @psalm-param array{usage?: int, region?: string} $cache
  1018. *
  1019. * @return void
  1020. */
  1021. public function enableAssociationCache($fieldName, array $cache)
  1022. {
  1023. $this->associationMappings[$fieldName]['cache'] = $this->getAssociationCacheDefaults($fieldName, $cache);
  1024. }
  1025. /**
  1026. * @param string $fieldName
  1027. * @param array $cache
  1028. * @psalm-param array{usage?: int, region?: string|null} $cache
  1029. *
  1030. * @return int[]|string[]
  1031. * @psalm-return array{usage: int, region: string|null}
  1032. */
  1033. public function getAssociationCacheDefaults($fieldName, array $cache)
  1034. {
  1035. if (! isset($cache['usage'])) {
  1036. $cache['usage'] = $this->cache['usage'] ?? self::CACHE_USAGE_READ_ONLY;
  1037. }
  1038. if (! isset($cache['region'])) {
  1039. $cache['region'] = strtolower(str_replace('\\', '_', $this->rootEntityName)) . '__' . $fieldName;
  1040. }
  1041. return $cache;
  1042. }
  1043. /**
  1044. * Sets the change tracking policy used by this class.
  1045. *
  1046. * @param int $policy
  1047. *
  1048. * @return void
  1049. */
  1050. public function setChangeTrackingPolicy($policy)
  1051. {
  1052. $this->changeTrackingPolicy = $policy;
  1053. }
  1054. /**
  1055. * Whether the change tracking policy of this class is "deferred explicit".
  1056. *
  1057. * @return bool
  1058. */
  1059. public function isChangeTrackingDeferredExplicit()
  1060. {
  1061. return $this->changeTrackingPolicy === self::CHANGETRACKING_DEFERRED_EXPLICIT;
  1062. }
  1063. /**
  1064. * Whether the change tracking policy of this class is "deferred implicit".
  1065. *
  1066. * @return bool
  1067. */
  1068. public function isChangeTrackingDeferredImplicit()
  1069. {
  1070. return $this->changeTrackingPolicy === self::CHANGETRACKING_DEFERRED_IMPLICIT;
  1071. }
  1072. /**
  1073. * Whether the change tracking policy of this class is "notify".
  1074. *
  1075. * @return bool
  1076. */
  1077. public function isChangeTrackingNotify()
  1078. {
  1079. return $this->changeTrackingPolicy === self::CHANGETRACKING_NOTIFY;
  1080. }
  1081. /**
  1082. * Checks whether a field is part of the identifier/primary key field(s).
  1083. *
  1084. * @param string $fieldName The field name.
  1085. *
  1086. * @return bool TRUE if the field is part of the table identifier/primary key field(s),
  1087. * FALSE otherwise.
  1088. */
  1089. public function isIdentifier($fieldName)
  1090. {
  1091. if (! $this->identifier) {
  1092. return false;
  1093. }
  1094. if (! $this->isIdentifierComposite) {
  1095. return $fieldName === $this->identifier[0];
  1096. }
  1097. return in_array($fieldName, $this->identifier, true);
  1098. }
  1099. /**
  1100. * Checks if the field is unique.
  1101. *
  1102. * @param string $fieldName The field name.
  1103. *
  1104. * @return bool TRUE if the field is unique, FALSE otherwise.
  1105. */
  1106. public function isUniqueField($fieldName)
  1107. {
  1108. $mapping = $this->getFieldMapping($fieldName);
  1109. return $mapping !== false && isset($mapping['unique']) && $mapping['unique'];
  1110. }
  1111. /**
  1112. * Checks if the field is not null.
  1113. *
  1114. * @param string $fieldName The field name.
  1115. *
  1116. * @return bool TRUE if the field is not null, FALSE otherwise.
  1117. */
  1118. public function isNullable($fieldName)
  1119. {
  1120. $mapping = $this->getFieldMapping($fieldName);
  1121. return $mapping !== false && isset($mapping['nullable']) && $mapping['nullable'];
  1122. }
  1123. /**
  1124. * Gets a column name for a field name.
  1125. * If the column name for the field cannot be found, the given field name
  1126. * is returned.
  1127. *
  1128. * @param string $fieldName The field name.
  1129. *
  1130. * @return string The column name.
  1131. */
  1132. public function getColumnName($fieldName)
  1133. {
  1134. return $this->columnNames[$fieldName] ?? $fieldName;
  1135. }
  1136. /**
  1137. * Gets the mapping of a (regular) field that holds some data but not a
  1138. * reference to another object.
  1139. *
  1140. * @param string $fieldName The field name.
  1141. *
  1142. * @return mixed[] The field mapping.
  1143. * @psalm-return array{
  1144. * type: string,
  1145. * fieldName: string,
  1146. * columnName?: string,
  1147. * inherited?: class-string,
  1148. * nullable?: bool,
  1149. * originalClass?: class-string,
  1150. * originalField?: string,
  1151. * scale?: int,
  1152. * precision?: int,
  1153. * length?: int
  1154. * }
  1155. *
  1156. * @throws MappingException
  1157. */
  1158. public function getFieldMapping($fieldName)
  1159. {
  1160. if (! isset($this->fieldMappings[$fieldName])) {
  1161. throw MappingException::mappingNotFound($this->name, $fieldName);
  1162. }
  1163. return $this->fieldMappings[$fieldName];
  1164. }
  1165. /**
  1166. * Gets the mapping of an association.
  1167. *
  1168. * @see ClassMetadataInfo::$associationMappings
  1169. *
  1170. * @param string $fieldName The field name that represents the association in
  1171. * the object model.
  1172. *
  1173. * @return mixed[] The mapping.
  1174. * @psalm-return array<string, mixed>
  1175. *
  1176. * @throws MappingException
  1177. */
  1178. public function getAssociationMapping($fieldName)
  1179. {
  1180. if (! isset($this->associationMappings[$fieldName])) {
  1181. throw MappingException::mappingNotFound($this->name, $fieldName);
  1182. }
  1183. return $this->associationMappings[$fieldName];
  1184. }
  1185. /**
  1186. * Gets all association mappings of the class.
  1187. *
  1188. * @psalm-return array<string, array<string, mixed>>
  1189. */
  1190. public function getAssociationMappings()
  1191. {
  1192. return $this->associationMappings;
  1193. }
  1194. /**
  1195. * Gets the field name for a column name.
  1196. * If no field name can be found the column name is returned.
  1197. *
  1198. * @param string $columnName The column name.
  1199. *
  1200. * @return string The column alias.
  1201. */
  1202. public function getFieldName($columnName)
  1203. {
  1204. return $this->fieldNames[$columnName] ?? $columnName;
  1205. }
  1206. /**
  1207. * Gets the named query.
  1208. *
  1209. * @see ClassMetadataInfo::$namedQueries
  1210. *
  1211. * @param string $queryName The query name.
  1212. *
  1213. * @return string
  1214. *
  1215. * @throws MappingException
  1216. */
  1217. public function getNamedQuery($queryName)
  1218. {
  1219. if (! isset($this->namedQueries[$queryName])) {
  1220. throw MappingException::queryNotFound($this->name, $queryName);
  1221. }
  1222. return $this->namedQueries[$queryName]['dql'];
  1223. }
  1224. /**
  1225. * Gets all named queries of the class.
  1226. *
  1227. * @return mixed[][]
  1228. * @psalm-return array<string, array<string, mixed>>
  1229. */
  1230. public function getNamedQueries()
  1231. {
  1232. return $this->namedQueries;
  1233. }
  1234. /**
  1235. * Gets the named native query.
  1236. *
  1237. * @see ClassMetadataInfo::$namedNativeQueries
  1238. *
  1239. * @param string $queryName The query name.
  1240. *
  1241. * @return mixed[]
  1242. * @psalm-return array<string, mixed>
  1243. *
  1244. * @throws MappingException
  1245. */
  1246. public function getNamedNativeQuery($queryName)
  1247. {
  1248. if (! isset($this->namedNativeQueries[$queryName])) {
  1249. throw MappingException::queryNotFound($this->name, $queryName);
  1250. }
  1251. return $this->namedNativeQueries[$queryName];
  1252. }
  1253. /**
  1254. * Gets all named native queries of the class.
  1255. *
  1256. * @psalm-return array<string, array<string, mixed>>
  1257. */
  1258. public function getNamedNativeQueries()
  1259. {
  1260. return $this->namedNativeQueries;
  1261. }
  1262. /**
  1263. * Gets the result set mapping.
  1264. *
  1265. * @see ClassMetadataInfo::$sqlResultSetMappings
  1266. *
  1267. * @param string $name The result set mapping name.
  1268. *
  1269. * @return mixed[]
  1270. * @psalm-return array{name: string, entities: array, columns: array}
  1271. *
  1272. * @throws MappingException
  1273. */
  1274. public function getSqlResultSetMapping($name)
  1275. {
  1276. if (! isset($this->sqlResultSetMappings[$name])) {
  1277. throw MappingException::resultMappingNotFound($this->name, $name);
  1278. }
  1279. return $this->sqlResultSetMappings[$name];
  1280. }
  1281. /**
  1282. * Gets all sql result set mappings of the class.
  1283. *
  1284. * @return mixed[]
  1285. * @psalm-return array<string, array{name: string, entities: array, columns: array}>
  1286. */
  1287. public function getSqlResultSetMappings()
  1288. {
  1289. return $this->sqlResultSetMappings;
  1290. }
  1291. /**
  1292. * Checks whether given property has type
  1293. *
  1294. * @param string $name Property name
  1295. */
  1296. private function isTypedProperty(string $name): bool
  1297. {
  1298. return PHP_VERSION_ID >= 70400
  1299. && isset($this->reflClass)
  1300. && $this->reflClass->hasProperty($name)
  1301. && $this->reflClass->getProperty($name)->hasType();
  1302. }
  1303. /**
  1304. * Validates & completes the given field mapping based on typed property.
  1305. *
  1306. * @param mixed[] $mapping The field mapping to validate & complete.
  1307. *
  1308. * @return mixed[] The updated mapping.
  1309. */
  1310. private function validateAndCompleteTypedFieldMapping(array $mapping): array
  1311. {
  1312. $type = $this->reflClass->getProperty($mapping['fieldName'])->getType();
  1313. if ($type) {
  1314. if (
  1315. ! isset($mapping['type'])
  1316. && ($type instanceof ReflectionNamedType)
  1317. ) {
  1318. switch ($type->getName()) {
  1319. case DateInterval::class:
  1320. $mapping['type'] = Types::DATEINTERVAL;
  1321. break;
  1322. case DateTime::class:
  1323. $mapping['type'] = Types::DATETIME_MUTABLE;
  1324. break;
  1325. case DateTimeImmutable::class:
  1326. $mapping['type'] = Types::DATETIME_IMMUTABLE;
  1327. break;
  1328. case 'array':
  1329. $mapping['type'] = Types::JSON;
  1330. break;
  1331. case 'bool':
  1332. $mapping['type'] = Types::BOOLEAN;
  1333. break;
  1334. case 'float':
  1335. $mapping['type'] = Types::FLOAT;
  1336. break;
  1337. case 'int':
  1338. $mapping['type'] = Types::INTEGER;
  1339. break;
  1340. case 'string':
  1341. $mapping['type'] = Types::STRING;
  1342. break;
  1343. }
  1344. }
  1345. }
  1346. return $mapping;
  1347. }
  1348. /**
  1349. * Validates & completes the basic mapping information based on typed property.
  1350. *
  1351. * @param mixed[] $mapping The mapping.
  1352. *
  1353. * @return mixed[] The updated mapping.
  1354. */
  1355. private function validateAndCompleteTypedAssociationMapping(array $mapping): array
  1356. {
  1357. $type = $this->reflClass->getProperty($mapping['fieldName'])->getType();
  1358. if ($type === null || ($mapping['type'] & self::TO_ONE) === 0) {
  1359. return $mapping;
  1360. }
  1361. if (! isset($mapping['targetEntity']) && $type instanceof ReflectionNamedType) {
  1362. $mapping['targetEntity'] = $type->getName();
  1363. }
  1364. return $mapping;
  1365. }
  1366. /**
  1367. * Validates & completes the given field mapping.
  1368. *
  1369. * @psalm-param array<string, mixed> $mapping The field mapping to validate & complete.
  1370. *
  1371. * @return mixed[] The updated mapping.
  1372. *
  1373. * @throws MappingException
  1374. */
  1375. protected function validateAndCompleteFieldMapping(array $mapping): array
  1376. {
  1377. // Check mandatory fields
  1378. if (! isset($mapping['fieldName']) || ! $mapping['fieldName']) {
  1379. throw MappingException::missingFieldName($this->name);
  1380. }
  1381. if ($this->isTypedProperty($mapping['fieldName'])) {
  1382. $mapping = $this->validateAndCompleteTypedFieldMapping($mapping);
  1383. }
  1384. if (! isset($mapping['type'])) {
  1385. // Default to string
  1386. $mapping['type'] = 'string';
  1387. }
  1388. // Complete fieldName and columnName mapping
  1389. if (! isset($mapping['columnName'])) {
  1390. $mapping['columnName'] = $this->namingStrategy->propertyToColumnName($mapping['fieldName'], $this->name);
  1391. }
  1392. if ($mapping['columnName'][0] === '`') {
  1393. $mapping['columnName'] = trim($mapping['columnName'], '`');
  1394. $mapping['quoted'] = true;
  1395. }
  1396. $this->columnNames[$mapping['fieldName']] = $mapping['columnName'];
  1397. if (isset($this->fieldNames[$mapping['columnName']]) || ($this->discriminatorColumn && $this->discriminatorColumn['name'] === $mapping['columnName'])) {
  1398. throw MappingException::duplicateColumnName($this->name, $mapping['columnName']);
  1399. }
  1400. $this->fieldNames[$mapping['columnName']] = $mapping['fieldName'];
  1401. // Complete id mapping
  1402. if (isset($mapping['id']) && $mapping['id'] === true) {
  1403. if ($this->versionField === $mapping['fieldName']) {
  1404. throw MappingException::cannotVersionIdField($this->name, $mapping['fieldName']);
  1405. }
  1406. if (! in_array($mapping['fieldName'], $this->identifier, true)) {
  1407. $this->identifier[] = $mapping['fieldName'];
  1408. }
  1409. // Check for composite key
  1410. if (! $this->isIdentifierComposite && count($this->identifier) > 1) {
  1411. $this->isIdentifierComposite = true;
  1412. }
  1413. }
  1414. if (Type::hasType($mapping['type']) && Type::getType($mapping['type'])->canRequireSQLConversion()) {
  1415. if (isset($mapping['id']) && $mapping['id'] === true) {
  1416. throw MappingException::sqlConversionNotAllowedForIdentifiers($this->name, $mapping['fieldName'], $mapping['type']);
  1417. }
  1418. $mapping['requireSQLConversion'] = true;
  1419. }
  1420. return $mapping;
  1421. }
  1422. /**
  1423. * Validates & completes the basic mapping information that is common to all
  1424. * association mappings (one-to-one, many-ot-one, one-to-many, many-to-many).
  1425. *
  1426. * @psalm-param array<string, mixed> $mapping The mapping.
  1427. *
  1428. * @return mixed[] The updated mapping.
  1429. * @psalm-return array{
  1430. * mappedBy: mixed|null,
  1431. * inversedBy: mixed|null,
  1432. * isOwningSide: bool,
  1433. * sourceEntity: class-string,
  1434. * targetEntity: string,
  1435. * fieldName: mixed,
  1436. * fetch: mixed,
  1437. * cascade: array<array-key,string>,
  1438. * isCascadeRemove: bool,
  1439. * isCascadePersist: bool,
  1440. * isCascadeRefresh: bool,
  1441. * isCascadeMerge: bool,
  1442. * isCascadeDetach: bool,
  1443. * type: int,
  1444. * originalField: string,
  1445. * originalClass: class-string,
  1446. * ?orphanRemoval: bool
  1447. * }
  1448. *
  1449. * @throws MappingException If something is wrong with the …

Large files files are truncated, but you can click here to view the full file