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

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