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

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