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

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