vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php line 1150

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\ORM\Mapping;
  4. use BackedEnum;
  5. use BadMethodCallException;
  6. use DateInterval;
  7. use DateTime;
  8. use DateTimeImmutable;
  9. use Doctrine\DBAL\Platforms\AbstractPlatform;
  10. use Doctrine\DBAL\Types\Type;
  11. use Doctrine\DBAL\Types\Types;
  12. use Doctrine\Deprecations\Deprecation;
  13. use Doctrine\Instantiator\Instantiator;
  14. use Doctrine\Instantiator\InstantiatorInterface;
  15. use Doctrine\ORM\Cache\Exception\NonCacheableEntityAssociation;
  16. use Doctrine\ORM\EntityRepository;
  17. use Doctrine\ORM\Id\AbstractIdGenerator;
  18. use Doctrine\Persistence\Mapping\ClassMetadata;
  19. use Doctrine\Persistence\Mapping\ReflectionService;
  20. use InvalidArgumentException;
  21. use LogicException;
  22. use ReflectionClass;
  23. use ReflectionEnum;
  24. use ReflectionNamedType;
  25. use ReflectionProperty;
  26. use RuntimeException;
  27. use function array_diff;
  28. use function array_flip;
  29. use function array_intersect;
  30. use function array_keys;
  31. use function array_map;
  32. use function array_merge;
  33. use function array_pop;
  34. use function array_values;
  35. use function assert;
  36. use function class_exists;
  37. use function count;
  38. use function enum_exists;
  39. use function explode;
  40. use function gettype;
  41. use function in_array;
  42. use function interface_exists;
  43. use function is_array;
  44. use function is_subclass_of;
  45. use function ltrim;
  46. use function method_exists;
  47. use function spl_object_id;
  48. use function str_replace;
  49. use function strpos;
  50. use function strtolower;
  51. use function trait_exists;
  52. use function trim;
  53. use const PHP_VERSION_ID;
  54. /**
  55.  * A <tt>ClassMetadata</tt> instance holds all the object-relational mapping metadata
  56.  * of an entity and its associations.
  57.  *
  58.  * Once populated, ClassMetadata instances are usually cached in a serialized form.
  59.  *
  60.  * <b>IMPORTANT NOTE:</b>
  61.  *
  62.  * The fields of this class are only public for 2 reasons:
  63.  * 1) To allow fast READ access.
  64.  * 2) To drastically reduce the size of a serialized instance (private/protected members
  65.  *    get the whole class name, namespace inclusive, prepended to every property in
  66.  *    the serialized representation).
  67.  *
  68.  * @template-covariant T of object
  69.  * @template-implements ClassMetadata<T>
  70.  * @psalm-type FieldMapping = array{
  71.  *      type: string,
  72.  *      fieldName: string,
  73.  *      columnName: string,
  74.  *      length?: int,
  75.  *      id?: bool,
  76.  *      nullable?: bool,
  77.  *      notInsertable?: bool,
  78.  *      notUpdatable?: bool,
  79.  *      generated?: string,
  80.  *      enumType?: class-string<BackedEnum>,
  81.  *      columnDefinition?: string,
  82.  *      precision?: int,
  83.  *      scale?: int,
  84.  *      unique?: string,
  85.  *      inherited?: class-string,
  86.  *      originalClass?: class-string,
  87.  *      originalField?: string,
  88.  *      quoted?: bool,
  89.  *      requireSQLConversion?: bool,
  90.  *      declared?: class-string,
  91.  *      declaredField?: string,
  92.  *      options?: array<string, mixed>
  93.  * }
  94.  */
  95. class ClassMetadataInfo implements ClassMetadata
  96. {
  97.     /* The inheritance mapping types */
  98.     /**
  99.      * NONE means the class does not participate in an inheritance hierarchy
  100.      * and therefore does not need an inheritance mapping type.
  101.      */
  102.     public const INHERITANCE_TYPE_NONE 1;
  103.     /**
  104.      * JOINED means the class will be persisted according to the rules of
  105.      * <tt>Class Table Inheritance</tt>.
  106.      */
  107.     public const INHERITANCE_TYPE_JOINED 2;
  108.     /**
  109.      * SINGLE_TABLE means the class will be persisted according to the rules of
  110.      * <tt>Single Table Inheritance</tt>.
  111.      */
  112.     public const INHERITANCE_TYPE_SINGLE_TABLE 3;
  113.     /**
  114.      * TABLE_PER_CLASS means the class will be persisted according to the rules
  115.      * of <tt>Concrete Table Inheritance</tt>.
  116.      */
  117.     public const INHERITANCE_TYPE_TABLE_PER_CLASS 4;
  118.     /* The Id generator types. */
  119.     /**
  120.      * AUTO means the generator type will depend on what the used platform prefers.
  121.      * Offers full portability.
  122.      */
  123.     public const GENERATOR_TYPE_AUTO 1;
  124.     /**
  125.      * SEQUENCE means a separate sequence object will be used. Platforms that do
  126.      * not have native sequence support may emulate it. Full portability is currently
  127.      * not guaranteed.
  128.      */
  129.     public const GENERATOR_TYPE_SEQUENCE 2;
  130.     /**
  131.      * TABLE means a separate table is used for id generation.
  132.      * Offers full portability (in that it results in an exception being thrown
  133.      * no matter the platform).
  134.      *
  135.      * @deprecated no replacement planned
  136.      */
  137.     public const GENERATOR_TYPE_TABLE 3;
  138.     /**
  139.      * IDENTITY means an identity column is used for id generation. The database
  140.      * will fill in the id column on insertion. Platforms that do not support
  141.      * native identity columns may emulate them. Full portability is currently
  142.      * not guaranteed.
  143.      */
  144.     public const GENERATOR_TYPE_IDENTITY 4;
  145.     /**
  146.      * NONE means the class does not have a generated id. That means the class
  147.      * must have a natural, manually assigned id.
  148.      */
  149.     public const GENERATOR_TYPE_NONE 5;
  150.     /**
  151.      * UUID means that a UUID/GUID expression is used for id generation. Full
  152.      * portability is currently not guaranteed.
  153.      *
  154.      * @deprecated use an application-side generator instead
  155.      */
  156.     public const GENERATOR_TYPE_UUID 6;
  157.     /**
  158.      * CUSTOM means that customer will use own ID generator that supposedly work
  159.      */
  160.     public const GENERATOR_TYPE_CUSTOM 7;
  161.     /**
  162.      * DEFERRED_IMPLICIT means that changes of entities are calculated at commit-time
  163.      * by doing a property-by-property comparison with the original data. This will
  164.      * be done for all entities that are in MANAGED state at commit-time.
  165.      *
  166.      * This is the default change tracking policy.
  167.      */
  168.     public const CHANGETRACKING_DEFERRED_IMPLICIT 1;
  169.     /**
  170.      * DEFERRED_EXPLICIT means that changes of entities are calculated at commit-time
  171.      * by doing a property-by-property comparison with the original data. This will
  172.      * be done only for entities that were explicitly saved (through persist() or a cascade).
  173.      */
  174.     public const CHANGETRACKING_DEFERRED_EXPLICIT 2;
  175.     /**
  176.      * NOTIFY means that Doctrine relies on the entities sending out notifications
  177.      * when their properties change. Such entity classes must implement
  178.      * the <tt>NotifyPropertyChanged</tt> interface.
  179.      */
  180.     public const CHANGETRACKING_NOTIFY 3;
  181.     /**
  182.      * Specifies that an association is to be fetched when it is first accessed.
  183.      */
  184.     public const FETCH_LAZY 2;
  185.     /**
  186.      * Specifies that an association is to be fetched when the owner of the
  187.      * association is fetched.
  188.      */
  189.     public const FETCH_EAGER 3;
  190.     /**
  191.      * Specifies that an association is to be fetched lazy (on first access) and that
  192.      * commands such as Collection#count, Collection#slice are issued directly against
  193.      * the database if the collection is not yet initialized.
  194.      */
  195.     public const FETCH_EXTRA_LAZY 4;
  196.     /**
  197.      * Identifies a one-to-one association.
  198.      */
  199.     public const ONE_TO_ONE 1;
  200.     /**
  201.      * Identifies a many-to-one association.
  202.      */
  203.     public const MANY_TO_ONE 2;
  204.     /**
  205.      * Identifies a one-to-many association.
  206.      */
  207.     public const ONE_TO_MANY 4;
  208.     /**
  209.      * Identifies a many-to-many association.
  210.      */
  211.     public const MANY_TO_MANY 8;
  212.     /**
  213.      * Combined bitmask for to-one (single-valued) associations.
  214.      */
  215.     public const TO_ONE 3;
  216.     /**
  217.      * Combined bitmask for to-many (collection-valued) associations.
  218.      */
  219.     public const TO_MANY 12;
  220.     /**
  221.      * ReadOnly cache can do reads, inserts and deletes, cannot perform updates or employ any locks,
  222.      */
  223.     public const CACHE_USAGE_READ_ONLY 1;
  224.     /**
  225.      * Nonstrict Read Write Cache doesn’t employ any locks but can do inserts, update and deletes.
  226.      */
  227.     public const CACHE_USAGE_NONSTRICT_READ_WRITE 2;
  228.     /**
  229.      * Read Write Attempts to lock the entity before update/delete.
  230.      */
  231.     public const CACHE_USAGE_READ_WRITE 3;
  232.     /**
  233.      * The value of this column is never generated by the database.
  234.      */
  235.     public const GENERATED_NEVER 0;
  236.     /**
  237.      * The value of this column is generated by the database on INSERT, but not on UPDATE.
  238.      */
  239.     public const GENERATED_INSERT 1;
  240.     /**
  241.      * The value of this column is generated by the database on both INSERT and UDPATE statements.
  242.      */
  243.     public const GENERATED_ALWAYS 2;
  244.     /**
  245.      * READ-ONLY: The name of the entity class.
  246.      *
  247.      * @var string
  248.      * @psalm-var class-string<T>
  249.      */
  250.     public $name;
  251.     /**
  252.      * READ-ONLY: The namespace the entity class is contained in.
  253.      *
  254.      * @var string
  255.      * @todo Not really needed. Usage could be localized.
  256.      */
  257.     public $namespace;
  258.     /**
  259.      * READ-ONLY: The name of the entity class that is at the root of the mapped entity inheritance
  260.      * hierarchy. If the entity is not part of a mapped inheritance hierarchy this is the same
  261.      * as {@link $name}.
  262.      *
  263.      * @var string
  264.      * @psalm-var class-string
  265.      */
  266.     public $rootEntityName;
  267.     /**
  268.      * READ-ONLY: The definition of custom generator. Only used for CUSTOM
  269.      * generator type
  270.      *
  271.      * The definition has the following structure:
  272.      * <code>
  273.      * array(
  274.      *     'class' => 'ClassName',
  275.      * )
  276.      * </code>
  277.      *
  278.      * @todo Merge with tableGeneratorDefinition into generic generatorDefinition
  279.      * @var array<string, string>|null
  280.      */
  281.     public $customGeneratorDefinition;
  282.     /**
  283.      * The name of the custom repository class used for the entity class.
  284.      * (Optional).
  285.      *
  286.      * @var string|null
  287.      * @psalm-var ?class-string<EntityRepository>
  288.      */
  289.     public $customRepositoryClassName;
  290.     /**
  291.      * READ-ONLY: Whether this class describes the mapping of a mapped superclass.
  292.      *
  293.      * @var bool
  294.      */
  295.     public $isMappedSuperclass false;
  296.     /**
  297.      * READ-ONLY: Whether this class describes the mapping of an embeddable class.
  298.      *
  299.      * @var bool
  300.      */
  301.     public $isEmbeddedClass false;
  302.     /**
  303.      * READ-ONLY: The names of the parent classes (ancestors).
  304.      *
  305.      * @psalm-var list<class-string>
  306.      */
  307.     public $parentClasses = [];
  308.     /**
  309.      * READ-ONLY: The names of all subclasses (descendants).
  310.      *
  311.      * @psalm-var list<class-string>
  312.      */
  313.     public $subClasses = [];
  314.     /**
  315.      * READ-ONLY: The names of all embedded classes based on properties.
  316.      *
  317.      * @psalm-var array<string, mixed[]>
  318.      */
  319.     public $embeddedClasses = [];
  320.     /**
  321.      * READ-ONLY: The named queries allowed to be called directly from Repository.
  322.      *
  323.      * @psalm-var array<string, array<string, mixed>>
  324.      */
  325.     public $namedQueries = [];
  326.     /**
  327.      * READ-ONLY: The named native queries allowed to be called directly from Repository.
  328.      *
  329.      * A native SQL named query definition has the following structure:
  330.      * <pre>
  331.      * array(
  332.      *     'name'               => <query name>,
  333.      *     'query'              => <sql query>,
  334.      *     'resultClass'        => <class of the result>,
  335.      *     'resultSetMapping'   => <name of a SqlResultSetMapping>
  336.      * )
  337.      * </pre>
  338.      *
  339.      * @psalm-var array<string, array<string, mixed>>
  340.      */
  341.     public $namedNativeQueries = [];
  342.     /**
  343.      * READ-ONLY: The mappings of the results of native SQL queries.
  344.      *
  345.      * A native result mapping definition has the following structure:
  346.      * <pre>
  347.      * array(
  348.      *     'name'               => <result name>,
  349.      *     'entities'           => array(<entity result mapping>),
  350.      *     'columns'            => array(<column result mapping>)
  351.      * )
  352.      * </pre>
  353.      *
  354.      * @psalm-var array<string, array{
  355.      *                name: string,
  356.      *                entities: mixed[],
  357.      *                columns: mixed[]
  358.      *            }>
  359.      */
  360.     public $sqlResultSetMappings = [];
  361.     /**
  362.      * READ-ONLY: The field names of all fields that are part of the identifier/primary key
  363.      * of the mapped entity class.
  364.      *
  365.      * @psalm-var list<string>
  366.      */
  367.     public $identifier = [];
  368.     /**
  369.      * READ-ONLY: The inheritance mapping type used by the class.
  370.      *
  371.      * @var int
  372.      * @psalm-var self::INHERITANCE_TYPE_*
  373.      */
  374.     public $inheritanceType self::INHERITANCE_TYPE_NONE;
  375.     /**
  376.      * READ-ONLY: The Id generator type used by the class.
  377.      *
  378.      * @var int
  379.      * @psalm-var self::GENERATOR_TYPE_*
  380.      */
  381.     public $generatorType self::GENERATOR_TYPE_NONE;
  382.     /**
  383.      * READ-ONLY: The field mappings of the class.
  384.      * Keys are field names and values are mapping definitions.
  385.      *
  386.      * The mapping definition array has the following values:
  387.      *
  388.      * - <b>fieldName</b> (string)
  389.      * The name of the field in the Entity.
  390.      *
  391.      * - <b>type</b> (string)
  392.      * The type name of the mapped field. Can be one of Doctrine's mapping types
  393.      * or a custom mapping type.
  394.      *
  395.      * - <b>columnName</b> (string, optional)
  396.      * The column name. Optional. Defaults to the field name.
  397.      *
  398.      * - <b>length</b> (integer, optional)
  399.      * The database length of the column. Optional. Default value taken from
  400.      * the type.
  401.      *
  402.      * - <b>id</b> (boolean, optional)
  403.      * Marks the field as the primary key of the entity. Multiple fields of an
  404.      * entity can have the id attribute, forming a composite key.
  405.      *
  406.      * - <b>nullable</b> (boolean, optional)
  407.      * Whether the column is nullable. Defaults to FALSE.
  408.      *
  409.      * - <b>'notInsertable'</b> (boolean, optional)
  410.      * Whether the column is not insertable. Optional. Is only set if value is TRUE.
  411.      *
  412.      * - <b>'notUpdatable'</b> (boolean, optional)
  413.      * Whether the column is updatable. Optional. Is only set if value is TRUE.
  414.      *
  415.      * - <b>columnDefinition</b> (string, optional, schema-only)
  416.      * The SQL fragment that is used when generating the DDL for the column.
  417.      *
  418.      * - <b>precision</b> (integer, optional, schema-only)
  419.      * The precision of a decimal column. Only valid if the column type is decimal.
  420.      *
  421.      * - <b>scale</b> (integer, optional, schema-only)
  422.      * The scale of a decimal column. Only valid if the column type is decimal.
  423.      *
  424.      * - <b>'unique'</b> (string, optional, schema-only)
  425.      * Whether a unique constraint should be generated for the column.
  426.      *
  427.      * @var mixed[]
  428.      * @psalm-var array<string, FieldMapping>
  429.      */
  430.     public $fieldMappings = [];
  431.     /**
  432.      * READ-ONLY: An array of field names. Used to look up field names from column names.
  433.      * Keys are column names and values are field names.
  434.      *
  435.      * @psalm-var array<string, string>
  436.      */
  437.     public $fieldNames = [];
  438.     /**
  439.      * READ-ONLY: A map of field names to column names. Keys are field names and values column names.
  440.      * Used to look up column names from field names.
  441.      * This is the reverse lookup map of $_fieldNames.
  442.      *
  443.      * @deprecated 3.0 Remove this.
  444.      *
  445.      * @var mixed[]
  446.      */
  447.     public $columnNames = [];
  448.     /**
  449.      * READ-ONLY: The discriminator value of this class.
  450.      *
  451.      * <b>This does only apply to the JOINED and SINGLE_TABLE inheritance mapping strategies
  452.      * where a discriminator column is used.</b>
  453.      *
  454.      * @see discriminatorColumn
  455.      *
  456.      * @var mixed
  457.      */
  458.     public $discriminatorValue;
  459.     /**
  460.      * READ-ONLY: The discriminator map of all mapped classes in the hierarchy.
  461.      *
  462.      * <b>This does only apply to the JOINED and SINGLE_TABLE inheritance mapping strategies
  463.      * where a discriminator column is used.</b>
  464.      *
  465.      * @see discriminatorColumn
  466.      *
  467.      * @var mixed
  468.      */
  469.     public $discriminatorMap = [];
  470.     /**
  471.      * READ-ONLY: The definition of the discriminator column used in JOINED and SINGLE_TABLE
  472.      * inheritance mappings.
  473.      *
  474.      * @psalm-var array<string, mixed>|null
  475.      */
  476.     public $discriminatorColumn;
  477.     /**
  478.      * READ-ONLY: The primary table definition. The definition is an array with the
  479.      * following entries:
  480.      *
  481.      * name => <tableName>
  482.      * schema => <schemaName>
  483.      * indexes => array
  484.      * uniqueConstraints => array
  485.      *
  486.      * @var mixed[]
  487.      * @psalm-var array{
  488.      *               name: string,
  489.      *               schema: string,
  490.      *               indexes: array,
  491.      *               uniqueConstraints: array,
  492.      *               options: array<string, mixed>,
  493.      *               quoted?: bool
  494.      *           }
  495.      */
  496.     public $table;
  497.     /**
  498.      * READ-ONLY: The registered lifecycle callbacks for entities of this class.
  499.      *
  500.      * @psalm-var array<string, list<string>>
  501.      */
  502.     public $lifecycleCallbacks = [];
  503.     /**
  504.      * READ-ONLY: The registered entity listeners.
  505.      *
  506.      * @psalm-var array<string, list<array{class: class-string, method: string}>>
  507.      */
  508.     public $entityListeners = [];
  509.     /**
  510.      * READ-ONLY: The association mappings of this class.
  511.      *
  512.      * The mapping definition array supports the following keys:
  513.      *
  514.      * - <b>fieldName</b> (string)
  515.      * The name of the field in the entity the association is mapped to.
  516.      *
  517.      * - <b>targetEntity</b> (string)
  518.      * The class name of the target entity. If it is fully-qualified it is used as is.
  519.      * If it is a simple, unqualified class name the namespace is assumed to be the same
  520.      * as the namespace of the source entity.
  521.      *
  522.      * - <b>mappedBy</b> (string, required for bidirectional associations)
  523.      * The name of the field that completes the bidirectional association on the owning side.
  524.      * This key must be specified on the inverse side of a bidirectional association.
  525.      *
  526.      * - <b>inversedBy</b> (string, required for bidirectional associations)
  527.      * The name of the field that completes the bidirectional association on the inverse side.
  528.      * This key must be specified on the owning side of a bidirectional association.
  529.      *
  530.      * - <b>cascade</b> (array, optional)
  531.      * The names of persistence operations to cascade on the association. The set of possible
  532.      * values are: "persist", "remove", "detach", "merge", "refresh", "all" (implies all others).
  533.      *
  534.      * - <b>orderBy</b> (array, one-to-many/many-to-many only)
  535.      * A map of field names (of the target entity) to sorting directions (ASC/DESC).
  536.      * Example: array('priority' => 'desc')
  537.      *
  538.      * - <b>fetch</b> (integer, optional)
  539.      * The fetching strategy to use for the association, usually defaults to FETCH_LAZY.
  540.      * Possible values are: ClassMetadata::FETCH_EAGER, ClassMetadata::FETCH_LAZY.
  541.      *
  542.      * - <b>joinTable</b> (array, optional, many-to-many only)
  543.      * Specification of the join table and its join columns (foreign keys).
  544.      * Only valid for many-to-many mappings. Note that one-to-many associations can be mapped
  545.      * through a join table by simply mapping the association as many-to-many with a unique
  546.      * constraint on the join table.
  547.      *
  548.      * - <b>indexBy</b> (string, optional, to-many only)
  549.      * Specification of a field on target-entity that is used to index the collection by.
  550.      * This field HAS to be either the primary key or a unique column. Otherwise the collection
  551.      * does not contain all the entities that are actually related.
  552.      *
  553.      * A join table definition has the following structure:
  554.      * <pre>
  555.      * array(
  556.      *     'name' => <join table name>,
  557.      *      'joinColumns' => array(<join column mapping from join table to source table>),
  558.      *      'inverseJoinColumns' => array(<join column mapping from join table to target table>)
  559.      * )
  560.      * </pre>
  561.      *
  562.      * @psalm-var array<string, array<string, mixed>>
  563.      */
  564.     public $associationMappings = [];
  565.     /**
  566.      * READ-ONLY: Flag indicating whether the identifier/primary key of the class is composite.
  567.      *
  568.      * @var bool
  569.      */
  570.     public $isIdentifierComposite false;
  571.     /**
  572.      * READ-ONLY: Flag indicating whether the identifier/primary key contains at least one foreign key association.
  573.      *
  574.      * This flag is necessary because some code blocks require special treatment of this cases.
  575.      *
  576.      * @var bool
  577.      */
  578.     public $containsForeignIdentifier false;
  579.     /**
  580.      * READ-ONLY: The ID generator used for generating IDs for this class.
  581.      *
  582.      * @var AbstractIdGenerator
  583.      * @todo Remove!
  584.      */
  585.     public $idGenerator;
  586.     /**
  587.      * READ-ONLY: The definition of the sequence generator of this class. Only used for the
  588.      * SEQUENCE generation strategy.
  589.      *
  590.      * The definition has the following structure:
  591.      * <code>
  592.      * array(
  593.      *     'sequenceName' => 'name',
  594.      *     'allocationSize' => '20',
  595.      *     'initialValue' => '1'
  596.      * )
  597.      * </code>
  598.      *
  599.      * @var array<string, mixed>
  600.      * @psalm-var array{sequenceName: string, allocationSize: string, initialValue: string, quoted?: mixed}
  601.      * @todo Merge with tableGeneratorDefinition into generic generatorDefinition
  602.      */
  603.     public $sequenceGeneratorDefinition;
  604.     /**
  605.      * READ-ONLY: The definition of the table generator of this class. Only used for the
  606.      * TABLE generation strategy.
  607.      *
  608.      * @deprecated
  609.      *
  610.      * @var array<string, mixed>
  611.      */
  612.     public $tableGeneratorDefinition;
  613.     /**
  614.      * READ-ONLY: The policy used for change-tracking on entities of this class.
  615.      *
  616.      * @var int
  617.      */
  618.     public $changeTrackingPolicy self::CHANGETRACKING_DEFERRED_IMPLICIT;
  619.     /**
  620.      * READ-ONLY: A Flag indicating whether one or more columns of this class
  621.      * have to be reloaded after insert / update operations.
  622.      *
  623.      * @var bool
  624.      */
  625.     public $requiresFetchAfterChange false;
  626.     /**
  627.      * READ-ONLY: A flag for whether or not instances of this class are to be versioned
  628.      * with optimistic locking.
  629.      *
  630.      * @var bool
  631.      */
  632.     public $isVersioned false;
  633.     /**
  634.      * READ-ONLY: The name of the field which is used for versioning in optimistic locking (if any).
  635.      *
  636.      * @var mixed
  637.      */
  638.     public $versionField;
  639.     /** @var mixed[]|null */
  640.     public $cache;
  641.     /**
  642.      * The ReflectionClass instance of the mapped class.
  643.      *
  644.      * @var ReflectionClass|null
  645.      */
  646.     public $reflClass;
  647.     /**
  648.      * Is this entity marked as "read-only"?
  649.      *
  650.      * That means it is never considered for change-tracking in the UnitOfWork. It is a very helpful performance
  651.      * optimization for entities that are immutable, either in your domain or through the relation database
  652.      * (coming from a view, or a history table for example).
  653.      *
  654.      * @var bool
  655.      */
  656.     public $isReadOnly false;
  657.     /**
  658.      * NamingStrategy determining the default column and table names.
  659.      *
  660.      * @var NamingStrategy
  661.      */
  662.     protected $namingStrategy;
  663.     /**
  664.      * The ReflectionProperty instances of the mapped class.
  665.      *
  666.      * @var array<string, ReflectionProperty|null>
  667.      */
  668.     public $reflFields = [];
  669.     /** @var InstantiatorInterface|null */
  670.     private $instantiator;
  671.     /**
  672.      * Initializes a new ClassMetadata instance that will hold the object-relational mapping
  673.      * metadata of the class with the given name.
  674.      *
  675.      * @param string $entityName The name of the entity class the new instance is used for.
  676.      * @psalm-param class-string<T> $entityName
  677.      */
  678.     public function __construct($entityName, ?NamingStrategy $namingStrategy null)
  679.     {
  680.         $this->name           $entityName;
  681.         $this->rootEntityName $entityName;
  682.         $this->namingStrategy $namingStrategy ?: new DefaultNamingStrategy();
  683.         $this->instantiator   = new Instantiator();
  684.     }
  685.     /**
  686.      * Gets the ReflectionProperties of the mapped class.
  687.      *
  688.      * @return ReflectionProperty[]|null[] An array of ReflectionProperty instances.
  689.      * @psalm-return array<ReflectionProperty|null>
  690.      */
  691.     public function getReflectionProperties()
  692.     {
  693.         return $this->reflFields;
  694.     }
  695.     /**
  696.      * Gets a ReflectionProperty for a specific field of the mapped class.
  697.      *
  698.      * @param string $name
  699.      *
  700.      * @return ReflectionProperty
  701.      */
  702.     public function getReflectionProperty($name)
  703.     {
  704.         return $this->reflFields[$name];
  705.     }
  706.     /**
  707.      * Gets the ReflectionProperty for the single identifier field.
  708.      *
  709.      * @return ReflectionProperty
  710.      *
  711.      * @throws BadMethodCallException If the class has a composite identifier.
  712.      */
  713.     public function getSingleIdReflectionProperty()
  714.     {
  715.         if ($this->isIdentifierComposite) {
  716.             throw new BadMethodCallException('Class ' $this->name ' has a composite identifier.');
  717.         }
  718.         return $this->reflFields[$this->identifier[0]];
  719.     }
  720.     /**
  721.      * Extracts the identifier values of an entity of this class.
  722.      *
  723.      * For composite identifiers, the identifier values are returned as an array
  724.      * with the same order as the field order in {@link identifier}.
  725.      *
  726.      * @param object $entity
  727.      *
  728.      * @return array<string, mixed>
  729.      */
  730.     public function getIdentifierValues($entity)
  731.     {
  732.         if ($this->isIdentifierComposite) {
  733.             $id = [];
  734.             foreach ($this->identifier as $idField) {
  735.                 $value $this->reflFields[$idField]->getValue($entity);
  736.                 if ($value !== null) {
  737.                     $id[$idField] = $value;
  738.                 }
  739.             }
  740.             return $id;
  741.         }
  742.         $id    $this->identifier[0];
  743.         $value $this->reflFields[$id]->getValue($entity);
  744.         if ($value === null) {
  745.             return [];
  746.         }
  747.         return [$id => $value];
  748.     }
  749.     /**
  750.      * Populates the entity identifier of an entity.
  751.      *
  752.      * @param object $entity
  753.      * @psalm-param array<string, mixed> $id
  754.      *
  755.      * @return void
  756.      *
  757.      * @todo Rename to assignIdentifier()
  758.      */
  759.     public function setIdentifierValues($entity, array $id)
  760.     {
  761.         foreach ($id as $idField => $idValue) {
  762.             $this->reflFields[$idField]->setValue($entity$idValue);
  763.         }
  764.     }
  765.     /**
  766.      * Sets the specified field to the specified value on the given entity.
  767.      *
  768.      * @param object $entity
  769.      * @param string $field
  770.      * @param mixed  $value
  771.      *
  772.      * @return void
  773.      */
  774.     public function setFieldValue($entity$field$value)
  775.     {
  776.         $this->reflFields[$field]->setValue($entity$value);
  777.     }
  778.     /**
  779.      * Gets the specified field's value off the given entity.
  780.      *
  781.      * @param object $entity
  782.      * @param string $field
  783.      *
  784.      * @return mixed
  785.      */
  786.     public function getFieldValue($entity$field)
  787.     {
  788.         return $this->reflFields[$field]->getValue($entity);
  789.     }
  790.     /**
  791.      * Creates a string representation of this instance.
  792.      *
  793.      * @return string The string representation of this instance.
  794.      *
  795.      * @todo Construct meaningful string representation.
  796.      */
  797.     public function __toString()
  798.     {
  799.         return self::class . '@' spl_object_id($this);
  800.     }
  801.     /**
  802.      * Determines which fields get serialized.
  803.      *
  804.      * It is only serialized what is necessary for best unserialization performance.
  805.      * That means any metadata properties that are not set or empty or simply have
  806.      * their default value are NOT serialized.
  807.      *
  808.      * Parts that are also NOT serialized because they can not be properly unserialized:
  809.      *      - reflClass (ReflectionClass)
  810.      *      - reflFields (ReflectionProperty array)
  811.      *
  812.      * @return string[] The names of all the fields that should be serialized.
  813.      */
  814.     public function __sleep()
  815.     {
  816.         // This metadata is always serialized/cached.
  817.         $serialized = [
  818.             'associationMappings',
  819.             'columnNames'//TODO: 3.0 Remove this. Can use fieldMappings[$fieldName]['columnName']
  820.             'fieldMappings',
  821.             'fieldNames',
  822.             'embeddedClasses',
  823.             'identifier',
  824.             'isIdentifierComposite'// TODO: REMOVE
  825.             'name',
  826.             'namespace'// TODO: REMOVE
  827.             'table',
  828.             'rootEntityName',
  829.             'idGenerator'//TODO: Does not really need to be serialized. Could be moved to runtime.
  830.         ];
  831.         // The rest of the metadata is only serialized if necessary.
  832.         if ($this->changeTrackingPolicy !== self::CHANGETRACKING_DEFERRED_IMPLICIT) {
  833.             $serialized[] = 'changeTrackingPolicy';
  834.         }
  835.         if ($this->customRepositoryClassName) {
  836.             $serialized[] = 'customRepositoryClassName';
  837.         }
  838.         if ($this->inheritanceType !== self::INHERITANCE_TYPE_NONE) {
  839.             $serialized[] = 'inheritanceType';
  840.             $serialized[] = 'discriminatorColumn';
  841.             $serialized[] = 'discriminatorValue';
  842.             $serialized[] = 'discriminatorMap';
  843.             $serialized[] = 'parentClasses';
  844.             $serialized[] = 'subClasses';
  845.         }
  846.         if ($this->generatorType !== self::GENERATOR_TYPE_NONE) {
  847.             $serialized[] = 'generatorType';
  848.             if ($this->generatorType === self::GENERATOR_TYPE_SEQUENCE) {
  849.                 $serialized[] = 'sequenceGeneratorDefinition';
  850.             }
  851.         }
  852.         if ($this->isMappedSuperclass) {
  853.             $serialized[] = 'isMappedSuperclass';
  854.         }
  855.         if ($this->isEmbeddedClass) {
  856.             $serialized[] = 'isEmbeddedClass';
  857.         }
  858.         if ($this->containsForeignIdentifier) {
  859.             $serialized[] = 'containsForeignIdentifier';
  860.         }
  861.         if ($this->isVersioned) {
  862.             $serialized[] = 'isVersioned';
  863.             $serialized[] = 'versionField';
  864.         }
  865.         if ($this->lifecycleCallbacks) {
  866.             $serialized[] = 'lifecycleCallbacks';
  867.         }
  868.         if ($this->entityListeners) {
  869.             $serialized[] = 'entityListeners';
  870.         }
  871.         if ($this->namedQueries) {
  872.             $serialized[] = 'namedQueries';
  873.         }
  874.         if ($this->namedNativeQueries) {
  875.             $serialized[] = 'namedNativeQueries';
  876.         }
  877.         if ($this->sqlResultSetMappings) {
  878.             $serialized[] = 'sqlResultSetMappings';
  879.         }
  880.         if ($this->isReadOnly) {
  881.             $serialized[] = 'isReadOnly';
  882.         }
  883.         if ($this->customGeneratorDefinition) {
  884.             $serialized[] = 'customGeneratorDefinition';
  885.         }
  886.         if ($this->cache) {
  887.             $serialized[] = 'cache';
  888.         }
  889.         if ($this->requiresFetchAfterChange) {
  890.             $serialized[] = 'requiresFetchAfterChange';
  891.         }
  892.         return $serialized;
  893.     }
  894.     /**
  895.      * Creates a new instance of the mapped class, without invoking the constructor.
  896.      *
  897.      * @return object
  898.      */
  899.     public function newInstance()
  900.     {
  901.         return $this->instantiator->instantiate($this->name);
  902.     }
  903.     /**
  904.      * Restores some state that can not be serialized/unserialized.
  905.      *
  906.      * @param ReflectionService $reflService
  907.      *
  908.      * @return void
  909.      */
  910.     public function wakeupReflection($reflService)
  911.     {
  912.         // Restore ReflectionClass and properties
  913.         $this->reflClass    $reflService->getClass($this->name);
  914.         $this->instantiator $this->instantiator ?: new Instantiator();
  915.         $parentReflFields = [];
  916.         foreach ($this->embeddedClasses as $property => $embeddedClass) {
  917.             if (isset($embeddedClass['declaredField'])) {
  918.                 $childProperty $this->getAccessibleProperty(
  919.                     $reflService,
  920.                     $this->embeddedClasses[$embeddedClass['declaredField']]['class'],
  921.                     $embeddedClass['originalField']
  922.                 );
  923.                 assert($childProperty !== null);
  924.                 $parentReflFields[$property] = new ReflectionEmbeddedProperty(
  925.                     $parentReflFields[$embeddedClass['declaredField']],
  926.                     $childProperty,
  927.                     $this->embeddedClasses[$embeddedClass['declaredField']]['class']
  928.                 );
  929.                 continue;
  930.             }
  931.             $fieldRefl $this->getAccessibleProperty(
  932.                 $reflService,
  933.                 $embeddedClass['declared'] ?? $this->name,
  934.                 $property
  935.             );
  936.             $parentReflFields[$property] = $fieldRefl;
  937.             $this->reflFields[$property] = $fieldRefl;
  938.         }
  939.         foreach ($this->fieldMappings as $field => $mapping) {
  940.             if (isset($mapping['declaredField']) && isset($parentReflFields[$mapping['declaredField']])) {
  941.                 $childProperty $this->getAccessibleProperty($reflService$mapping['originalClass'], $mapping['originalField']);
  942.                 assert($childProperty !== null);
  943.                 if (isset($mapping['enumType'])) {
  944.                     $childProperty = new ReflectionEnumProperty(
  945.                         $childProperty,
  946.                         $mapping['enumType']
  947.                     );
  948.                 }
  949.                 $this->reflFields[$field] = new ReflectionEmbeddedProperty(
  950.                     $parentReflFields[$mapping['declaredField']],
  951.                     $childProperty,
  952.                     $mapping['originalClass']
  953.                 );
  954.                 continue;
  955.             }
  956.             $this->reflFields[$field] = isset($mapping['declared'])
  957.                 ? $this->getAccessibleProperty($reflService$mapping['declared'], $field)
  958.                 : $this->getAccessibleProperty($reflService$this->name$field);
  959.             if (isset($mapping['enumType']) && $this->reflFields[$field] !== null) {
  960.                 $this->reflFields[$field] = new ReflectionEnumProperty(
  961.                     $this->reflFields[$field],
  962.                     $mapping['enumType']
  963.                 );
  964.             }
  965.         }
  966.         foreach ($this->associationMappings as $field => $mapping) {
  967.             $this->reflFields[$field] = isset($mapping['declared'])
  968.                 ? $this->getAccessibleProperty($reflService$mapping['declared'], $field)
  969.                 : $this->getAccessibleProperty($reflService$this->name$field);
  970.         }
  971.     }
  972.     /**
  973.      * Initializes a new ClassMetadata instance that will hold the object-relational mapping
  974.      * metadata of the class with the given name.
  975.      *
  976.      * @param ReflectionService $reflService The reflection service.
  977.      *
  978.      * @return void
  979.      */
  980.     public function initializeReflection($reflService)
  981.     {
  982.         $this->reflClass $reflService->getClass($this->name);
  983.         $this->namespace $reflService->getClassNamespace($this->name);
  984.         if ($this->reflClass) {
  985.             $this->name $this->rootEntityName $this->reflClass->getName();
  986.         }
  987.         $this->table['name'] = $this->namingStrategy->classToTableName($this->name);
  988.     }
  989.     /**
  990.      * Validates Identifier.
  991.      *
  992.      * @return void
  993.      *
  994.      * @throws MappingException
  995.      */
  996.     public function validateIdentifier()
  997.     {
  998.         if ($this->isMappedSuperclass || $this->isEmbeddedClass) {
  999.             return;
  1000.         }
  1001.         // Verify & complete identifier mapping
  1002.         if (! $this->identifier) {
  1003.             throw MappingException::identifierRequired($this->name);
  1004.         }
  1005.         if ($this->usesIdGenerator() && $this->isIdentifierComposite) {
  1006.             throw MappingException::compositeKeyAssignedIdGeneratorRequired($this->name);
  1007.         }
  1008.     }
  1009.     /**
  1010.      * Validates association targets actually exist.
  1011.      *
  1012.      * @return void
  1013.      *
  1014.      * @throws MappingException
  1015.      */
  1016.     public function validateAssociations()
  1017.     {
  1018.         foreach ($this->associationMappings as $mapping) {
  1019.             if (
  1020.                 ! class_exists($mapping['targetEntity'])
  1021.                 && ! interface_exists($mapping['targetEntity'])
  1022.                 && ! trait_exists($mapping['targetEntity'])
  1023.             ) {
  1024.                 throw MappingException::invalidTargetEntityClass($mapping['targetEntity'], $this->name$mapping['fieldName']);
  1025.             }
  1026.         }
  1027.     }
  1028.     /**
  1029.      * Validates lifecycle callbacks.
  1030.      *
  1031.      * @param ReflectionService $reflService
  1032.      *
  1033.      * @return void
  1034.      *
  1035.      * @throws MappingException
  1036.      */
  1037.     public function validateLifecycleCallbacks($reflService)
  1038.     {
  1039.         foreach ($this->lifecycleCallbacks as $callbacks) {
  1040.             foreach ($callbacks as $callbackFuncName) {
  1041.                 if (! $reflService->hasPublicMethod($this->name$callbackFuncName)) {
  1042.                     throw MappingException::lifecycleCallbackMethodNotFound($this->name$callbackFuncName);
  1043.                 }
  1044.             }
  1045.         }
  1046.     }
  1047.     /**
  1048.      * {@inheritDoc}
  1049.      */
  1050.     public function getReflectionClass()
  1051.     {
  1052.         return $this->reflClass;
  1053.     }
  1054.     /**
  1055.      * @psalm-param array{usage?: mixed, region?: mixed} $cache
  1056.      *
  1057.      * @return void
  1058.      */
  1059.     public function enableCache(array $cache)
  1060.     {
  1061.         if (! isset($cache['usage'])) {
  1062.             $cache['usage'] = self::CACHE_USAGE_READ_ONLY;
  1063.         }
  1064.         if (! isset($cache['region'])) {
  1065.             $cache['region'] = strtolower(str_replace('\\''_'$this->rootEntityName));
  1066.         }
  1067.         $this->cache $cache;
  1068.     }
  1069.     /**
  1070.      * @param string $fieldName
  1071.      * @psalm-param array{usage?: int, region?: string} $cache
  1072.      *
  1073.      * @return void
  1074.      */
  1075.     public function enableAssociationCache($fieldName, array $cache)
  1076.     {
  1077.         $this->associationMappings[$fieldName]['cache'] = $this->getAssociationCacheDefaults($fieldName$cache);
  1078.     }
  1079.     /**
  1080.      * @param string $fieldName
  1081.      * @param array  $cache
  1082.      * @psalm-param array{usage?: int, region?: string|null} $cache
  1083.      *
  1084.      * @return int[]|string[]
  1085.      * @psalm-return array{usage: int, region: string|null}
  1086.      */
  1087.     public function getAssociationCacheDefaults($fieldName, array $cache)
  1088.     {
  1089.         if (! isset($cache['usage'])) {
  1090.             $cache['usage'] = $this->cache['usage'] ?? self::CACHE_USAGE_READ_ONLY;
  1091.         }
  1092.         if (! isset($cache['region'])) {
  1093.             $cache['region'] = strtolower(str_replace('\\''_'$this->rootEntityName)) . '__' $fieldName;
  1094.         }
  1095.         return $cache;
  1096.     }
  1097.     /**
  1098.      * Sets the change tracking policy used by this class.
  1099.      *
  1100.      * @param int $policy
  1101.      *
  1102.      * @return void
  1103.      */
  1104.     public function setChangeTrackingPolicy($policy)
  1105.     {
  1106.         $this->changeTrackingPolicy $policy;
  1107.     }
  1108.     /**
  1109.      * Whether the change tracking policy of this class is "deferred explicit".
  1110.      *
  1111.      * @return bool
  1112.      */
  1113.     public function isChangeTrackingDeferredExplicit()
  1114.     {
  1115.         return $this->changeTrackingPolicy === self::CHANGETRACKING_DEFERRED_EXPLICIT;
  1116.     }
  1117.     /**
  1118.      * Whether the change tracking policy of this class is "deferred implicit".
  1119.      *
  1120.      * @return bool
  1121.      */
  1122.     public function isChangeTrackingDeferredImplicit()
  1123.     {
  1124.         return $this->changeTrackingPolicy === self::CHANGETRACKING_DEFERRED_IMPLICIT;
  1125.     }
  1126.     /**
  1127.      * Whether the change tracking policy of this class is "notify".
  1128.      *
  1129.      * @return bool
  1130.      */
  1131.     public function isChangeTrackingNotify()
  1132.     {
  1133.         return $this->changeTrackingPolicy === self::CHANGETRACKING_NOTIFY;
  1134.     }
  1135.     /**
  1136.      * Checks whether a field is part of the identifier/primary key field(s).
  1137.      *
  1138.      * @param string $fieldName The field name.
  1139.      *
  1140.      * @return bool TRUE if the field is part of the table identifier/primary key field(s),
  1141.      * FALSE otherwise.
  1142.      */
  1143.     public function isIdentifier($fieldName)
  1144.     {
  1145.         if (! $this->identifier) {
  1146.             return false;
  1147.         }
  1148.         if (! $this->isIdentifierComposite) {
  1149.             return $fieldName === $this->identifier[0];
  1150.         }
  1151.         return in_array($fieldName$this->identifiertrue);
  1152.     }
  1153.     /**
  1154.      * Checks if the field is unique.
  1155.      *
  1156.      * @param string $fieldName The field name.
  1157.      *
  1158.      * @return bool TRUE if the field is unique, FALSE otherwise.
  1159.      */
  1160.     public function isUniqueField($fieldName)
  1161.     {
  1162.         $mapping $this->getFieldMapping($fieldName);
  1163.         return $mapping !== false && isset($mapping['unique']) && $mapping['unique'];
  1164.     }
  1165.     /**
  1166.      * Checks if the field is not null.
  1167.      *
  1168.      * @param string $fieldName The field name.
  1169.      *
  1170.      * @return bool TRUE if the field is not null, FALSE otherwise.
  1171.      */
  1172.     public function isNullable($fieldName)
  1173.     {
  1174.         $mapping $this->getFieldMapping($fieldName);
  1175.         return $mapping !== false && isset($mapping['nullable']) && $mapping['nullable'];
  1176.     }
  1177.     /**
  1178.      * Gets a column name for a field name.
  1179.      * If the column name for the field cannot be found, the given field name
  1180.      * is returned.
  1181.      *
  1182.      * @param string $fieldName The field name.
  1183.      *
  1184.      * @return string The column name.
  1185.      */
  1186.     public function getColumnName($fieldName)
  1187.     {
  1188.         return $this->columnNames[$fieldName] ?? $fieldName;
  1189.     }
  1190.     /**
  1191.      * Gets the mapping of a (regular) field that holds some data but not a
  1192.      * reference to another object.
  1193.      *
  1194.      * @param string $fieldName The field name.
  1195.      *
  1196.      * @return mixed[] The field mapping.
  1197.      * @psalm-return FieldMapping
  1198.      *
  1199.      * @throws MappingException
  1200.      */
  1201.     public function getFieldMapping($fieldName)
  1202.     {
  1203.         if (! isset($this->fieldMappings[$fieldName])) {
  1204.             throw MappingException::mappingNotFound($this->name$fieldName);
  1205.         }
  1206.         return $this->fieldMappings[$fieldName];
  1207.     }
  1208.     /**
  1209.      * Gets the mapping of an association.
  1210.      *
  1211.      * @see ClassMetadataInfo::$associationMappings
  1212.      *
  1213.      * @param string $fieldName The field name that represents the association in
  1214.      *                          the object model.
  1215.      *
  1216.      * @return mixed[] The mapping.
  1217.      * @psalm-return array<string, mixed>
  1218.      *
  1219.      * @throws MappingException
  1220.      */
  1221.     public function getAssociationMapping($fieldName)
  1222.     {
  1223.         if (! isset($this->associationMappings[$fieldName])) {
  1224.             throw MappingException::mappingNotFound($this->name$fieldName);
  1225.         }
  1226.         return $this->associationMappings[$fieldName];
  1227.     }
  1228.     /**
  1229.      * Gets all association mappings of the class.
  1230.      *
  1231.      * @psalm-return array<string, array<string, mixed>>
  1232.      */
  1233.     public function getAssociationMappings()
  1234.     {
  1235.         return $this->associationMappings;
  1236.     }
  1237.     /**
  1238.      * Gets the field name for a column name.
  1239.      * If no field name can be found the column name is returned.
  1240.      *
  1241.      * @param string $columnName The column name.
  1242.      *
  1243.      * @return string The column alias.
  1244.      */
  1245.     public function getFieldName($columnName)
  1246.     {
  1247.         return $this->fieldNames[$columnName] ?? $columnName;
  1248.     }
  1249.     /**
  1250.      * Gets the named query.
  1251.      *
  1252.      * @see ClassMetadataInfo::$namedQueries
  1253.      *
  1254.      * @param string $queryName The query name.
  1255.      *
  1256.      * @return string
  1257.      *
  1258.      * @throws MappingException
  1259.      */
  1260.     public function getNamedQuery($queryName)
  1261.     {
  1262.         if (! isset($this->namedQueries[$queryName])) {
  1263.             throw MappingException::queryNotFound($this->name$queryName);
  1264.         }
  1265.         return $this->namedQueries[$queryName]['dql'];
  1266.     }
  1267.     /**
  1268.      * Gets all named queries of the class.
  1269.      *
  1270.      * @return mixed[][]
  1271.      * @psalm-return array<string, array<string, mixed>>
  1272.      */
  1273.     public function getNamedQueries()
  1274.     {
  1275.         return $this->namedQueries;
  1276.     }
  1277.     /**
  1278.      * Gets the named native query.
  1279.      *
  1280.      * @see ClassMetadataInfo::$namedNativeQueries
  1281.      *
  1282.      * @param string $queryName The query name.
  1283.      *
  1284.      * @return mixed[]
  1285.      * @psalm-return array<string, mixed>
  1286.      *
  1287.      * @throws MappingException
  1288.      */
  1289.     public function getNamedNativeQuery($queryName)
  1290.     {
  1291.         if (! isset($this->namedNativeQueries[$queryName])) {
  1292.             throw MappingException::queryNotFound($this->name$queryName);
  1293.         }
  1294.         return $this->namedNativeQueries[$queryName];
  1295.     }
  1296.     /**
  1297.      * Gets all named native queries of the class.
  1298.      *
  1299.      * @psalm-return array<string, array<string, mixed>>
  1300.      */
  1301.     public function getNamedNativeQueries()
  1302.     {
  1303.         return $this->namedNativeQueries;
  1304.     }
  1305.     /**
  1306.      * Gets the result set mapping.
  1307.      *
  1308.      * @see ClassMetadataInfo::$sqlResultSetMappings
  1309.      *
  1310.      * @param string $name The result set mapping name.
  1311.      *
  1312.      * @return mixed[]
  1313.      * @psalm-return array{name: string, entities: array, columns: array}
  1314.      *
  1315.      * @throws MappingException
  1316.      */
  1317.     public function getSqlResultSetMapping($name)
  1318.     {
  1319.         if (! isset($this->sqlResultSetMappings[$name])) {
  1320.             throw MappingException::resultMappingNotFound($this->name$name);
  1321.         }
  1322.         return $this->sqlResultSetMappings[$name];
  1323.     }
  1324.     /**
  1325.      * Gets all sql result set mappings of the class.
  1326.      *
  1327.      * @return mixed[]
  1328.      * @psalm-return array<string, array{name: string, entities: array, columns: array}>
  1329.      */
  1330.     public function getSqlResultSetMappings()
  1331.     {
  1332.         return $this->sqlResultSetMappings;
  1333.     }
  1334.     /**
  1335.      * Checks whether given property has type
  1336.      *
  1337.      * @param string $name Property name
  1338.      */
  1339.     private function isTypedProperty(string $name): bool
  1340.     {
  1341.         return PHP_VERSION_ID >= 70400
  1342.                && isset($this->reflClass)
  1343.                && $this->reflClass->hasProperty($name)
  1344.                && $this->reflClass->getProperty($name)->hasType();
  1345.     }
  1346.     /**
  1347.      * Validates & completes the given field mapping based on typed property.
  1348.      *
  1349.      * @param  mixed[] $mapping The field mapping to validate & complete.
  1350.      *
  1351.      * @return mixed[] The updated mapping.
  1352.      */
  1353.     private function validateAndCompleteTypedFieldMapping(array $mapping): array
  1354.     {
  1355.         $type $this->reflClass->getProperty($mapping['fieldName'])->getType();
  1356.         if ($type) {
  1357.             if (
  1358.                 ! isset($mapping['type'])
  1359.                 && ($type instanceof ReflectionNamedType)
  1360.             ) {
  1361.                 if (PHP_VERSION_ID >= 80100 && ! $type->isBuiltin() && enum_exists($type->getName())) {
  1362.                     $mapping['enumType'] = $type->getName();
  1363.                     $reflection = new ReflectionEnum($type->getName());
  1364.                     $type       $reflection->getBackingType();
  1365.                     assert($type instanceof ReflectionNamedType);
  1366.                 }
  1367.                 switch ($type->getName()) {
  1368.                     case DateInterval::class:
  1369.                         $mapping['type'] = Types::DATEINTERVAL;
  1370.                         break;
  1371.                     case DateTime::class:
  1372.                         $mapping['type'] = Types::DATETIME_MUTABLE;
  1373.                         break;
  1374.                     case DateTimeImmutable::class:
  1375.                         $mapping['type'] = Types::DATETIME_IMMUTABLE;
  1376.                         break;
  1377.                     case 'array':
  1378.                         $mapping['type'] = Types::JSON;
  1379.                         break;
  1380.                     case 'bool':
  1381.                         $mapping['type'] = Types::BOOLEAN;
  1382.                         break;
  1383.                     case 'float':
  1384.                         $mapping['type'] = Types::FLOAT;
  1385.                         break;
  1386.                     case 'int':
  1387.                         $mapping['type'] = Types::INTEGER;
  1388.                         break;
  1389.                     case 'string':
  1390.                         $mapping['type'] = Types::STRING;
  1391.                         break;
  1392.                 }
  1393.             }
  1394.         }
  1395.         return $mapping;
  1396.     }
  1397.     /**
  1398.      * Validates & completes the basic mapping information based on typed property.
  1399.      *
  1400.      * @param mixed[] $mapping The mapping.
  1401.      *
  1402.      * @return mixed[] The updated mapping.
  1403.      */
  1404.     private function validateAndCompleteTypedAssociationMapping(array $mapping): array
  1405.     {
  1406.         $type $this->reflClass->getProperty($mapping['fieldName'])->getType();
  1407.         if ($type === null || ($mapping['type'] & self::TO_ONE) === 0) {
  1408.             return $mapping;
  1409.         }
  1410.         if (! isset($mapping['targetEntity']) && $type instanceof ReflectionNamedType) {
  1411.             $mapping['targetEntity'] = $type->getName();
  1412.         }
  1413.         return $mapping;
  1414.     }
  1415.     /**
  1416.      * Validates & completes the given field mapping.
  1417.      *
  1418.      * @psalm-param array<string, mixed> $mapping The field mapping to validate & complete.
  1419.      *
  1420.      * @return mixed[] The updated mapping.
  1421.      *
  1422.      * @throws MappingException
  1423.      */
  1424.     protected function validateAndCompleteFieldMapping(array $mapping): array
  1425.     {
  1426.         // Check mandatory fields
  1427.         if (! isset($mapping['fieldName']) || ! $mapping['fieldName']) {
  1428.             throw MappingException::missingFieldName($this->name);
  1429.         }
  1430.         if ($this->isTypedProperty($mapping['fieldName'])) {
  1431.             $mapping $this->validateAndCompleteTypedFieldMapping($mapping);
  1432.         }
  1433.         if (! isset($mapping['type'])) {
  1434.             // Default to string
  1435.             $mapping['type'] = 'string';
  1436.         }
  1437.         // Complete fieldName and columnName mapping
  1438.         if (! isset($mapping['columnName'])) {
  1439.             $mapping['columnName'] = $this->namingStrategy->propertyToColumnName($mapping['fieldName'], $this->name);
  1440.         }
  1441.         if ($mapping['columnName'][0] === '`') {
  1442.             $mapping['columnName'] = trim($mapping['columnName'], '`');
  1443.             $mapping['quoted']     = true;
  1444.         }
  1445.         $this->columnNames[$mapping['fieldName']] = $mapping['columnName'];
  1446.         if (isset($this->fieldNames[$mapping['columnName']]) || ($this->discriminatorColumn && $this->discriminatorColumn['name'] === $mapping['columnName'])) {
  1447.             throw MappingException::duplicateColumnName($this->name$mapping['columnName']);
  1448.         }
  1449.         $this->fieldNames[$mapping['columnName']] = $mapping['fieldName'];
  1450.         // Complete id mapping
  1451.         if (isset($mapping['id']) && $mapping['id'] === true) {
  1452.             if ($this->versionField === $mapping['fieldName']) {
  1453.                 throw MappingException::cannotVersionIdField($this->name$mapping['fieldName']);
  1454.             }
  1455.             if (! in_array($mapping['fieldName'], $this->identifiertrue)) {
  1456.                 $this->identifier[] = $mapping['fieldName'];
  1457.             }
  1458.             // Check for composite key
  1459.             if (! $this->isIdentifierComposite && count($this->identifier) > 1) {
  1460.                 $this->isIdentifierComposite true;
  1461.             }
  1462.         }
  1463.         if (Type::hasType($mapping['type']) && Type::getType($mapping['type'])->canRequireSQLConversion()) {
  1464.             if (isset($mapping['id']) && $mapping['id'] === true) {
  1465.                  throw MappingException::sqlConversionNotAllowedForIdentifiers($this->name$mapping['fieldName'], $mapping['type']);
  1466.             }
  1467.             $mapping['requireSQLConversion'] = true;
  1468.         }
  1469.         if (isset($mapping['generated'])) {
  1470.             if (! in_array($mapping['generated'], [self::GENERATED_NEVERself::GENERATED_INSERTself::GENERATED_ALWAYS])) {
  1471.                 throw MappingException::invalidGeneratedMode($mapping['generated']);
  1472.             }
  1473.             if ($mapping['generated'] === self::GENERATED_NEVER) {
  1474.                 unset($mapping['generated']);
  1475.             }
  1476.         }
  1477.         if (isset($mapping['enumType'])) {
  1478.             if (PHP_VERSION_ID 80100) {
  1479.                 throw MappingException::enumsRequirePhp81($this->name$mapping['fieldName']);
  1480.             }
  1481.             if (! enum_exists($mapping['enumType'])) {
  1482.                 throw MappingException::nonEnumTypeMapped($this->name$mapping['fieldName'], $mapping['enumType']);
  1483.             }
  1484.         }
  1485.         return $mapping;
  1486.     }
  1487.     /**
  1488.      * Validates & completes the basic mapping information that is common to all
  1489.      * association mappings (one-to-one, many-ot-one, one-to-many, many-to-many).
  1490.      *
  1491.      * @psalm-param array<string, mixed> $mapping The mapping.
  1492.      *
  1493.      * @return mixed[] The updated mapping.
  1494.      * @psalm-return array{
  1495.      *                   mappedBy: mixed|null,
  1496.      *                   inversedBy: mixed|null,
  1497.      *                   isOwningSide: bool,
  1498.      *                   sourceEntity: class-string,
  1499.      *                   targetEntity: string,
  1500.      *                   fieldName: mixed,
  1501.      *                   fetch: mixed,
  1502.      *                   cascade: array<array-key,string>,
  1503.      *                   isCascadeRemove: bool,
  1504.      *                   isCascadePersist: bool,
  1505.      *                   isCascadeRefresh: bool,
  1506.      *                   isCascadeMerge: bool,
  1507.      *                   isCascadeDetach: bool,
  1508.      *                   type: int,
  1509.      *                   originalField: string,
  1510.      *                   originalClass: class-string,
  1511.      *                   ?orphanRemoval: bool
  1512.      *               }
  1513.      *
  1514.      * @throws MappingException If something is wrong with the mapping.
  1515.      */
  1516.     protected function _validateAndCompleteAssociationMapping(array $mapping)
  1517.     {
  1518.         if (! isset($mapping['mappedBy'])) {
  1519.             $mapping['mappedBy'] = null;
  1520.         }
  1521.         if (! isset($mapping['inversedBy'])) {
  1522.             $mapping['inversedBy'] = null;
  1523.         }
  1524.         $mapping['isOwningSide'] = true// assume owning side until we hit mappedBy
  1525.         if (empty($mapping['indexBy'])) {
  1526.             unset($mapping['indexBy']);
  1527.         }
  1528.         // If targetEntity is unqualified, assume it is in the same namespace as
  1529.         // the sourceEntity.
  1530.         $mapping['sourceEntity'] = $this->name;
  1531.         if ($this->isTypedProperty($mapping['fieldName'])) {
  1532.             $mapping $this->validateAndCompleteTypedAssociationMapping($mapping);
  1533.         }
  1534.         if (isset($mapping['targetEntity'])) {
  1535.             $mapping['targetEntity'] = $this->fullyQualifiedClassName($mapping['targetEntity']);
  1536.             $mapping['targetEntity'] = ltrim($mapping['targetEntity'], '\\');
  1537.         }
  1538.         if (($mapping['type'] & self::MANY_TO_ONE) > && isset($mapping['orphanRemoval']) && $mapping['orphanRemoval']) {
  1539.             throw MappingException::illegalOrphanRemoval($this->name$mapping['fieldName']);
  1540.         }
  1541.         // Complete id mapping
  1542.         if (isset($mapping['id']) && $mapping['id'] === true) {
  1543.             if (isset($mapping['orphanRemoval']) && $mapping['orphanRemoval']) {
  1544.                 throw MappingException::illegalOrphanRemovalOnIdentifierAssociation($this->name$mapping['fieldName']);
  1545.             }
  1546.             if (! in_array($mapping['fieldName'], $this->identifiertrue)) {
  1547.                 if (isset($mapping['joinColumns']) && count($mapping['joinColumns']) >= 2) {
  1548.                     throw MappingException::cannotMapCompositePrimaryKeyEntitiesAsForeignId(
  1549.                         $mapping['targetEntity'],
  1550.                         $this->name,
  1551.                         $mapping['fieldName']
  1552.                     );
  1553.                 }
  1554.                 $this->identifier[]              = $mapping['fieldName'];
  1555.                 $this->containsForeignIdentifier true;
  1556.             }
  1557.             // Check for composite key
  1558.             if (! $this->isIdentifierComposite && count($this->identifier) > 1) {
  1559.                 $this->isIdentifierComposite true;
  1560.             }
  1561.             if ($this->cache && ! isset($mapping['cache'])) {
  1562.                 throw NonCacheableEntityAssociation::fromEntityAndField(
  1563.                     $this->name,
  1564.                     $mapping['fieldName']
  1565.                 );
  1566.             }
  1567.         }
  1568.         // Mandatory attributes for both sides
  1569.         // Mandatory: fieldName, targetEntity
  1570.         if (! isset($mapping['fieldName']) || ! $mapping['fieldName']) {
  1571.             throw MappingException::missingFieldName($this->name);
  1572.         }
  1573.         if (! isset($mapping['targetEntity'])) {
  1574.             throw MappingException::missingTargetEntity($mapping['fieldName']);
  1575.         }
  1576.         // Mandatory and optional attributes for either side
  1577.         if (! $mapping['mappedBy']) {
  1578.             if (isset($mapping['joinTable']) && $mapping['joinTable']) {
  1579.                 if (isset($mapping['joinTable']['name']) && $mapping['joinTable']['name'][0] === '`') {
  1580.                     $mapping['joinTable']['name']   = trim($mapping['joinTable']['name'], '`');
  1581.                     $mapping['joinTable']['quoted'] = true;
  1582.                 }
  1583.             }
  1584.         } else {
  1585.             $mapping['isOwningSide'] = false;
  1586.         }
  1587.         if (isset($mapping['id']) && $mapping['id'] === true && $mapping['type'] & self::TO_MANY) {
  1588.             throw MappingException::illegalToManyIdentifierAssociation($this->name$mapping['fieldName']);
  1589.         }
  1590.         // Fetch mode. Default fetch mode to LAZY, if not set.
  1591.         if (! isset($mapping['fetch'])) {
  1592.             $mapping['fetch'] = self::FETCH_LAZY;
  1593.         }
  1594.         // Cascades
  1595.         $cascades = isset($mapping['cascade']) ? array_map('strtolower'$mapping['cascade']) : [];
  1596.         $allCascades = ['remove''persist''refresh''merge''detach'];
  1597.         if (in_array('all'$cascadestrue)) {
  1598.             $cascades $allCascades;
  1599.         } elseif (count($cascades) !== count(array_intersect($cascades$allCascades))) {
  1600.             throw MappingException::invalidCascadeOption(
  1601.                 array_diff($cascades$allCascades),
  1602.                 $this->name,
  1603.                 $mapping['fieldName']
  1604.             );
  1605.         }
  1606.         $mapping['cascade']          = $cascades;
  1607.         $mapping['isCascadeRemove']  = in_array('remove'$cascadestrue);
  1608.         $mapping['isCascadePersist'] = in_array('persist'$cascadestrue);
  1609.         $mapping['isCascadeRefresh'] = in_array('refresh'$cascadestrue);
  1610.         $mapping['isCascadeMerge']   = in_array('merge'$cascadestrue);
  1611.         $mapping['isCascadeDetach']  = in_array('detach'$cascadestrue);
  1612.         return $mapping;
  1613.     }
  1614.     /**
  1615.      * Validates & completes a one-to-one association mapping.
  1616.      *
  1617.      * @psalm-param array<string, mixed> $mapping The mapping to validate & complete.
  1618.      * @psalm-param array<string, mixed> $mapping The mapping to validate & complete.
  1619.      *
  1620.      * @return mixed[] The validated & completed mapping.
  1621.      * @psalm-return array{isOwningSide: mixed, orphanRemoval: bool, isCascadeRemove: bool}
  1622.      * @psalm-return array{
  1623.      *      mappedBy: mixed|null,
  1624.      *      inversedBy: mixed|null,
  1625.      *      isOwningSide: bool,
  1626.      *      sourceEntity: class-string,
  1627.      *      targetEntity: string,
  1628.      *      fieldName: mixed,
  1629.      *      fetch: mixed,
  1630.      *      cascade: array<string>,
  1631.      *      isCascadeRemove: bool,
  1632.      *      isCascadePersist: bool,
  1633.      *      isCascadeRefresh: bool,
  1634.      *      isCascadeMerge: bool,
  1635.      *      isCascadeDetach: bool,
  1636.      *      type: int,
  1637.      *      originalField: string,
  1638.      *      originalClass: class-string,
  1639.      *      joinColumns?: array{0: array{name: string, referencedColumnName: string}}|mixed,
  1640.      *      id?: mixed,
  1641.      *      sourceToTargetKeyColumns?: array,
  1642.      *      joinColumnFieldNames?: array,
  1643.      *      targetToSourceKeyColumns?: array<array-key>,
  1644.      *      orphanRemoval: bool
  1645.      * }
  1646.      *
  1647.      * @throws RuntimeException
  1648.      * @throws MappingException
  1649.      */
  1650.     protected function _validateAndCompleteOneToOneMapping(array $mapping)
  1651.     {
  1652.         $mapping $this->_validateAndCompleteAssociationMapping($mapping);
  1653.         if (isset($mapping['joinColumns']) && $mapping['joinColumns']) {
  1654.             $mapping['isOwningSide'] = true;
  1655.         }
  1656.         if ($mapping['isOwningSide']) {
  1657.             if (empty($mapping['joinColumns'])) {
  1658.                 // Apply default join column
  1659.                 $mapping['joinColumns'] = [
  1660.                     [
  1661.                         'name' => $this->namingStrategy->joinColumnName($mapping['fieldName'], $this->name),
  1662.                         'referencedColumnName' => $this->namingStrategy->referenceColumnName(),
  1663.                     ],
  1664.                 ];
  1665.             }
  1666.             $uniqueConstraintColumns = [];
  1667.             foreach ($mapping['joinColumns'] as &$joinColumn) {
  1668.                 if ($mapping['type'] === self::ONE_TO_ONE && ! $this->isInheritanceTypeSingleTable()) {
  1669.                     if (count($mapping['joinColumns']) === 1) {
  1670.                         if (empty($mapping['id'])) {
  1671.                             $joinColumn['unique'] = true;
  1672.                         }
  1673.                     } else {
  1674.                         $uniqueConstraintColumns[] = $joinColumn['name'];
  1675.                     }
  1676.                 }
  1677.                 if (empty($joinColumn['name'])) {
  1678.                     $joinColumn['name'] = $this->namingStrategy->joinColumnName($mapping['fieldName'], $this->name);
  1679.                 }
  1680.                 if (empty($joinColumn['referencedColumnName'])) {
  1681.                     $joinColumn['referencedColumnName'] = $this->namingStrategy->referenceColumnName();
  1682.                 }
  1683.                 if ($joinColumn['name'][0] === '`') {
  1684.                     $joinColumn['name']   = trim($joinColumn['name'], '`');
  1685.                     $joinColumn['quoted'] = true;
  1686.                 }
  1687.                 if ($joinColumn['referencedColumnName'][0] === '`') {
  1688.                     $joinColumn['referencedColumnName'] = trim($joinColumn['referencedColumnName'], '`');
  1689.                     $joinColumn['quoted']               = true;
  1690.                 }
  1691.                 $mapping['sourceToTargetKeyColumns'][$joinColumn['name']] = $joinColumn['referencedColumnName'];
  1692.                 $mapping['joinColumnFieldNames'][$joinColumn['name']]     = $joinColumn['fieldName'] ?? $joinColumn['name'];
  1693.             }
  1694.             if ($uniqueConstraintColumns) {
  1695.                 if (! $this->table) {
  1696.                     throw new RuntimeException('ClassMetadataInfo::setTable() has to be called before defining a one to one relationship.');
  1697.                 }
  1698.                 $this->table['uniqueConstraints'][$mapping['fieldName'] . '_uniq'] = ['columns' => $uniqueConstraintColumns];
  1699.             }
  1700.             $mapping['targetToSourceKeyColumns'] = array_flip($mapping['sourceToTargetKeyColumns']);
  1701.         }
  1702.         $mapping['orphanRemoval']   = isset($mapping['orphanRemoval']) && $mapping['orphanRemoval'];
  1703.         $mapping['isCascadeRemove'] = $mapping['orphanRemoval'] || $mapping['isCascadeRemove'];
  1704.         if ($mapping['orphanRemoval']) {
  1705.             unset($mapping['unique']);
  1706.         }
  1707.         if (isset($mapping['id']) && $mapping['id'] === true && ! $mapping['isOwningSide']) {
  1708.             throw MappingException::illegalInverseIdentifierAssociation($this->name$mapping['fieldName']);
  1709.         }
  1710.         return $mapping;
  1711.     }
  1712.     /**
  1713.      * Validates & completes a one-to-many association mapping.
  1714.      *
  1715.      * @psalm-param array<string, mixed> $mapping The mapping to validate and complete.
  1716.      *
  1717.      * @return mixed[] The validated and completed mapping.
  1718.      * @psalm-return array{
  1719.      *                   mappedBy: mixed,
  1720.      *                   inversedBy: mixed,
  1721.      *                   isOwningSide: bool,
  1722.      *                   sourceEntity: string,
  1723.      *                   targetEntity: string,
  1724.      *                   fieldName: mixed,
  1725.      *                   fetch: int|mixed,
  1726.      *                   cascade: array<array-key,string>,
  1727.      *                   isCascadeRemove: bool,
  1728.      *                   isCascadePersist: bool,
  1729.      *                   isCascadeRefresh: bool,
  1730.      *                   isCascadeMerge: bool,
  1731.      *                   isCascadeDetach: bool,
  1732.      *                   orphanRemoval: bool
  1733.      *               }
  1734.      *
  1735.      * @throws MappingException
  1736.      * @throws InvalidArgumentException
  1737.      */
  1738.     protected function _validateAndCompleteOneToManyMapping(array $mapping)
  1739.     {
  1740.         $mapping $this->_validateAndCompleteAssociationMapping($mapping);
  1741.         // OneToMany-side MUST be inverse (must have mappedBy)
  1742.         if (! isset($mapping['mappedBy'])) {
  1743.             throw MappingException::oneToManyRequiresMappedBy($this->name$mapping['fieldName']);
  1744.         }
  1745.         $mapping['orphanRemoval']   = isset($mapping['orphanRemoval']) && $mapping['orphanRemoval'];
  1746.         $mapping['isCascadeRemove'] = $mapping['orphanRemoval'] || $mapping['isCascadeRemove'];
  1747.         $this->assertMappingOrderBy($mapping);
  1748.         return $mapping;
  1749.     }
  1750.     /**
  1751.      * Validates & completes a many-to-many association mapping.
  1752.      *
  1753.      * @psalm-param array<string, mixed> $mapping The mapping to validate & complete.
  1754.      * @psalm-param array<string, mixed> $mapping The mapping to validate & complete.
  1755.      *
  1756.      * @return mixed[] The validated & completed mapping.
  1757.      * @psalm-return array{
  1758.      *      mappedBy: mixed,
  1759.      *      inversedBy: mixed,
  1760.      *      isOwningSide: bool,
  1761.      *      sourceEntity: class-string,
  1762.      *      targetEntity: string,
  1763.      *      fieldName: mixed,
  1764.      *      fetch: mixed,
  1765.      *      cascade: array<string>,
  1766.      *      isCascadeRemove: bool,
  1767.      *      isCascadePersist: bool,
  1768.      *      isCascadeRefresh: bool,
  1769.      *      isCascadeMerge: bool,
  1770.      *      isCascadeDetach: bool,
  1771.      *      type: int,
  1772.      *      originalField: string,
  1773.      *      originalClass: class-string,
  1774.      *      joinTable?: array{inverseJoinColumns: mixed}|mixed,
  1775.      *      joinTableColumns?: list<mixed>,
  1776.      *      isOnDeleteCascade?: true,
  1777.      *      relationToSourceKeyColumns?: array,
  1778.      *      relationToTargetKeyColumns?: array,
  1779.      *      orphanRemoval: bool
  1780.      * }
  1781.      *
  1782.      * @throws InvalidArgumentException
  1783.      */
  1784.     protected function _validateAndCompleteManyToManyMapping(array $mapping)
  1785.     {
  1786.         $mapping $this->_validateAndCompleteAssociationMapping($mapping);
  1787.         if ($mapping['isOwningSide']) {
  1788.             // owning side MUST have a join table
  1789.             if (! isset($mapping['joinTable']['name'])) {
  1790.                 $mapping['joinTable']['name'] = $this->namingStrategy->joinTableName($mapping['sourceEntity'], $mapping['targetEntity'], $mapping['fieldName']);
  1791.             }
  1792.             $selfReferencingEntityWithoutJoinColumns $mapping['sourceEntity'] === $mapping['targetEntity']
  1793.                 && (! (isset($mapping['joinTable']['joinColumns']) || isset($mapping['joinTable']['inverseJoinColumns'])));
  1794.             if (! isset($mapping['joinTable']['joinColumns'])) {
  1795.                 $mapping['joinTable']['joinColumns'] = [
  1796.                     [
  1797.                         'name' => $this->namingStrategy->joinKeyColumnName($mapping['sourceEntity'], $selfReferencingEntityWithoutJoinColumns 'source' null),
  1798.                         'referencedColumnName' => $this->namingStrategy->referenceColumnName(),
  1799.                         'onDelete' => 'CASCADE',
  1800.                     ],
  1801.                 ];
  1802.             }
  1803.             if (! isset($mapping['joinTable']['inverseJoinColumns'])) {
  1804.                 $mapping['joinTable']['inverseJoinColumns'] = [
  1805.                     [
  1806.                         'name' => $this->namingStrategy->joinKeyColumnName($mapping['targetEntity'], $selfReferencingEntityWithoutJoinColumns 'target' null),
  1807.                         'referencedColumnName' => $this->namingStrategy->referenceColumnName(),
  1808.                         'onDelete' => 'CASCADE',
  1809.                     ],
  1810.                 ];
  1811.             }
  1812.             $mapping['joinTableColumns'] = [];
  1813.             foreach ($mapping['joinTable']['joinColumns'] as &$joinColumn) {
  1814.                 if (empty($joinColumn['name'])) {
  1815.                     $joinColumn['name'] = $this->namingStrategy->joinKeyColumnName($mapping['sourceEntity'], $joinColumn['referencedColumnName']);
  1816.                 }
  1817.                 if (empty($joinColumn['referencedColumnName'])) {
  1818.                     $joinColumn['referencedColumnName'] = $this->namingStrategy->referenceColumnName();
  1819.                 }
  1820.                 if ($joinColumn['name'][0] === '`') {
  1821.                     $joinColumn['name']   = trim($joinColumn['name'], '`');
  1822.                     $joinColumn['quoted'] = true;
  1823.                 }
  1824.                 if ($joinColumn['referencedColumnName'][0] === '`') {
  1825.                     $joinColumn['referencedColumnName'] = trim($joinColumn['referencedColumnName'], '`');
  1826.                     $joinColumn['quoted']               = true;
  1827.                 }
  1828.                 if (isset($joinColumn['onDelete']) && strtolower($joinColumn['onDelete']) === 'cascade') {
  1829.                     $mapping['isOnDeleteCascade'] = true;
  1830.                 }
  1831.                 $mapping['relationToSourceKeyColumns'][$joinColumn['name']] = $joinColumn['referencedColumnName'];
  1832.                 $mapping['joinTableColumns'][]                              = $joinColumn['name'];
  1833.             }
  1834.             foreach ($mapping['joinTable']['inverseJoinColumns'] as &$inverseJoinColumn) {
  1835.                 if (empty($inverseJoinColumn['name'])) {
  1836.                     $inverseJoinColumn['name'] = $this->namingStrategy->joinKeyColumnName($mapping['targetEntity'], $inverseJoinColumn['referencedColumnName']);
  1837.                 }
  1838.                 if (empty($inverseJoinColumn['referencedColumnName'])) {
  1839.                     $inverseJoinColumn['referencedColumnName'] = $this->namingStrategy->referenceColumnName();
  1840.                 }
  1841.                 if ($inverseJoinColumn['name'][0] === '`') {
  1842.                     $inverseJoinColumn['name']   = trim($inverseJoinColumn['name'], '`');
  1843.                     $inverseJoinColumn['quoted'] = true;
  1844.                 }
  1845.                 if ($inverseJoinColumn['referencedColumnName'][0] === '`') {
  1846.                     $inverseJoinColumn['referencedColumnName'] = trim($inverseJoinColumn['referencedColumnName'], '`');
  1847.                     $inverseJoinColumn['quoted']               = true;
  1848.                 }
  1849.                 if (isset($inverseJoinColumn['onDelete']) && strtolower($inverseJoinColumn['onDelete']) === 'cascade') {
  1850.                     $mapping['isOnDeleteCascade'] = true;
  1851.                 }
  1852.                 $mapping['relationToTargetKeyColumns'][$inverseJoinColumn['name']] = $inverseJoinColumn['referencedColumnName'];
  1853.                 $mapping['joinTableColumns'][]                                     = $inverseJoinColumn['name'];
  1854.             }
  1855.         }
  1856.         $mapping['orphanRemoval'] = isset($mapping['orphanRemoval']) && $mapping['orphanRemoval'];
  1857.         $this->assertMappingOrderBy($mapping);
  1858.         return $mapping;
  1859.     }
  1860.     /**
  1861.      * {@inheritDoc}
  1862.      */
  1863.     public function getIdentifierFieldNames()
  1864.     {
  1865.         return $this->identifier;
  1866.     }
  1867.     /**
  1868.      * Gets the name of the single id field. Note that this only works on
  1869.      * entity classes that have a single-field pk.
  1870.      *
  1871.      * @return string
  1872.      *
  1873.      * @throws MappingException If the class doesn't have an identifier or it has a composite primary key.
  1874.      */
  1875.     public function getSingleIdentifierFieldName()
  1876.     {
  1877.         if ($this->isIdentifierComposite) {
  1878.             throw MappingException::singleIdNotAllowedOnCompositePrimaryKey($this->name);
  1879.         }
  1880.         if (! isset($this->identifier[0])) {
  1881.             throw MappingException::noIdDefined($this->name);
  1882.         }
  1883.         return $this->identifier[0];
  1884.     }
  1885.     /**
  1886.      * Gets the column name of the single id column. Note that this only works on
  1887.      * entity classes that have a single-field pk.
  1888.      *
  1889.      * @return string
  1890.      *
  1891.      * @throws MappingException If the class doesn't have an identifier or it has a composite primary key.
  1892.      */
  1893.     public function getSingleIdentifierColumnName()
  1894.     {
  1895.         return $this->getColumnName($this->getSingleIdentifierFieldName());
  1896.     }
  1897.     /**
  1898.      * INTERNAL:
  1899.      * Sets the mapped identifier/primary key fields of this class.
  1900.      * Mainly used by the ClassMetadataFactory to assign inherited identifiers.
  1901.      *
  1902.      * @psalm-param list<mixed> $identifier
  1903.      *
  1904.      * @return void
  1905.      */
  1906.     public function setIdentifier(array $identifier)
  1907.     {
  1908.         $this->identifier            $identifier;
  1909.         $this->isIdentifierComposite = (count($this->identifier) > 1);
  1910.     }
  1911.     /**
  1912.      * {@inheritDoc}
  1913.      */
  1914.     public function getIdentifier()
  1915.     {
  1916.         return $this->identifier;
  1917.     }
  1918.     /**
  1919.      * {@inheritDoc}
  1920.      */
  1921.     public function hasField($fieldName)
  1922.     {
  1923.         return isset($this->fieldMappings[$fieldName]) || isset($this->embeddedClasses[$fieldName]);
  1924.     }
  1925.     /**
  1926.      * Gets an array containing all the column names.
  1927.      *
  1928.      * @psalm-param list<string>|null $fieldNames
  1929.      *
  1930.      * @return mixed[]
  1931.      * @psalm-return list<string>
  1932.      */
  1933.     public function getColumnNames(?array $fieldNames null)
  1934.     {
  1935.         if ($fieldNames === null) {
  1936.             return array_keys($this->fieldNames);
  1937.         }
  1938.         return array_values(array_map([$this'getColumnName'], $fieldNames));
  1939.     }
  1940.     /**
  1941.      * Returns an array with all the identifier column names.
  1942.      *
  1943.      * @psalm-return list<string>
  1944.      */
  1945.     public function getIdentifierColumnNames()
  1946.     {
  1947.         $columnNames = [];
  1948.         foreach ($this->identifier as $idProperty) {
  1949.             if (isset($this->fieldMappings[$idProperty])) {
  1950.                 $columnNames[] = $this->fieldMappings[$idProperty]['columnName'];
  1951.                 continue;
  1952.             }
  1953.             // Association defined as Id field
  1954.             $joinColumns      $this->associationMappings[$idProperty]['joinColumns'];
  1955.             $assocColumnNames array_map(static function ($joinColumn) {
  1956.                 return $joinColumn['name'];
  1957.             }, $joinColumns);
  1958.             $columnNames array_merge($columnNames$assocColumnNames);
  1959.         }
  1960.         return $columnNames;
  1961.     }
  1962.     /**
  1963.      * Sets the type of Id generator to use for the mapped class.
  1964.      *
  1965.      * @param int $generatorType
  1966.      * @psalm-param self::GENERATOR_TYPE_* $generatorType
  1967.      *
  1968.      * @return void
  1969.      */
  1970.     public function setIdGeneratorType($generatorType)
  1971.     {
  1972.         $this->generatorType $generatorType;
  1973.     }
  1974.     /**
  1975.      * Checks whether the mapped class uses an Id generator.
  1976.      *
  1977.      * @return bool TRUE if the mapped class uses an Id generator, FALSE otherwise.
  1978.      */
  1979.     public function usesIdGenerator()
  1980.     {
  1981.         return $this->generatorType !== self::GENERATOR_TYPE_NONE;
  1982.     }
  1983.     /**
  1984.      * @return bool
  1985.      */
  1986.     public function isInheritanceTypeNone()
  1987.     {
  1988.         return $this->inheritanceType === self::INHERITANCE_TYPE_NONE;
  1989.     }
  1990.     /**
  1991.      * Checks whether the mapped class uses the JOINED inheritance mapping strategy.
  1992.      *
  1993.      * @return bool TRUE if the class participates in a JOINED inheritance mapping,
  1994.      * FALSE otherwise.
  1995.      */
  1996.     public function isInheritanceTypeJoined()
  1997.     {
  1998.         return $this->inheritanceType === self::INHERITANCE_TYPE_JOINED;
  1999.     }
  2000.     /**
  2001.      * Checks whether the mapped class uses the SINGLE_TABLE inheritance mapping strategy.
  2002.      *
  2003.      * @return bool TRUE if the class participates in a SINGLE_TABLE inheritance mapping,
  2004.      * FALSE otherwise.
  2005.      */
  2006.     public function isInheritanceTypeSingleTable()
  2007.     {
  2008.         return $this->inheritanceType === self::INHERITANCE_TYPE_SINGLE_TABLE;
  2009.     }
  2010.     /**
  2011.      * Checks whether the mapped class uses the TABLE_PER_CLASS inheritance mapping strategy.
  2012.      *
  2013.      * @return bool TRUE if the class participates in a TABLE_PER_CLASS inheritance mapping,
  2014.      * FALSE otherwise.
  2015.      */
  2016.     public function isInheritanceTypeTablePerClass()
  2017.     {
  2018.         return $this->inheritanceType === self::INHERITANCE_TYPE_TABLE_PER_CLASS;
  2019.     }
  2020.     /**
  2021.      * Checks whether the class uses an identity column for the Id generation.
  2022.      *
  2023.      * @return bool TRUE if the class uses the IDENTITY generator, FALSE otherwise.
  2024.      */
  2025.     public function isIdGeneratorIdentity()
  2026.     {
  2027.         return $this->generatorType === self::GENERATOR_TYPE_IDENTITY;
  2028.     }
  2029.     /**
  2030.      * Checks whether the class uses a sequence for id generation.
  2031.      *
  2032.      * @return bool TRUE if the class uses the SEQUENCE generator, FALSE otherwise.
  2033.      */
  2034.     public function isIdGeneratorSequence()
  2035.     {
  2036.         return $this->generatorType === self::GENERATOR_TYPE_SEQUENCE;
  2037.     }
  2038.     /**
  2039.      * Checks whether the class uses a table for id generation.
  2040.      *
  2041.      * @deprecated
  2042.      *
  2043.      * @return false
  2044.      */
  2045.     public function isIdGeneratorTable()
  2046.     {
  2047.         Deprecation::trigger(
  2048.             'doctrine/orm',
  2049.             'https://github.com/doctrine/orm/pull/9046',
  2050.             '%s is deprecated',
  2051.             __METHOD__
  2052.         );
  2053.         return false;
  2054.     }
  2055.     /**
  2056.      * Checks whether the class has a natural identifier/pk (which means it does
  2057.      * not use any Id generator.
  2058.      *
  2059.      * @return bool
  2060.      */
  2061.     public function isIdentifierNatural()
  2062.     {
  2063.         return $this->generatorType === self::GENERATOR_TYPE_NONE;
  2064.     }
  2065.     /**
  2066.      * Checks whether the class use a UUID for id generation.
  2067.      *
  2068.      * @deprecated
  2069.      *
  2070.      * @return bool
  2071.      */
  2072.     public function isIdentifierUuid()
  2073.     {
  2074.         Deprecation::trigger(
  2075.             'doctrine/orm',
  2076.             'https://github.com/doctrine/orm/pull/9046',
  2077.             '%s is deprecated',
  2078.             __METHOD__
  2079.         );
  2080.         return $this->generatorType === self::GENERATOR_TYPE_UUID;
  2081.     }
  2082.     /**
  2083.      * Gets the type of a field.
  2084.      *
  2085.      * @param string $fieldName
  2086.      *
  2087.      * @return string|null
  2088.      *
  2089.      * @todo 3.0 Remove this. PersisterHelper should fix it somehow
  2090.      */
  2091.     public function getTypeOfField($fieldName)
  2092.     {
  2093.         return isset($this->fieldMappings[$fieldName])
  2094.             ? $this->fieldMappings[$fieldName]['type']
  2095.             : null;
  2096.     }
  2097.     /**
  2098.      * Gets the type of a column.
  2099.      *
  2100.      * @deprecated 3.0 remove this. this method is bogus and unreliable, since it cannot resolve the type of a column
  2101.      *             that is derived by a referenced field on a different entity.
  2102.      *
  2103.      * @param string $columnName
  2104.      *
  2105.      * @return string|null
  2106.      */
  2107.     public function getTypeOfColumn($columnName)
  2108.     {
  2109.         return $this->getTypeOfField($this->getFieldName($columnName));
  2110.     }
  2111.     /**
  2112.      * Gets the name of the primary table.
  2113.      *
  2114.      * @return string
  2115.      */
  2116.     public function getTableName()
  2117.     {
  2118.         return $this->table['name'];
  2119.     }
  2120.     /**
  2121.      * Gets primary table's schema name.
  2122.      *
  2123.      * @return string|null
  2124.      */
  2125.     public function getSchemaName()
  2126.     {
  2127.         return $this->table['schema'] ?? null;
  2128.     }
  2129.     /**
  2130.      * Gets the table name to use for temporary identifier tables of this class.
  2131.      *
  2132.      * @return string
  2133.      */
  2134.     public function getTemporaryIdTableName()
  2135.     {
  2136.         // replace dots with underscores because PostgreSQL creates temporary tables in a special schema
  2137.         return str_replace('.''_'$this->getTableName() . '_id_tmp');
  2138.     }
  2139.     /**
  2140.      * Sets the mapped subclasses of this class.
  2141.      *
  2142.      * @psalm-param list<string> $subclasses The names of all mapped subclasses.
  2143.      *
  2144.      * @return void
  2145.      */
  2146.     public function setSubclasses(array $subclasses)
  2147.     {
  2148.         foreach ($subclasses as $subclass) {
  2149.             $this->subClasses[] = $this->fullyQualifiedClassName($subclass);
  2150.         }
  2151.     }
  2152.     /**
  2153.      * Sets the parent class names.
  2154.      * Assumes that the class names in the passed array are in the order:
  2155.      * directParent -> directParentParent -> directParentParentParent ... -> root.
  2156.      *
  2157.      * @psalm-param list<class-string> $classNames
  2158.      *
  2159.      * @return void
  2160.      */
  2161.     public function setParentClasses(array $classNames)
  2162.     {
  2163.         $this->parentClasses $classNames;
  2164.         if (count($classNames) > 0) {
  2165.             $this->rootEntityName array_pop($classNames);
  2166.         }
  2167.     }
  2168.     /**
  2169.      * Sets the inheritance type used by the class and its subclasses.
  2170.      *
  2171.      * @param int $type
  2172.      * @psalm-param self::INHERITANCE_TYPE_* $type
  2173.      *
  2174.      * @return void
  2175.      *
  2176.      * @throws MappingException
  2177.      */
  2178.     public function setInheritanceType($type)
  2179.     {
  2180.         if (! $this->isInheritanceType($type)) {
  2181.             throw MappingException::invalidInheritanceType($this->name$type);
  2182.         }
  2183.         $this->inheritanceType $type;
  2184.     }
  2185.     /**
  2186.      * Sets the association to override association mapping of property for an entity relationship.
  2187.      *
  2188.      * @param string $fieldName
  2189.      * @psalm-param array<string, mixed> $overrideMapping
  2190.      *
  2191.      * @return void
  2192.      *
  2193.      * @throws MappingException
  2194.      */
  2195.     public function setAssociationOverride($fieldName, array $overrideMapping)
  2196.     {
  2197.         if (! isset($this->associationMappings[$fieldName])) {
  2198.             throw MappingException::invalidOverrideFieldName($this->name$fieldName);
  2199.         }
  2200.         $mapping $this->associationMappings[$fieldName];
  2201.         //if (isset($mapping['inherited']) && (count($overrideMapping) !== 1 || ! isset($overrideMapping['fetch']))) {
  2202.             // TODO: Deprecate overriding the fetch mode via association override for 3.0,
  2203.             // users should do this with a listener and a custom attribute/annotation
  2204.             // TODO: Enable this exception in 2.8
  2205.             //throw MappingException::illegalOverrideOfInheritedProperty($this->name, $fieldName);
  2206.         //}
  2207.         if (isset($overrideMapping['joinColumns'])) {
  2208.             $mapping['joinColumns'] = $overrideMapping['joinColumns'];
  2209.         }
  2210.         if (isset($overrideMapping['inversedBy'])) {
  2211.             $mapping['inversedBy'] = $overrideMapping['inversedBy'];
  2212.         }
  2213.         if (isset($overrideMapping['joinTable'])) {
  2214.             $mapping['joinTable'] = $overrideMapping['joinTable'];
  2215.         }
  2216.         if (isset($overrideMapping['fetch'])) {
  2217.             $mapping['fetch'] = $overrideMapping['fetch'];
  2218.         }
  2219.         $mapping['joinColumnFieldNames']       = null;
  2220.         $mapping['joinTableColumns']           = null;
  2221.         $mapping['sourceToTargetKeyColumns']   = null;
  2222.         $mapping['relationToSourceKeyColumns'] = null;
  2223.         $mapping['relationToTargetKeyColumns'] = null;
  2224.         switch ($mapping['type']) {
  2225.             case self::ONE_TO_ONE:
  2226.                 $mapping $this->_validateAndCompleteOneToOneMapping($mapping);
  2227.                 break;
  2228.             case self::ONE_TO_MANY:
  2229.                 $mapping $this->_validateAndCompleteOneToManyMapping($mapping);
  2230.                 break;
  2231.             case self::MANY_TO_ONE:
  2232.                 $mapping $this->_validateAndCompleteOneToOneMapping($mapping);
  2233.                 break;
  2234.             case self::MANY_TO_MANY:
  2235.                 $mapping $this->_validateAndCompleteManyToManyMapping($mapping);
  2236.                 break;
  2237.         }
  2238.         $this->associationMappings[$fieldName] = $mapping;
  2239.     }
  2240.     /**
  2241.      * Sets the override for a mapped field.
  2242.      *
  2243.      * @param string $fieldName
  2244.      * @psalm-param array<string, mixed> $overrideMapping
  2245.      *
  2246.      * @return void
  2247.      *
  2248.      * @throws MappingException
  2249.      */
  2250.     public function setAttributeOverride($fieldName, array $overrideMapping)
  2251.     {
  2252.         if (! isset($this->fieldMappings[$fieldName])) {
  2253.             throw MappingException::invalidOverrideFieldName($this->name$fieldName);
  2254.         }
  2255.         $mapping $this->fieldMappings[$fieldName];
  2256.         //if (isset($mapping['inherited'])) {
  2257.             // TODO: Enable this exception in 2.8
  2258.             //throw MappingException::illegalOverrideOfInheritedProperty($this->name, $fieldName);
  2259.         //}
  2260.         if (isset($mapping['id'])) {
  2261.             $overrideMapping['id'] = $mapping['id'];
  2262.         }
  2263.         if (! isset($overrideMapping['type'])) {
  2264.             $overrideMapping['type'] = $mapping['type'];
  2265.         }
  2266.         if (! isset($overrideMapping['fieldName'])) {
  2267.             $overrideMapping['fieldName'] = $mapping['fieldName'];
  2268.         }
  2269.         if ($overrideMapping['type'] !== $mapping['type']) {
  2270.             throw MappingException::invalidOverrideFieldType($this->name$fieldName);
  2271.         }
  2272.         unset($this->fieldMappings[$fieldName]);
  2273.         unset($this->fieldNames[$mapping['columnName']]);
  2274.         unset($this->columnNames[$mapping['fieldName']]);
  2275.         $overrideMapping $this->validateAndCompleteFieldMapping($overrideMapping);
  2276.         $this->fieldMappings[$fieldName] = $overrideMapping;
  2277.     }
  2278.     /**
  2279.      * Checks whether a mapped field is inherited from an entity superclass.
  2280.      *
  2281.      * @param string $fieldName
  2282.      *
  2283.      * @return bool TRUE if the field is inherited, FALSE otherwise.
  2284.      */
  2285.     public function isInheritedField($fieldName)
  2286.     {
  2287.         return isset($this->fieldMappings[$fieldName]['inherited']);
  2288.     }
  2289.     /**
  2290.      * Checks if this entity is the root in any entity-inheritance-hierarchy.
  2291.      *
  2292.      * @return bool
  2293.      */
  2294.     public function isRootEntity()
  2295.     {
  2296.         return $this->name === $this->rootEntityName;
  2297.     }
  2298.     /**
  2299.      * Checks whether a mapped association field is inherited from a superclass.
  2300.      *
  2301.      * @param string $fieldName
  2302.      *
  2303.      * @return bool TRUE if the field is inherited, FALSE otherwise.
  2304.      */
  2305.     public function isInheritedAssociation($fieldName)
  2306.     {
  2307.         return isset($this->associationMappings[$fieldName]['inherited']);
  2308.     }
  2309.     /**
  2310.      * @param string $fieldName
  2311.      *
  2312.      * @return bool
  2313.      */
  2314.     public function isInheritedEmbeddedClass($fieldName)
  2315.     {
  2316.         return isset($this->embeddedClasses[$fieldName]['inherited']);
  2317.     }
  2318.     /**
  2319.      * Sets the name of the primary table the class is mapped to.
  2320.      *
  2321.      * @deprecated Use {@link setPrimaryTable}.
  2322.      *
  2323.      * @param string $tableName The table name.
  2324.      *
  2325.      * @return void
  2326.      */
  2327.     public function setTableName($tableName)
  2328.     {
  2329.         $this->table['name'] = $tableName;
  2330.     }
  2331.     /**
  2332.      * Sets the primary table definition. The provided array supports the
  2333.      * following structure:
  2334.      *
  2335.      * name => <tableName> (optional, defaults to class name)
  2336.      * indexes => array of indexes (optional)
  2337.      * uniqueConstraints => array of constraints (optional)
  2338.      *
  2339.      * If a key is omitted, the current value is kept.
  2340.      *
  2341.      * @psalm-param array<string, mixed> $table The table description.
  2342.      *
  2343.      * @return void
  2344.      */
  2345.     public function setPrimaryTable(array $table)
  2346.     {
  2347.         if (isset($table['name'])) {
  2348.             // Split schema and table name from a table name like "myschema.mytable"
  2349.             if (strpos($table['name'], '.') !== false) {
  2350.                 [$this->table['schema'], $table['name']] = explode('.'$table['name'], 2);
  2351.             }
  2352.             if ($table['name'][0] === '`') {
  2353.                 $table['name']         = trim($table['name'], '`');
  2354.                 $this->table['quoted'] = true;
  2355.             }
  2356.             $this->table['name'] = $table['name'];
  2357.         }
  2358.         if (isset($table['quoted'])) {
  2359.             $this->table['quoted'] = $table['quoted'];
  2360.         }
  2361.         if (isset($table['schema'])) {
  2362.             $this->table['schema'] = $table['schema'];
  2363.         }
  2364.         if (isset($table['indexes'])) {
  2365.             $this->table['indexes'] = $table['indexes'];
  2366.         }
  2367.         if (isset($table['uniqueConstraints'])) {
  2368.             $this->table['uniqueConstraints'] = $table['uniqueConstraints'];
  2369.         }
  2370.         if (isset($table['options'])) {
  2371.             $this->table['options'] = $table['options'];
  2372.         }
  2373.     }
  2374.     /**
  2375.      * Checks whether the given type identifies an inheritance type.
  2376.      *
  2377.      * @return bool TRUE if the given type identifies an inheritance type, FALSE otherwise.
  2378.      */
  2379.     private function isInheritanceType(int $type): bool
  2380.     {
  2381.         return $type === self::INHERITANCE_TYPE_NONE ||
  2382.                 $type === self::INHERITANCE_TYPE_SINGLE_TABLE ||
  2383.                 $type === self::INHERITANCE_TYPE_JOINED ||
  2384.                 $type === self::INHERITANCE_TYPE_TABLE_PER_CLASS;
  2385.     }
  2386.     /**
  2387.      * Adds a mapped field to the class.
  2388.      *
  2389.      * @psalm-param array<string, mixed> $mapping The field mapping.
  2390.      *
  2391.      * @return void
  2392.      *
  2393.      * @throws MappingException
  2394.      */
  2395.     public function mapField(array $mapping)
  2396.     {
  2397.         $mapping $this->validateAndCompleteFieldMapping($mapping);
  2398.         $this->assertFieldNotMapped($mapping['fieldName']);
  2399.         if (isset($mapping['generated'])) {
  2400.             $this->requiresFetchAfterChange true;
  2401.         }
  2402.         $this->fieldMappings[$mapping['fieldName']] = $mapping;
  2403.     }
  2404.     /**
  2405.      * INTERNAL:
  2406.      * Adds an association mapping without completing/validating it.
  2407.      * This is mainly used to add inherited association mappings to derived classes.
  2408.      *
  2409.      * @psalm-param array<string, mixed> $mapping
  2410.      *
  2411.      * @return void
  2412.      *
  2413.      * @throws MappingException
  2414.      */
  2415.     public function addInheritedAssociationMapping(array $mapping/*, $owningClassName = null*/)
  2416.     {
  2417.         if (isset($this->associationMappings[$mapping['fieldName']])) {
  2418.             throw MappingException::duplicateAssociationMapping($this->name$mapping['fieldName']);
  2419.         }
  2420.         $this->associationMappings[$mapping['fieldName']] = $mapping;
  2421.     }
  2422.     /**
  2423.      * INTERNAL:
  2424.      * Adds a field mapping without completing/validating it.
  2425.      * This is mainly used to add inherited field mappings to derived classes.
  2426.      *
  2427.      * @psalm-param array<string, mixed> $fieldMapping
  2428.      *
  2429.      * @return void
  2430.      */
  2431.     public function addInheritedFieldMapping(array $fieldMapping)
  2432.     {
  2433.         $this->fieldMappings[$fieldMapping['fieldName']] = $fieldMapping;
  2434.         $this->columnNames[$fieldMapping['fieldName']]   = $fieldMapping['columnName'];
  2435.         $this->fieldNames[$fieldMapping['columnName']]   = $fieldMapping['fieldName'];
  2436.     }
  2437.     /**
  2438.      * INTERNAL:
  2439.      * Adds a named query to this class.
  2440.      *
  2441.      * @deprecated
  2442.      *
  2443.      * @psalm-param array<string, mixed> $queryMapping
  2444.      *
  2445.      * @return void
  2446.      *
  2447.      * @throws MappingException
  2448.      */
  2449.     public function addNamedQuery(array $queryMapping)
  2450.     {
  2451.         if (! isset($queryMapping['name'])) {
  2452.             throw MappingException::nameIsMandatoryForQueryMapping($this->name);
  2453.         }
  2454.         Deprecation::trigger(
  2455.             'doctrine/orm',
  2456.             'https://github.com/doctrine/orm/issues/8592',
  2457.             'Named Queries are deprecated, here "%s" on entity %s. Move the query logic into EntityRepository',
  2458.             $queryMapping['name'],
  2459.             $this->name
  2460.         );
  2461.         if (isset($this->namedQueries[$queryMapping['name']])) {
  2462.             throw MappingException::duplicateQueryMapping($this->name$queryMapping['name']);
  2463.         }
  2464.         if (! isset($queryMapping['query'])) {
  2465.             throw MappingException::emptyQueryMapping($this->name$queryMapping['name']);
  2466.         }
  2467.         $name  $queryMapping['name'];
  2468.         $query $queryMapping['query'];
  2469.         $dql   str_replace('__CLASS__'$this->name$query);
  2470.         $this->namedQueries[$name] = [
  2471.             'name'  => $name,
  2472.             'query' => $query,
  2473.             'dql'   => $dql,
  2474.         ];
  2475.     }
  2476.     /**
  2477.      * INTERNAL:
  2478.      * Adds a named native query to this class.
  2479.      *
  2480.      * @deprecated
  2481.      *
  2482.      * @psalm-param array<string, mixed> $queryMapping
  2483.      *
  2484.      * @return void
  2485.      *
  2486.      * @throws MappingException
  2487.      */
  2488.     public function addNamedNativeQuery(array $queryMapping)
  2489.     {
  2490.         if (! isset($queryMapping['name'])) {
  2491.             throw MappingException::nameIsMandatoryForQueryMapping($this->name);
  2492.         }
  2493.         Deprecation::trigger(
  2494.             'doctrine/orm',
  2495.             'https://github.com/doctrine/orm/issues/8592',
  2496.             'Named Native Queries are deprecated, here "%s" on entity %s. Move the query logic into EntityRepository',
  2497.             $queryMapping['name'],
  2498.             $this->name
  2499.         );
  2500.         if (isset($this->namedNativeQueries[$queryMapping['name']])) {
  2501.             throw MappingException::duplicateQueryMapping($this->name$queryMapping['name']);
  2502.         }
  2503.         if (! isset($queryMapping['query'])) {
  2504.             throw MappingException::emptyQueryMapping($this->name$queryMapping['name']);
  2505.         }
  2506.         if (! isset($queryMapping['resultClass']) && ! isset($queryMapping['resultSetMapping'])) {
  2507.             throw MappingException::missingQueryMapping($this->name$queryMapping['name']);
  2508.         }
  2509.         $queryMapping['isSelfClass'] = false;
  2510.         if (isset($queryMapping['resultClass'])) {
  2511.             if ($queryMapping['resultClass'] === '__CLASS__') {
  2512.                 $queryMapping['isSelfClass'] = true;
  2513.                 $queryMapping['resultClass'] = $this->name;
  2514.             }
  2515.             $queryMapping['resultClass'] = $this->fullyQualifiedClassName($queryMapping['resultClass']);
  2516.             $queryMapping['resultClass'] = ltrim($queryMapping['resultClass'], '\\');
  2517.         }
  2518.         $this->namedNativeQueries[$queryMapping['name']] = $queryMapping;
  2519.     }
  2520.     /**
  2521.      * INTERNAL:
  2522.      * Adds a sql result set mapping to this class.
  2523.      *
  2524.      * @psalm-param array<string, mixed> $resultMapping
  2525.      *
  2526.      * @return void
  2527.      *
  2528.      * @throws MappingException
  2529.      */
  2530.     public function addSqlResultSetMapping(array $resultMapping)
  2531.     {
  2532.         if (! isset($resultMapping['name'])) {
  2533.             throw MappingException::nameIsMandatoryForSqlResultSetMapping($this->name);
  2534.         }
  2535.         if (isset($this->sqlResultSetMappings[$resultMapping['name']])) {
  2536.             throw MappingException::duplicateResultSetMapping($this->name$resultMapping['name']);
  2537.         }
  2538.         if (isset($resultMapping['entities'])) {
  2539.             foreach ($resultMapping['entities'] as $key => $entityResult) {
  2540.                 if (! isset($entityResult['entityClass'])) {
  2541.                     throw MappingException::missingResultSetMappingEntity($this->name$resultMapping['name']);
  2542.                 }
  2543.                 $entityResult['isSelfClass'] = false;
  2544.                 if ($entityResult['entityClass'] === '__CLASS__') {
  2545.                     $entityResult['isSelfClass'] = true;
  2546.                     $entityResult['entityClass'] = $this->name;
  2547.                 }
  2548.                 $entityResult['entityClass'] = $this->fullyQualifiedClassName($entityResult['entityClass']);
  2549.                 $resultMapping['entities'][$key]['entityClass'] = ltrim($entityResult['entityClass'], '\\');
  2550.                 $resultMapping['entities'][$key]['isSelfClass'] = $entityResult['isSelfClass'];
  2551.                 if (isset($entityResult['fields'])) {
  2552.                     foreach ($entityResult['fields'] as $k => $field) {
  2553.                         if (! isset($field['name'])) {
  2554.                             throw MappingException::missingResultSetMappingFieldName($this->name$resultMapping['name']);
  2555.                         }
  2556.                         if (! isset($field['column'])) {
  2557.                             $fieldName $field['name'];
  2558.                             if (strpos($fieldName'.')) {
  2559.                                 [, $fieldName] = explode('.'$fieldName);
  2560.                             }
  2561.                             $resultMapping['entities'][$key]['fields'][$k]['column'] = $fieldName;
  2562.                         }
  2563.                     }
  2564.                 }
  2565.             }
  2566.         }
  2567.         $this->sqlResultSetMappings[$resultMapping['name']] = $resultMapping;
  2568.     }
  2569.     /**
  2570.      * Adds a one-to-one mapping.
  2571.      *
  2572.      * @param array<string, mixed> $mapping The mapping.
  2573.      *
  2574.      * @return void
  2575.      */
  2576.     public function mapOneToOne(array $mapping)
  2577.     {
  2578.         $mapping['type'] = self::ONE_TO_ONE;
  2579.         $mapping $this->_validateAndCompleteOneToOneMapping($mapping);
  2580.         $this->_storeAssociationMapping($mapping);
  2581.     }
  2582.     /**
  2583.      * Adds a one-to-many mapping.
  2584.      *
  2585.      * @psalm-param array<string, mixed> $mapping The mapping.
  2586.      *
  2587.      * @return void
  2588.      */
  2589.     public function mapOneToMany(array $mapping)
  2590.     {
  2591.         $mapping['type'] = self::ONE_TO_MANY;
  2592.         $mapping $this->_validateAndCompleteOneToManyMapping($mapping);
  2593.         $this->_storeAssociationMapping($mapping);
  2594.     }
  2595.     /**
  2596.      * Adds a many-to-one mapping.
  2597.      *
  2598.      * @psalm-param array<string, mixed> $mapping The mapping.
  2599.      *
  2600.      * @return void
  2601.      */
  2602.     public function mapManyToOne(array $mapping)
  2603.     {
  2604.         $mapping['type'] = self::MANY_TO_ONE;
  2605.         // A many-to-one mapping is essentially a one-one backreference
  2606.         $mapping $this->_validateAndCompleteOneToOneMapping($mapping);
  2607.         $this->_storeAssociationMapping($mapping);
  2608.     }
  2609.     /**
  2610.      * Adds a many-to-many mapping.
  2611.      *
  2612.      * @psalm-param array<string, mixed> $mapping The mapping.
  2613.      *
  2614.      * @return void
  2615.      */
  2616.     public function mapManyToMany(array $mapping)
  2617.     {
  2618.         $mapping['type'] = self::MANY_TO_MANY;
  2619.         $mapping $this->_validateAndCompleteManyToManyMapping($mapping);
  2620.         $this->_storeAssociationMapping($mapping);
  2621.     }
  2622.     /**
  2623.      * Stores the association mapping.
  2624.      *
  2625.      * @psalm-param array<string, mixed> $assocMapping
  2626.      *
  2627.      * @return void
  2628.      *
  2629.      * @throws MappingException
  2630.      */
  2631.     protected function _storeAssociationMapping(array $assocMapping)
  2632.     {
  2633.         $sourceFieldName $assocMapping['fieldName'];
  2634.         $this->assertFieldNotMapped($sourceFieldName);
  2635.         $this->associationMappings[$sourceFieldName] = $assocMapping;
  2636.     }
  2637.     /**
  2638.      * Registers a custom repository class for the entity class.
  2639.      *
  2640.      * @param string|null $repositoryClassName The class name of the custom mapper.
  2641.      * @psalm-param class-string<EntityRepository>|null $repositoryClassName
  2642.      *
  2643.      * @return void
  2644.      */
  2645.     public function setCustomRepositoryClass($repositoryClassName)
  2646.     {
  2647.         $this->customRepositoryClassName $this->fullyQualifiedClassName($repositoryClassName);
  2648.     }
  2649.     /**
  2650.      * Dispatches the lifecycle event of the given entity to the registered
  2651.      * lifecycle callbacks and lifecycle listeners.
  2652.      *
  2653.      * @deprecated Deprecated since version 2.4 in favor of \Doctrine\ORM\Event\ListenersInvoker
  2654.      *
  2655.      * @param string $lifecycleEvent The lifecycle event.
  2656.      * @param object $entity         The Entity on which the event occurred.
  2657.      *
  2658.      * @return void
  2659.      */
  2660.     public function invokeLifecycleCallbacks($lifecycleEvent$entity)
  2661.     {
  2662.         foreach ($this->lifecycleCallbacks[$lifecycleEvent] as $callback) {
  2663.             $entity->$callback();
  2664.         }
  2665.     }
  2666.     /**
  2667.      * Whether the class has any attached lifecycle listeners or callbacks for a lifecycle event.
  2668.      *
  2669.      * @param string $lifecycleEvent
  2670.      *
  2671.      * @return bool
  2672.      */
  2673.     public function hasLifecycleCallbacks($lifecycleEvent)
  2674.     {
  2675.         return isset($this->lifecycleCallbacks[$lifecycleEvent]);
  2676.     }
  2677.     /**
  2678.      * Gets the registered lifecycle callbacks for an event.
  2679.      *
  2680.      * @param string $event
  2681.      *
  2682.      * @return string[]
  2683.      * @psalm-return list<string>
  2684.      */
  2685.     public function getLifecycleCallbacks($event)
  2686.     {
  2687.         return $this->lifecycleCallbacks[$event] ?? [];
  2688.     }
  2689.     /**
  2690.      * Adds a lifecycle callback for entities of this class.
  2691.      *
  2692.      * @param string $callback
  2693.      * @param string $event
  2694.      *
  2695.      * @return void
  2696.      */
  2697.     public function addLifecycleCallback($callback$event)
  2698.     {
  2699.         if ($this->isEmbeddedClass) {
  2700.             Deprecation::trigger(
  2701.                 'doctrine/orm',
  2702.                 'https://github.com/doctrine/orm/pull/8381',
  2703.                 'Registering lifecycle callback %s on Embedded class %s is not doing anything and will throw exception in 3.0',
  2704.                 $event,
  2705.                 $this->name
  2706.             );
  2707.         }
  2708.         if (isset($this->lifecycleCallbacks[$event]) && in_array($callback$this->lifecycleCallbacks[$event], true)) {
  2709.             return;
  2710.         }
  2711.         $this->lifecycleCallbacks[$event][] = $callback;
  2712.     }
  2713.     /**
  2714.      * Sets the lifecycle callbacks for entities of this class.
  2715.      * Any previously registered callbacks are overwritten.
  2716.      *
  2717.      * @psalm-param array<string, list<string>> $callbacks
  2718.      *
  2719.      * @return void
  2720.      */
  2721.     public function setLifecycleCallbacks(array $callbacks)
  2722.     {
  2723.         $this->lifecycleCallbacks $callbacks;
  2724.     }
  2725.     /**
  2726.      * Adds a entity listener for entities of this class.
  2727.      *
  2728.      * @param string $eventName The entity lifecycle event.
  2729.      * @param string $class     The listener class.
  2730.      * @param string $method    The listener callback method.
  2731.      *
  2732.      * @return void
  2733.      *
  2734.      * @throws MappingException
  2735.      */
  2736.     public function addEntityListener($eventName$class$method)
  2737.     {
  2738.         $class $this->fullyQualifiedClassName($class);
  2739.         $listener = [
  2740.             'class'  => $class,
  2741.             'method' => $method,
  2742.         ];
  2743.         if (! class_exists($class)) {
  2744.             throw MappingException::entityListenerClassNotFound($class$this->name);
  2745.         }
  2746.         if (! method_exists($class$method)) {
  2747.             throw MappingException::entityListenerMethodNotFound($class$method$this->name);
  2748.         }
  2749.         if (isset($this->entityListeners[$eventName]) && in_array($listener$this->entityListeners[$eventName], true)) {
  2750.             throw MappingException::duplicateEntityListener($class$method$this->name);
  2751.         }
  2752.         $this->entityListeners[$eventName][] = $listener;
  2753.     }
  2754.     /**
  2755.      * Sets the discriminator column definition.
  2756.      *
  2757.      * @see getDiscriminatorColumn()
  2758.      *
  2759.      * @param mixed[]|null $columnDef
  2760.      * @psalm-param array<string, mixed>|null $columnDef
  2761.      *
  2762.      * @return void
  2763.      *
  2764.      * @throws MappingException
  2765.      */
  2766.     public function setDiscriminatorColumn($columnDef)
  2767.     {
  2768.         if ($columnDef !== null) {
  2769.             if (! isset($columnDef['name'])) {
  2770.                 throw MappingException::nameIsMandatoryForDiscriminatorColumns($this->name);
  2771.             }
  2772.             if (isset($this->fieldNames[$columnDef['name']])) {
  2773.                 throw MappingException::duplicateColumnName($this->name$columnDef['name']);
  2774.             }
  2775.             if (! isset($columnDef['fieldName'])) {
  2776.                 $columnDef['fieldName'] = $columnDef['name'];
  2777.             }
  2778.             if (! isset($columnDef['type'])) {
  2779.                 $columnDef['type'] = 'string';
  2780.             }
  2781.             if (in_array($columnDef['type'], ['boolean''array''object''datetime''time''date'], true)) {
  2782.                 throw MappingException::invalidDiscriminatorColumnType($this->name$columnDef['type']);
  2783.             }
  2784.             $this->discriminatorColumn $columnDef;
  2785.         }
  2786.     }
  2787.     /**
  2788.      * @return array<string, mixed>
  2789.      */
  2790.     final public function getDiscriminatorColumn(): array
  2791.     {
  2792.         if ($this->discriminatorColumn === null) {
  2793.             throw new LogicException('The discriminator column was not set.');
  2794.         }
  2795.         return $this->discriminatorColumn;
  2796.     }
  2797.     /**
  2798.      * Sets the discriminator values used by this class.
  2799.      * Used for JOINED and SINGLE_TABLE inheritance mapping strategies.
  2800.      *
  2801.      * @psalm-param array<string, class-string> $map
  2802.      *
  2803.      * @return void
  2804.      */
  2805.     public function setDiscriminatorMap(array $map)
  2806.     {
  2807.         foreach ($map as $value => $className) {
  2808.             $this->addDiscriminatorMapClass($value$className);
  2809.         }
  2810.     }
  2811.     /**
  2812.      * Adds one entry of the discriminator map with a new class and corresponding name.
  2813.      *
  2814.      * @param string $name
  2815.      * @param string $className
  2816.      * @psalm-param class-string $className
  2817.      *
  2818.      * @return void
  2819.      *
  2820.      * @throws MappingException
  2821.      */
  2822.     public function addDiscriminatorMapClass($name$className)
  2823.     {
  2824.         $className $this->fullyQualifiedClassName($className);
  2825.         $className ltrim($className'\\');
  2826.         $this->discriminatorMap[$name] = $className;
  2827.         if ($this->name === $className) {
  2828.             $this->discriminatorValue $name;
  2829.             return;
  2830.         }
  2831.         if (! (class_exists($className) || interface_exists($className))) {
  2832.             throw MappingException::invalidClassInDiscriminatorMap($className$this->name);
  2833.         }
  2834.         if (is_subclass_of($className$this->name) && ! in_array($className$this->subClassestrue)) {
  2835.             $this->subClasses[] = $className;
  2836.         }
  2837.     }
  2838.     /**
  2839.      * Checks whether the class has a named query with the given query name.
  2840.      *
  2841.      * @param string $queryName
  2842.      *
  2843.      * @return bool
  2844.      */
  2845.     public function hasNamedQuery($queryName)
  2846.     {
  2847.         return isset($this->namedQueries[$queryName]);
  2848.     }
  2849.     /**
  2850.      * Checks whether the class has a named native query with the given query name.
  2851.      *
  2852.      * @param string $queryName
  2853.      *
  2854.      * @return bool
  2855.      */
  2856.     public function hasNamedNativeQuery($queryName)
  2857.     {
  2858.         return isset($this->namedNativeQueries[$queryName]);
  2859.     }
  2860.     /**
  2861.      * Checks whether the class has a named native query with the given query name.
  2862.      *
  2863.      * @param string $name
  2864.      *
  2865.      * @return bool
  2866.      */
  2867.     public function hasSqlResultSetMapping($name)
  2868.     {
  2869.         return isset($this->sqlResultSetMappings[$name]);
  2870.     }
  2871.     /**
  2872.      * {@inheritDoc}
  2873.      */
  2874.     public function hasAssociation($fieldName)
  2875.     {
  2876.         return isset($this->associationMappings[$fieldName]);
  2877.     }
  2878.     /**
  2879.      * {@inheritDoc}
  2880.      */
  2881.     public function isSingleValuedAssociation($fieldName)
  2882.     {
  2883.         return isset($this->associationMappings[$fieldName])
  2884.             && ($this->associationMappings[$fieldName]['type'] & self::TO_ONE);
  2885.     }
  2886.     /**
  2887.      * {@inheritDoc}
  2888.      */
  2889.     public function isCollectionValuedAssociation($fieldName)
  2890.     {
  2891.         return isset($this->associationMappings[$fieldName])
  2892.             && ! ($this->associationMappings[$fieldName]['type'] & self::TO_ONE);
  2893.     }
  2894.     /**
  2895.      * Is this an association that only has a single join column?
  2896.      *
  2897.      * @param string $fieldName
  2898.      *
  2899.      * @return bool
  2900.      */
  2901.     public function isAssociationWithSingleJoinColumn($fieldName)
  2902.     {
  2903.         return isset($this->associationMappings[$fieldName])
  2904.             && isset($this->associationMappings[$fieldName]['joinColumns'][0])
  2905.             && ! isset($this->associationMappings[$fieldName]['joinColumns'][1]);
  2906.     }
  2907.     /**
  2908.      * Returns the single association join column (if any).
  2909.      *
  2910.      * @param string $fieldName
  2911.      *
  2912.      * @return string
  2913.      *
  2914.      * @throws MappingException
  2915.      */
  2916.     public function getSingleAssociationJoinColumnName($fieldName)
  2917.     {
  2918.         if (! $this->isAssociationWithSingleJoinColumn($fieldName)) {
  2919.             throw MappingException::noSingleAssociationJoinColumnFound($this->name$fieldName);
  2920.         }
  2921.         return $this->associationMappings[$fieldName]['joinColumns'][0]['name'];
  2922.     }
  2923.     /**
  2924.      * Returns the single association referenced join column name (if any).
  2925.      *
  2926.      * @param string $fieldName
  2927.      *
  2928.      * @return string
  2929.      *
  2930.      * @throws MappingException
  2931.      */
  2932.     public function getSingleAssociationReferencedJoinColumnName($fieldName)
  2933.     {
  2934.         if (! $this->isAssociationWithSingleJoinColumn($fieldName)) {
  2935.             throw MappingException::noSingleAssociationJoinColumnFound($this->name$fieldName);
  2936.         }
  2937.         return $this->associationMappings[$fieldName]['joinColumns'][0]['referencedColumnName'];
  2938.     }
  2939.     /**
  2940.      * Used to retrieve a fieldname for either field or association from a given column.
  2941.      *
  2942.      * This method is used in foreign-key as primary-key contexts.
  2943.      *
  2944.      * @param string $columnName
  2945.      *
  2946.      * @return string
  2947.      *
  2948.      * @throws MappingException
  2949.      */
  2950.     public function getFieldForColumn($columnName)
  2951.     {
  2952.         if (isset($this->fieldNames[$columnName])) {
  2953.             return $this->fieldNames[$columnName];
  2954.         }
  2955.         foreach ($this->associationMappings as $assocName => $mapping) {
  2956.             if (
  2957.                 $this->isAssociationWithSingleJoinColumn($assocName) &&
  2958.                 $this->associationMappings[$assocName]['joinColumns'][0]['name'] === $columnName
  2959.             ) {
  2960.                 return $assocName;
  2961.             }
  2962.         }
  2963.         throw MappingException::noFieldNameFoundForColumn($this->name$columnName);
  2964.     }
  2965.     /**
  2966.      * Sets the ID generator used to generate IDs for instances of this class.
  2967.      *
  2968.      * @param AbstractIdGenerator $generator
  2969.      *
  2970.      * @return void
  2971.      */
  2972.     public function setIdGenerator($generator)
  2973.     {
  2974.         $this->idGenerator $generator;
  2975.     }
  2976.     /**
  2977.      * Sets definition.
  2978.      *
  2979.      * @psalm-param array<string, string|null> $definition
  2980.      *
  2981.      * @return void
  2982.      */
  2983.     public function setCustomGeneratorDefinition(array $definition)
  2984.     {
  2985.         $this->customGeneratorDefinition $definition;
  2986.     }
  2987.     /**
  2988.      * Sets the definition of the sequence ID generator for this class.
  2989.      *
  2990.      * The definition must have the following structure:
  2991.      * <code>
  2992.      * array(
  2993.      *     'sequenceName'   => 'name',
  2994.      *     'allocationSize' => 20,
  2995.      *     'initialValue'   => 1
  2996.      *     'quoted'         => 1
  2997.      * )
  2998.      * </code>
  2999.      *
  3000.      * @psalm-param array{sequenceName?: string, allocationSize?: int|string, initialValue?: int|string, quoted?: mixed} $definition
  3001.      *
  3002.      * @return void
  3003.      *
  3004.      * @throws MappingException
  3005.      */
  3006.     public function setSequenceGeneratorDefinition(array $definition)
  3007.     {
  3008.         if (! isset($definition['sequenceName']) || trim($definition['sequenceName']) === '') {
  3009.             throw MappingException::missingSequenceName($this->name);
  3010.         }
  3011.         if ($definition['sequenceName'][0] === '`') {
  3012.             $definition['sequenceName'] = trim($definition['sequenceName'], '`');
  3013.             $definition['quoted']       = true;
  3014.         }
  3015.         if (! isset($definition['allocationSize']) || trim((string) $definition['allocationSize']) === '') {
  3016.             $definition['allocationSize'] = '1';
  3017.         }
  3018.         if (! isset($definition['initialValue']) || trim((string) $definition['initialValue']) === '') {
  3019.             $definition['initialValue'] = '1';
  3020.         }
  3021.         $definition['allocationSize'] = (string) $definition['allocationSize'];
  3022.         $definition['initialValue']   = (string) $definition['initialValue'];
  3023.         $this->sequenceGeneratorDefinition $definition;
  3024.     }
  3025.     /**
  3026.      * Sets the version field mapping used for versioning. Sets the default
  3027.      * value to use depending on the column type.
  3028.      *
  3029.      * @psalm-param array<string, mixed> $mapping The version field mapping array.
  3030.      *
  3031.      * @return void
  3032.      *
  3033.      * @throws MappingException
  3034.      */
  3035.     public function setVersionMapping(array &$mapping)
  3036.     {
  3037.         $this->isVersioned              true;
  3038.         $this->versionField             $mapping['fieldName'];
  3039.         $this->requiresFetchAfterChange true;
  3040.         if (! isset($mapping['default'])) {
  3041.             if (in_array($mapping['type'], ['integer''bigint''smallint'], true)) {
  3042.                 $mapping['default'] = 1;
  3043.             } elseif ($mapping['type'] === 'datetime') {
  3044.                 $mapping['default'] = 'CURRENT_TIMESTAMP';
  3045.             } else {
  3046.                 throw MappingException::unsupportedOptimisticLockingType($this->name$mapping['fieldName'], $mapping['type']);
  3047.             }
  3048.         }
  3049.     }
  3050.     /**
  3051.      * Sets whether this class is to be versioned for optimistic locking.
  3052.      *
  3053.      * @param bool $bool
  3054.      *
  3055.      * @return void
  3056.      */
  3057.     public function setVersioned($bool)
  3058.     {
  3059.         $this->isVersioned $bool;
  3060.         if ($bool) {
  3061.             $this->requiresFetchAfterChange true;
  3062.         }
  3063.     }
  3064.     /**
  3065.      * Sets the name of the field that is to be used for versioning if this class is
  3066.      * versioned for optimistic locking.
  3067.      *
  3068.      * @param string $versionField
  3069.      *
  3070.      * @return void
  3071.      */
  3072.     public function setVersionField($versionField)
  3073.     {
  3074.         $this->versionField $versionField;
  3075.     }
  3076.     /**
  3077.      * Marks this class as read only, no change tracking is applied to it.
  3078.      *
  3079.      * @return void
  3080.      */
  3081.     public function markReadOnly()
  3082.     {
  3083.         $this->isReadOnly true;
  3084.     }
  3085.     /**
  3086.      * {@inheritDoc}
  3087.      */
  3088.     public function getFieldNames()
  3089.     {
  3090.         return array_keys($this->fieldMappings);
  3091.     }
  3092.     /**
  3093.      * {@inheritDoc}
  3094.      */
  3095.     public function getAssociationNames()
  3096.     {
  3097.         return array_keys($this->associationMappings);
  3098.     }
  3099.     /**
  3100.      * {@inheritDoc}
  3101.      *
  3102.      * @param string $assocName
  3103.      *
  3104.      * @return string
  3105.      * @psalm-return class-string
  3106.      *
  3107.      * @throws InvalidArgumentException
  3108.      */
  3109.     public function getAssociationTargetClass($assocName)
  3110.     {
  3111.         if (! isset($this->associationMappings[$assocName])) {
  3112.             throw new InvalidArgumentException("Association name expected, '" $assocName "' is not an association.");
  3113.         }
  3114.         return $this->associationMappings[$assocName]['targetEntity'];
  3115.     }
  3116.     /**
  3117.      * {@inheritDoc}
  3118.      */
  3119.     public function getName()
  3120.     {
  3121.         return $this->name;
  3122.     }
  3123.     /**
  3124.      * Gets the (possibly quoted) identifier column names for safe use in an SQL statement.
  3125.      *
  3126.      * @deprecated Deprecated since version 2.3 in favor of \Doctrine\ORM\Mapping\QuoteStrategy
  3127.      *
  3128.      * @param AbstractPlatform $platform
  3129.      *
  3130.      * @return string[]
  3131.      * @psalm-return list<string>
  3132.      */
  3133.     public function getQuotedIdentifierColumnNames($platform)
  3134.     {
  3135.         $quotedColumnNames = [];
  3136.         foreach ($this->identifier as $idProperty) {
  3137.             if (isset($this->fieldMappings[$idProperty])) {
  3138.                 $quotedColumnNames[] = isset($this->fieldMappings[$idProperty]['quoted'])
  3139.                     ? $platform->quoteIdentifier($this->fieldMappings[$idProperty]['columnName'])
  3140.                     : $this->fieldMappings[$idProperty]['columnName'];
  3141.                 continue;
  3142.             }
  3143.             // Association defined as Id field
  3144.             $joinColumns            $this->associationMappings[$idProperty]['joinColumns'];
  3145.             $assocQuotedColumnNames array_map(
  3146.                 static function ($joinColumn) use ($platform) {
  3147.                     return isset($joinColumn['quoted'])
  3148.                         ? $platform->quoteIdentifier($joinColumn['name'])
  3149.                         : $joinColumn['name'];
  3150.                 },
  3151.                 $joinColumns
  3152.             );
  3153.             $quotedColumnNames array_merge($quotedColumnNames$assocQuotedColumnNames);
  3154.         }
  3155.         return $quotedColumnNames;
  3156.     }
  3157.     /**
  3158.      * Gets the (possibly quoted) column name of a mapped field for safe use  in an SQL statement.
  3159.      *
  3160.      * @deprecated Deprecated since version 2.3 in favor of \Doctrine\ORM\Mapping\QuoteStrategy
  3161.      *
  3162.      * @param string           $field
  3163.      * @param AbstractPlatform $platform
  3164.      *
  3165.      * @return string
  3166.      */
  3167.     public function getQuotedColumnName($field$platform)
  3168.     {
  3169.         return isset($this->fieldMappings[$field]['quoted'])
  3170.             ? $platform->quoteIdentifier($this->fieldMappings[$field]['columnName'])
  3171.             : $this->fieldMappings[$field]['columnName'];
  3172.     }
  3173.     /**
  3174.      * Gets the (possibly quoted) primary table name of this class for safe use in an SQL statement.
  3175.      *
  3176.      * @deprecated Deprecated since version 2.3 in favor of \Doctrine\ORM\Mapping\QuoteStrategy
  3177.      *
  3178.      * @param AbstractPlatform $platform
  3179.      *
  3180.      * @return string
  3181.      */
  3182.     public function getQuotedTableName($platform)
  3183.     {
  3184.         return isset($this->table['quoted'])
  3185.             ? $platform->quoteIdentifier($this->table['name'])
  3186.             : $this->table['name'];
  3187.     }
  3188.     /**
  3189.      * Gets the (possibly quoted) name of the join table.
  3190.      *
  3191.      * @deprecated Deprecated since version 2.3 in favor of \Doctrine\ORM\Mapping\QuoteStrategy
  3192.      *
  3193.      * @param mixed[]          $assoc
  3194.      * @param AbstractPlatform $platform
  3195.      *
  3196.      * @return string
  3197.      */
  3198.     public function getQuotedJoinTableName(array $assoc$platform)
  3199.     {
  3200.         return isset($assoc['joinTable']['quoted'])
  3201.             ? $platform->quoteIdentifier($assoc['joinTable']['name'])
  3202.             : $assoc['joinTable']['name'];
  3203.     }
  3204.     /**
  3205.      * {@inheritDoc}
  3206.      */
  3207.     public function isAssociationInverseSide($fieldName)
  3208.     {
  3209.         return isset($this->associationMappings[$fieldName])
  3210.             && ! $this->associationMappings[$fieldName]['isOwningSide'];
  3211.     }
  3212.     /**
  3213.      * {@inheritDoc}
  3214.      */
  3215.     public function getAssociationMappedByTargetField($fieldName)
  3216.     {
  3217.         return $this->associationMappings[$fieldName]['mappedBy'];
  3218.     }
  3219.     /**
  3220.      * @param string $targetClass
  3221.      *
  3222.      * @return mixed[][]
  3223.      * @psalm-return array<string, array<string, mixed>>
  3224.      */
  3225.     public function getAssociationsByTargetClass($targetClass)
  3226.     {
  3227.         $relations = [];
  3228.         foreach ($this->associationMappings as $mapping) {
  3229.             if ($mapping['targetEntity'] === $targetClass) {
  3230.                 $relations[$mapping['fieldName']] = $mapping;
  3231.             }
  3232.         }
  3233.         return $relations;
  3234.     }
  3235.     /**
  3236.      * @param string|null $className
  3237.      * @psalm-param string|class-string|null $className
  3238.      *
  3239.      * @return string|null null if the input value is null
  3240.      * @psalm-return class-string|null
  3241.      */
  3242.     public function fullyQualifiedClassName($className)
  3243.     {
  3244.         if (empty($className)) {
  3245.             return $className;
  3246.         }
  3247.         if (strpos($className'\\') === false && $this->namespace) {
  3248.             return $this->namespace '\\' $className;
  3249.         }
  3250.         return $className;
  3251.     }
  3252.     /**
  3253.      * @param string $name
  3254.      *
  3255.      * @return mixed
  3256.      */
  3257.     public function getMetadataValue($name)
  3258.     {
  3259.         if (isset($this->$name)) {
  3260.             return $this->$name;
  3261.         }
  3262.         return null;
  3263.     }
  3264.     /**
  3265.      * Map Embedded Class
  3266.      *
  3267.      * @psalm-param array<string, mixed> $mapping
  3268.      *
  3269.      * @return void
  3270.      *
  3271.      * @throws MappingException
  3272.      */
  3273.     public function mapEmbedded(array $mapping)
  3274.     {
  3275.         $this->assertFieldNotMapped($mapping['fieldName']);
  3276.         if (! isset($mapping['class']) && $this->isTypedProperty($mapping['fieldName'])) {
  3277.             $type $this->reflClass->getProperty($mapping['fieldName'])->getType();
  3278.             if ($type instanceof ReflectionNamedType) {
  3279.                 $mapping['class'] = $type->getName();
  3280.             }
  3281.         }
  3282.         $this->embeddedClasses[$mapping['fieldName']] = [
  3283.             'class' => $this->fullyQualifiedClassName($mapping['class']),
  3284.             'columnPrefix' => $mapping['columnPrefix'] ?? null,
  3285.             'declaredField' => $mapping['declaredField'] ?? null,
  3286.             'originalField' => $mapping['originalField'] ?? null,
  3287.         ];
  3288.     }
  3289.     /**
  3290.      * Inline the embeddable class
  3291.      *
  3292.      * @param string $property
  3293.      *
  3294.      * @return void
  3295.      */
  3296.     public function inlineEmbeddable($propertyClassMetadataInfo $embeddable)
  3297.     {
  3298.         foreach ($embeddable->fieldMappings as $fieldMapping) {
  3299.             $fieldMapping['originalClass'] = $fieldMapping['originalClass'] ?? $embeddable->name;
  3300.             $fieldMapping['declaredField'] = isset($fieldMapping['declaredField'])
  3301.                 ? $property '.' $fieldMapping['declaredField']
  3302.                 : $property;
  3303.             $fieldMapping['originalField'] = $fieldMapping['originalField'] ?? $fieldMapping['fieldName'];
  3304.             $fieldMapping['fieldName']     = $property '.' $fieldMapping['fieldName'];
  3305.             if (! empty($this->embeddedClasses[$property]['columnPrefix'])) {
  3306.                 $fieldMapping['columnName'] = $this->embeddedClasses[$property]['columnPrefix'] . $fieldMapping['columnName'];
  3307.             } elseif ($this->embeddedClasses[$property]['columnPrefix'] !== false) {
  3308.                 $fieldMapping['columnName'] = $this->namingStrategy
  3309.                     ->embeddedFieldToColumnName(
  3310.                         $property,
  3311.                         $fieldMapping['columnName'],
  3312.                         $this->reflClass->name,
  3313.                         $embeddable->reflClass->name
  3314.                     );
  3315.             }
  3316.             $this->mapField($fieldMapping);
  3317.         }
  3318.     }
  3319.     /**
  3320.      * @throws MappingException
  3321.      */
  3322.     private function assertFieldNotMapped(string $fieldName): void
  3323.     {
  3324.         if (
  3325.             isset($this->fieldMappings[$fieldName]) ||
  3326.             isset($this->associationMappings[$fieldName]) ||
  3327.             isset($this->embeddedClasses[$fieldName])
  3328.         ) {
  3329.             throw MappingException::duplicateFieldMapping($this->name$fieldName);
  3330.         }
  3331.     }
  3332.     /**
  3333.      * Gets the sequence name based on class metadata.
  3334.      *
  3335.      * @return string
  3336.      *
  3337.      * @todo Sequence names should be computed in DBAL depending on the platform
  3338.      */
  3339.     public function getSequenceName(AbstractPlatform $platform)
  3340.     {
  3341.         $sequencePrefix $this->getSequencePrefix($platform);
  3342.         $columnName     $this->getSingleIdentifierColumnName();
  3343.         return $sequencePrefix '_' $columnName '_seq';
  3344.     }
  3345.     /**
  3346.      * Gets the sequence name prefix based on class metadata.
  3347.      *
  3348.      * @return string
  3349.      *
  3350.      * @todo Sequence names should be computed in DBAL depending on the platform
  3351.      */
  3352.     public function getSequencePrefix(AbstractPlatform $platform)
  3353.     {
  3354.         $tableName      $this->getTableName();
  3355.         $sequencePrefix $tableName;
  3356.         // Prepend the schema name to the table name if there is one
  3357.         $schemaName $this->getSchemaName();
  3358.         if ($schemaName) {
  3359.             $sequencePrefix $schemaName '.' $tableName;
  3360.             if (! $platform->supportsSchemas() && $platform->canEmulateSchemas()) {
  3361.                 $sequencePrefix $schemaName '__' $tableName;
  3362.             }
  3363.         }
  3364.         return $sequencePrefix;
  3365.     }
  3366.     /**
  3367.      * @psalm-param array<string, mixed> $mapping
  3368.      */
  3369.     private function assertMappingOrderBy(array $mapping): void
  3370.     {
  3371.         if (isset($mapping['orderBy']) && ! is_array($mapping['orderBy'])) {
  3372.             throw new InvalidArgumentException("'orderBy' is expected to be an array, not " gettype($mapping['orderBy']));
  3373.         }
  3374.     }
  3375.     /**
  3376.      * @psalm-param class-string $class
  3377.      */
  3378.     private function getAccessibleProperty(ReflectionService $reflServicestring $classstring $field): ?ReflectionProperty
  3379.     {
  3380.         $reflectionProperty $reflService->getAccessibleProperty($class$field);
  3381.         if ($reflectionProperty !== null && PHP_VERSION_ID >= 80100 && $reflectionProperty->isReadOnly()) {
  3382.             $reflectionProperty = new ReflectionReadonlyProperty($reflectionProperty);
  3383.         }
  3384.         return $reflectionProperty;
  3385.     }
  3386. }