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

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