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

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