2008-05-26 00:10:41 +04:00
|
|
|
<?php
|
2008-02-11 20:08:22 +03:00
|
|
|
/*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many individuals
|
2012-05-26 16:37:00 +04:00
|
|
|
* and is licensed under the MIT license. For more information, see
|
2009-02-05 20:34:44 +03:00
|
|
|
* <http://www.doctrine-project.org>.
|
2008-02-11 20:08:22 +03:00
|
|
|
*/
|
2008-02-04 00:29:57 +03:00
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
namespace Doctrine\ORM\Mapping;
|
2008-12-18 17:08:11 +03:00
|
|
|
|
2012-10-12 15:53:20 +04:00
|
|
|
use ReflectionException;
|
|
|
|
use Doctrine\ORM\ORMException;
|
|
|
|
use Doctrine\ORM\EntityManager;
|
|
|
|
use Doctrine\DBAL\Platforms;
|
|
|
|
use Doctrine\ORM\Events;
|
|
|
|
use Doctrine\Common\Persistence\Mapping\ReflectionService;
|
|
|
|
use Doctrine\Common\Persistence\Mapping\ClassMetadata as ClassMetadataInterface;
|
|
|
|
use Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory;
|
|
|
|
use Doctrine\ORM\Id\IdentityGenerator;
|
2013-01-20 23:31:22 +04:00
|
|
|
use Doctrine\ORM\Id\BigIntegerIdentityGenerator;
|
2012-10-12 15:53:20 +04:00
|
|
|
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-02-04 00:29:57 +03:00
|
|
|
/**
|
2009-07-18 15:41:37 +04:00
|
|
|
* The ClassMetadataFactory is used to create ClassMetadata objects that contain all the
|
2012-06-09 17:17:45 +04:00
|
|
|
* metadata mapping information of a class which describes how a class should be mapped
|
2008-12-18 17:08:11 +03:00
|
|
|
* to a relational database.
|
2008-02-04 00:29:57 +03:00
|
|
|
*
|
2009-09-13 03:25:47 +04:00
|
|
|
* @since 2.0
|
2010-03-31 01:14:17 +04:00
|
|
|
* @author Benjamin Eberlei <kontakt@beberlei.de>
|
2009-09-13 03:25:47 +04:00
|
|
|
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
|
|
|
* @author Jonathan Wage <jonwage@gmail.com>
|
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
2008-02-04 00:29:57 +03:00
|
|
|
*/
|
2012-04-07 23:20:17 +04:00
|
|
|
class ClassMetadataFactory extends AbstractClassMetadataFactory
|
2008-02-04 00:29:57 +03:00
|
|
|
{
|
2010-08-28 00:14:48 +04:00
|
|
|
/**
|
|
|
|
* @var EntityManager
|
|
|
|
*/
|
|
|
|
private $em;
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2010-08-28 00:14:48 +04:00
|
|
|
/**
|
2012-06-09 17:17:45 +04:00
|
|
|
* @var \Doctrine\DBAL\Platforms\AbstractPlatform
|
2010-08-28 00:14:48 +04:00
|
|
|
*/
|
|
|
|
private $targetPlatform;
|
|
|
|
|
|
|
|
/**
|
2012-01-17 20:58:56 +04:00
|
|
|
* @var \Doctrine\Common\Persistence\Mapping\Driver\MappingDriver
|
2010-08-28 00:14:48 +04:00
|
|
|
*/
|
|
|
|
private $driver;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Doctrine\Common\EventManager
|
|
|
|
*/
|
|
|
|
private $evm;
|
|
|
|
|
2008-02-11 20:08:22 +03:00
|
|
|
/**
|
2012-06-09 17:17:45 +04:00
|
|
|
* @param EntityManager $em
|
2008-02-11 20:08:22 +03:00
|
|
|
*/
|
2010-11-27 22:53:26 +03:00
|
|
|
public function setEntityManager(EntityManager $em)
|
2008-02-04 00:29:57 +03:00
|
|
|
{
|
2010-08-28 00:14:48 +04:00
|
|
|
$this->em = $em;
|
2008-02-04 00:29:57 +03:00
|
|
|
}
|
2009-01-05 20:25:56 +03:00
|
|
|
|
2009-01-07 00:47:29 +03:00
|
|
|
/**
|
2012-04-07 23:20:17 +04:00
|
|
|
* {@inheritDoc}.
|
2009-01-07 00:47:29 +03:00
|
|
|
*/
|
2012-04-07 23:20:17 +04:00
|
|
|
protected function initialize()
|
2009-12-16 00:06:32 +03:00
|
|
|
{
|
2010-08-28 00:14:48 +04:00
|
|
|
$this->driver = $this->em->getConfiguration()->getMetadataDriverImpl();
|
|
|
|
$this->targetPlatform = $this->em->getConnection()->getDatabasePlatform();
|
|
|
|
$this->evm = $this->em->getEventManager();
|
|
|
|
$this->initialized = true;
|
2009-12-16 00:06:32 +03:00
|
|
|
}
|
2009-01-06 20:22:23 +03:00
|
|
|
|
2009-01-05 20:25:56 +03:00
|
|
|
/**
|
2012-04-07 23:20:17 +04:00
|
|
|
* {@inheritDoc}
|
2008-02-04 00:29:57 +03:00
|
|
|
*/
|
2012-06-09 18:04:47 +04:00
|
|
|
protected function doLoadMetadata($class, $parent, $rootEntityFound, array $nonSuperclassParents)
|
2008-02-04 00:29:57 +03:00
|
|
|
{
|
2012-06-09 18:04:47 +04:00
|
|
|
/* @var $class ClassMetadata */
|
|
|
|
/* @var $parent ClassMetadata */
|
|
|
|
if ($parent) {
|
|
|
|
$class->setInheritanceType($parent->inheritanceType);
|
|
|
|
$class->setDiscriminatorColumn($parent->discriminatorColumn);
|
|
|
|
$class->setIdGeneratorType($parent->generatorType);
|
|
|
|
$this->addInheritedFields($class, $parent);
|
|
|
|
$this->addInheritedRelations($class, $parent);
|
|
|
|
$class->setIdentifier($parent->identifier);
|
|
|
|
$class->setVersioned($parent->isVersioned);
|
|
|
|
$class->setVersionField($parent->versionField);
|
|
|
|
$class->setDiscriminatorMap($parent->discriminatorMap);
|
|
|
|
$class->setLifecycleCallbacks($parent->lifecycleCallbacks);
|
|
|
|
$class->setChangeTrackingPolicy($parent->changeTrackingPolicy);
|
|
|
|
|
2013-04-26 23:11:04 +04:00
|
|
|
if ( ! empty($parent->customGeneratorDefinition)) {
|
|
|
|
$class->setCustomGeneratorDefinition($parent->customGeneratorDefinition);
|
|
|
|
}
|
|
|
|
|
2012-06-09 18:04:47 +04:00
|
|
|
if ($parent->isMappedSuperclass) {
|
|
|
|
$class->setCustomRepositoryClass($parent->customRepositoryClassName);
|
2008-02-04 00:29:57 +03:00
|
|
|
}
|
2012-06-09 18:04:47 +04:00
|
|
|
}
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2012-06-09 18:04:47 +04:00
|
|
|
// Invoke driver
|
|
|
|
try {
|
|
|
|
$this->driver->loadMetadataForClass($class->getName(), $class);
|
|
|
|
} catch (ReflectionException $e) {
|
|
|
|
throw MappingException::reflectionFailure($class->getName(), $e);
|
|
|
|
}
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2012-06-09 18:04:47 +04:00
|
|
|
// If this class has a parent the id generator strategy is inherited.
|
|
|
|
// However this is only true if the hierarchy of parents contains the root entity,
|
|
|
|
// if it consists of mapped superclasses these don't necessarily include the id field.
|
|
|
|
if ($parent && $rootEntityFound) {
|
|
|
|
if ($parent->isIdGeneratorSequence()) {
|
|
|
|
$class->setSequenceGeneratorDefinition($parent->sequenceGeneratorDefinition);
|
|
|
|
} else if ($parent->isIdGeneratorTable()) {
|
|
|
|
$class->tableGeneratorDefinition = $parent->tableGeneratorDefinition;
|
2011-11-14 22:07:37 +04:00
|
|
|
}
|
2009-05-19 20:11:08 +04:00
|
|
|
|
2012-06-09 18:04:47 +04:00
|
|
|
if ($parent->generatorType) {
|
|
|
|
$class->setIdGeneratorType($parent->generatorType);
|
2012-03-12 04:46:31 +04:00
|
|
|
}
|
|
|
|
|
2012-06-09 18:04:47 +04:00
|
|
|
if ($parent->idGenerator) {
|
|
|
|
$class->setIdGenerator($parent->idGenerator);
|
2012-03-12 04:46:31 +04:00
|
|
|
}
|
2012-06-09 18:04:47 +04:00
|
|
|
} else {
|
|
|
|
$this->completeIdGeneratorMapping($class);
|
|
|
|
}
|
2012-03-12 04:46:31 +04:00
|
|
|
|
2012-06-09 18:04:47 +04:00
|
|
|
if ($parent && $parent->isInheritanceTypeSingleTable()) {
|
|
|
|
$class->setPrimaryTable($parent->table);
|
|
|
|
}
|
2009-05-21 12:53:40 +04:00
|
|
|
|
2012-06-09 18:04:47 +04:00
|
|
|
if ($parent && $parent->containsForeignIdentifier) {
|
|
|
|
$class->containsForeignIdentifier = true;
|
|
|
|
}
|
2011-12-17 03:19:27 +04:00
|
|
|
|
2012-06-11 01:52:48 +04:00
|
|
|
if ($parent && !empty($parent->namedQueries)) {
|
2012-06-09 18:04:47 +04:00
|
|
|
$this->addInheritedNamedQueries($class, $parent);
|
|
|
|
}
|
2010-04-14 12:46:35 +04:00
|
|
|
|
2012-06-11 01:52:48 +04:00
|
|
|
if ($parent && !empty($parent->namedNativeQueries)) {
|
2012-06-09 18:04:47 +04:00
|
|
|
$this->addInheritedNamedNativeQueries($class, $parent);
|
|
|
|
}
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2012-06-11 01:52:48 +04:00
|
|
|
if ($parent && !empty($parent->sqlResultSetMappings)) {
|
2012-06-09 18:04:47 +04:00
|
|
|
$this->addInheritedSqlResultSetMappings($class, $parent);
|
|
|
|
}
|
2010-04-14 12:46:35 +04:00
|
|
|
|
2012-07-30 02:10:33 +04:00
|
|
|
if ($parent && !empty($parent->entityListeners) && empty($class->entityListeners)) {
|
|
|
|
$class->entityListeners = $parent->entityListeners;
|
|
|
|
}
|
|
|
|
|
2012-06-09 18:04:47 +04:00
|
|
|
$class->setParentClasses($nonSuperclassParents);
|
2010-04-14 12:46:35 +04:00
|
|
|
|
2012-06-09 18:04:47 +04:00
|
|
|
if ( $class->isRootEntity() && ! $class->isInheritanceTypeNone() && ! $class->discriminatorMap) {
|
|
|
|
$this->addDefaultDiscriminatorMap($class);
|
|
|
|
}
|
2010-04-14 12:46:35 +04:00
|
|
|
|
2012-06-09 18:04:47 +04:00
|
|
|
if ($this->evm->hasListeners(Events::loadClassMetadata)) {
|
|
|
|
$eventArgs = new LoadClassMetadataEventArgs($class, $this->em);
|
|
|
|
$this->evm->dispatchEvent(Events::loadClassMetadata, $eventArgs);
|
2008-02-04 00:29:57 +03:00
|
|
|
}
|
2010-04-14 12:46:35 +04:00
|
|
|
|
2012-06-09 18:04:47 +04:00
|
|
|
$this->validateRuntimeMetadata($class, $parent);
|
2008-02-04 00:29:57 +03:00
|
|
|
}
|
2009-01-06 20:22:23 +03:00
|
|
|
|
2011-07-13 01:39:56 +04:00
|
|
|
/**
|
|
|
|
* Validate runtime metadata is correctly defined.
|
|
|
|
*
|
2012-12-03 13:36:08 +04:00
|
|
|
* @param ClassMetadata $class
|
|
|
|
* @param ClassMetadataInterface|null $parent
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*
|
2012-06-09 17:17:45 +04:00
|
|
|
* @throws MappingException
|
2011-07-13 01:39:56 +04:00
|
|
|
*/
|
|
|
|
protected function validateRuntimeMetadata($class, $parent)
|
|
|
|
{
|
2012-01-03 00:32:18 +04:00
|
|
|
if ( ! $class->reflClass ) {
|
|
|
|
// only validate if there is a reflection class instance
|
|
|
|
return;
|
2011-07-13 01:39:56 +04:00
|
|
|
}
|
|
|
|
|
2012-01-03 00:32:18 +04:00
|
|
|
$class->validateIdentifier();
|
2013-03-11 04:08:58 +04:00
|
|
|
$class->validateAssociations();
|
2012-01-03 00:32:18 +04:00
|
|
|
$class->validateLifecycleCallbacks($this->getReflectionService());
|
|
|
|
|
2011-07-13 01:39:56 +04:00
|
|
|
// verify inheritance
|
2012-06-23 21:19:47 +04:00
|
|
|
if ( ! $class->isMappedSuperclass && !$class->isInheritanceTypeNone()) {
|
|
|
|
if ( ! $parent) {
|
2011-07-13 01:39:56 +04:00
|
|
|
if (count($class->discriminatorMap) == 0) {
|
|
|
|
throw MappingException::missingDiscriminatorMap($class->name);
|
|
|
|
}
|
2012-06-23 21:19:47 +04:00
|
|
|
if ( ! $class->discriminatorColumn) {
|
2011-07-13 01:39:56 +04:00
|
|
|
throw MappingException::missingDiscriminatorColumn($class->name);
|
|
|
|
}
|
|
|
|
} else if ($parent && !$class->reflClass->isAbstract() && !in_array($class->name, array_values($class->discriminatorMap))) {
|
2012-06-09 17:17:45 +04:00
|
|
|
// enforce discriminator map for all entities of an inheritance hierarchy, otherwise problems will occur.
|
2011-07-13 01:39:56 +04:00
|
|
|
throw MappingException::mappedClassNotPartOfDiscriminatorMap($class->name, $class->rootEntityName);
|
|
|
|
}
|
|
|
|
} else if ($class->isMappedSuperclass && $class->name == $class->rootEntityName && (count($class->discriminatorMap) || $class->discriminatorColumn)) {
|
2012-06-09 17:17:45 +04:00
|
|
|
// second condition is necessary for mapped superclasses in the middle of an inheritance hierarchy
|
2011-07-13 01:39:56 +04:00
|
|
|
throw MappingException::noInheritanceOnMappedSuperClass($class->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-07 00:47:29 +03:00
|
|
|
/**
|
2012-04-07 23:20:17 +04:00
|
|
|
* {@inheritDoc}
|
2009-01-07 00:47:29 +03:00
|
|
|
*/
|
2010-08-28 00:14:48 +04:00
|
|
|
protected function newClassMetadataInstance($className)
|
2009-01-06 20:22:23 +03:00
|
|
|
{
|
2011-12-23 20:41:03 +04:00
|
|
|
return new ClassMetadata($className, $this->em->getConfiguration()->getNamingStrategy());
|
2009-01-06 20:22:23 +03:00
|
|
|
}
|
2010-07-30 08:30:02 +04:00
|
|
|
|
2011-12-17 03:19:27 +04:00
|
|
|
/**
|
|
|
|
* Adds a default discriminator map if no one is given
|
|
|
|
*
|
2012-05-05 01:15:12 +04:00
|
|
|
* If an entity is of any inheritance type and does not contain a
|
2012-06-09 17:17:45 +04:00
|
|
|
* discriminator map, then the map is generated automatically. This process
|
2012-05-05 01:15:12 +04:00
|
|
|
* is expensive computation wise.
|
|
|
|
*
|
2012-06-09 17:17:45 +04:00
|
|
|
* The automatically generated discriminator map contains the lowercase short name of
|
2012-05-05 01:15:12 +04:00
|
|
|
* each class as key.
|
|
|
|
*
|
2011-12-17 03:19:27 +04:00
|
|
|
* @param \Doctrine\ORM\Mapping\ClassMetadata $class
|
2012-12-03 13:36:08 +04:00
|
|
|
*
|
2012-06-09 17:17:45 +04:00
|
|
|
* @throws MappingException
|
2011-12-17 03:19:27 +04:00
|
|
|
*/
|
|
|
|
private function addDefaultDiscriminatorMap(ClassMetadata $class)
|
|
|
|
{
|
|
|
|
$allClasses = $this->driver->getAllClassNames();
|
|
|
|
$fqcn = $class->getName();
|
2012-05-05 01:15:12 +04:00
|
|
|
$map = array($this->getShortName($class->name) => $fqcn);
|
2011-12-17 03:19:27 +04:00
|
|
|
|
2012-05-05 01:15:12 +04:00
|
|
|
$duplicates = array();
|
|
|
|
foreach ($allClasses as $subClassCandidate) {
|
|
|
|
if (is_subclass_of($subClassCandidate, $fqcn)) {
|
|
|
|
$shortName = $this->getShortName($subClassCandidate);
|
2011-12-17 03:19:27 +04:00
|
|
|
|
2012-05-05 01:15:12 +04:00
|
|
|
if (isset($map[$shortName])) {
|
|
|
|
$duplicates[] = $shortName;
|
2011-12-17 03:19:27 +04:00
|
|
|
}
|
2012-05-05 01:15:12 +04:00
|
|
|
|
|
|
|
$map[$shortName] = $subClassCandidate;
|
2011-12-17 03:19:27 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-05 01:15:12 +04:00
|
|
|
if ($duplicates) {
|
|
|
|
throw MappingException::duplicateDiscriminatorEntry($class->name, $duplicates, $map);
|
|
|
|
}
|
2011-12-17 03:19:27 +04:00
|
|
|
|
2012-05-05 01:15:12 +04:00
|
|
|
$class->setDiscriminatorMap($map);
|
|
|
|
}
|
2011-12-17 03:19:27 +04:00
|
|
|
|
2012-05-05 01:15:12 +04:00
|
|
|
/**
|
2012-12-03 13:36:08 +04:00
|
|
|
* Gets the lower-case short name of a class.
|
2012-05-05 01:15:12 +04:00
|
|
|
*
|
|
|
|
* @param string $className
|
2012-12-03 13:36:08 +04:00
|
|
|
*
|
2012-05-05 01:15:12 +04:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function getShortName($className)
|
|
|
|
{
|
|
|
|
if (strpos($className, "\\") === false) {
|
|
|
|
return strtolower($className);
|
2011-12-17 03:19:27 +04:00
|
|
|
}
|
2012-05-05 01:15:12 +04:00
|
|
|
|
|
|
|
$parts = explode("\\", $className);
|
|
|
|
return strtolower(end($parts));
|
2011-12-17 03:19:27 +04:00
|
|
|
}
|
|
|
|
|
2008-08-16 23:40:59 +04:00
|
|
|
/**
|
|
|
|
* Adds inherited fields to the subclass mapping.
|
|
|
|
*
|
2011-12-12 00:52:29 +04:00
|
|
|
* @param \Doctrine\ORM\Mapping\ClassMetadata $subClass
|
|
|
|
* @param \Doctrine\ORM\Mapping\ClassMetadata $parentClass
|
2012-12-03 13:36:08 +04:00
|
|
|
*
|
|
|
|
* @return void
|
2008-08-16 23:40:59 +04:00
|
|
|
*/
|
2010-08-28 00:14:48 +04:00
|
|
|
private function addInheritedFields(ClassMetadata $subClass, ClassMetadata $parentClass)
|
2008-02-04 00:29:57 +03:00
|
|
|
{
|
2012-01-12 22:18:57 +04:00
|
|
|
foreach ($parentClass->fieldMappings as $mapping) {
|
2009-07-30 19:16:02 +04:00
|
|
|
if ( ! isset($mapping['inherited']) && ! $parentClass->isMappedSuperclass) {
|
2009-05-14 14:03:09 +04:00
|
|
|
$mapping['inherited'] = $parentClass->name;
|
2008-08-22 13:05:14 +04:00
|
|
|
}
|
2010-04-14 19:07:08 +04:00
|
|
|
if ( ! isset($mapping['declared'])) {
|
|
|
|
$mapping['declared'] = $parentClass->name;
|
|
|
|
}
|
|
|
|
$subClass->addInheritedFieldMapping($mapping);
|
2009-05-21 12:53:40 +04:00
|
|
|
}
|
|
|
|
foreach ($parentClass->reflFields as $name => $field) {
|
|
|
|
$subClass->reflFields[$name] = $field;
|
2008-02-04 00:29:57 +03:00
|
|
|
}
|
|
|
|
}
|
2010-04-14 19:07:08 +04:00
|
|
|
|
2008-08-16 23:40:59 +04:00
|
|
|
/**
|
2010-04-14 19:07:08 +04:00
|
|
|
* Adds inherited association mappings to the subclass mapping.
|
2008-08-16 23:40:59 +04:00
|
|
|
*
|
2011-12-12 00:52:29 +04:00
|
|
|
* @param \Doctrine\ORM\Mapping\ClassMetadata $subClass
|
|
|
|
* @param \Doctrine\ORM\Mapping\ClassMetadata $parentClass
|
2012-12-03 13:36:08 +04:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*
|
2012-06-09 17:17:45 +04:00
|
|
|
* @throws MappingException
|
2008-08-16 23:40:59 +04:00
|
|
|
*/
|
2010-08-28 00:14:48 +04:00
|
|
|
private function addInheritedRelations(ClassMetadata $subClass, ClassMetadata $parentClass)
|
2008-05-26 00:10:41 +04:00
|
|
|
{
|
2010-04-14 19:07:08 +04:00
|
|
|
foreach ($parentClass->associationMappings as $field => $mapping) {
|
2010-09-22 01:14:45 +04:00
|
|
|
if ($parentClass->isMappedSuperclass) {
|
2010-12-31 16:39:01 +03:00
|
|
|
if ($mapping['type'] & ClassMetadata::TO_MANY && !$mapping['isOwningSide']) {
|
2013-03-11 04:08:58 +04:00
|
|
|
throw MappingException::illegalToManyAssociationOnMappedSuperclass($parentClass->name, $field);
|
2010-12-28 13:59:51 +03:00
|
|
|
}
|
2010-09-22 01:14:45 +04:00
|
|
|
$mapping['sourceEntity'] = $subClass->name;
|
|
|
|
}
|
|
|
|
|
2010-08-09 15:13:21 +04:00
|
|
|
//$subclassMapping = $mapping;
|
|
|
|
if ( ! isset($mapping['inherited']) && ! $parentClass->isMappedSuperclass) {
|
|
|
|
$mapping['inherited'] = $parentClass->name;
|
2010-04-14 19:07:08 +04:00
|
|
|
}
|
2010-08-09 15:13:21 +04:00
|
|
|
if ( ! isset($mapping['declared'])) {
|
|
|
|
$mapping['declared'] = $parentClass->name;
|
2009-05-21 12:53:40 +04:00
|
|
|
}
|
2010-08-09 15:13:21 +04:00
|
|
|
$subClass->addInheritedAssociationMapping($mapping);
|
2009-05-21 12:53:40 +04:00
|
|
|
}
|
|
|
|
}
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2011-11-14 22:07:37 +04:00
|
|
|
/**
|
|
|
|
* Adds inherited named queries to the subclass mapping.
|
2011-12-20 01:56:19 +04:00
|
|
|
*
|
2011-11-14 22:07:37 +04:00
|
|
|
* @since 2.2
|
2012-12-03 13:36:08 +04:00
|
|
|
*
|
2011-12-12 01:46:24 +04:00
|
|
|
* @param \Doctrine\ORM\Mapping\ClassMetadata $subClass
|
|
|
|
* @param \Doctrine\ORM\Mapping\ClassMetadata $parentClass
|
2012-12-03 13:36:08 +04:00
|
|
|
*
|
|
|
|
* @return void
|
2011-11-14 22:07:37 +04:00
|
|
|
*/
|
|
|
|
private function addInheritedNamedQueries(ClassMetadata $subClass, ClassMetadata $parentClass)
|
|
|
|
{
|
|
|
|
foreach ($parentClass->namedQueries as $name => $query) {
|
2012-06-23 21:19:47 +04:00
|
|
|
if ( ! isset ($subClass->namedQueries[$name])) {
|
2011-11-14 22:07:37 +04:00
|
|
|
$subClass->addNamedQuery(array(
|
|
|
|
'name' => $query['name'],
|
|
|
|
'query' => $query['query']
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-05-21 12:53:40 +04:00
|
|
|
|
2012-03-12 04:46:31 +04:00
|
|
|
/**
|
|
|
|
* Adds inherited named native queries to the subclass mapping.
|
|
|
|
*
|
|
|
|
* @since 2.3
|
2012-12-03 13:36:08 +04:00
|
|
|
*
|
2012-03-12 04:46:31 +04:00
|
|
|
* @param \Doctrine\ORM\Mapping\ClassMetadata $subClass
|
|
|
|
* @param \Doctrine\ORM\Mapping\ClassMetadata $parentClass
|
2012-12-03 13:36:08 +04:00
|
|
|
*
|
|
|
|
* @return void
|
2012-03-12 04:46:31 +04:00
|
|
|
*/
|
|
|
|
private function addInheritedNamedNativeQueries(ClassMetadata $subClass, ClassMetadata $parentClass)
|
|
|
|
{
|
|
|
|
foreach ($parentClass->namedNativeQueries as $name => $query) {
|
2012-06-23 21:19:47 +04:00
|
|
|
if ( ! isset ($subClass->namedNativeQueries[$name])) {
|
2012-03-12 04:46:31 +04:00
|
|
|
$subClass->addNamedNativeQuery(array(
|
|
|
|
'name' => $query['name'],
|
|
|
|
'query' => $query['query'],
|
|
|
|
'isSelfClass' => $query['isSelfClass'],
|
|
|
|
'resultSetMapping' => $query['resultSetMapping'],
|
|
|
|
'resultClass' => $query['isSelfClass'] ? $subClass->name : $query['resultClass'],
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds inherited sql result set mappings to the subclass mapping.
|
|
|
|
*
|
|
|
|
* @since 2.3
|
2012-12-03 13:36:08 +04:00
|
|
|
*
|
2012-03-12 04:46:31 +04:00
|
|
|
* @param \Doctrine\ORM\Mapping\ClassMetadata $subClass
|
|
|
|
* @param \Doctrine\ORM\Mapping\ClassMetadata $parentClass
|
2012-12-03 13:36:08 +04:00
|
|
|
*
|
|
|
|
* @return void
|
2012-03-12 04:46:31 +04:00
|
|
|
*/
|
|
|
|
private function addInheritedSqlResultSetMappings(ClassMetadata $subClass, ClassMetadata $parentClass)
|
|
|
|
{
|
|
|
|
foreach ($parentClass->sqlResultSetMappings as $name => $mapping) {
|
2012-06-23 21:19:47 +04:00
|
|
|
if ( ! isset ($subClass->sqlResultSetMappings[$name])) {
|
2012-03-12 04:46:31 +04:00
|
|
|
$entities = array();
|
|
|
|
foreach ($mapping['entities'] as $entity) {
|
|
|
|
$entities[] = array(
|
|
|
|
'fields' => $entity['fields'],
|
|
|
|
'isSelfClass' => $entity['isSelfClass'],
|
|
|
|
'discriminatorColumn' => $entity['discriminatorColumn'],
|
|
|
|
'entityClass' => $entity['isSelfClass'] ? $subClass->name : $entity['entityClass'],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$subClass->addSqlResultSetMapping(array(
|
|
|
|
'name' => $mapping['name'],
|
|
|
|
'columns' => $mapping['columns'],
|
|
|
|
'entities' => $entities,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-20 05:43:27 +04:00
|
|
|
/**
|
2009-02-05 20:34:44 +03:00
|
|
|
* Completes the ID generator mapping. If "auto" is specified we choose the generator
|
|
|
|
* most appropriate for the targeted database platform.
|
2008-03-26 14:10:45 +03:00
|
|
|
*
|
2012-06-09 17:17:45 +04:00
|
|
|
* @param ClassMetadataInfo $class
|
2012-12-03 13:36:08 +04:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*
|
2012-06-09 17:17:45 +04:00
|
|
|
* @throws ORMException
|
2008-02-04 00:29:57 +03:00
|
|
|
*/
|
2010-08-28 00:14:48 +04:00
|
|
|
private function completeIdGeneratorMapping(ClassMetadataInfo $class)
|
2008-02-04 00:29:57 +03:00
|
|
|
{
|
2009-05-14 14:03:09 +04:00
|
|
|
$idGenType = $class->generatorType;
|
2009-03-30 23:43:05 +04:00
|
|
|
if ($idGenType == ClassMetadata::GENERATOR_TYPE_AUTO) {
|
2010-08-28 00:14:48 +04:00
|
|
|
if ($this->targetPlatform->prefersSequences()) {
|
2009-02-05 20:34:44 +03:00
|
|
|
$class->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_SEQUENCE);
|
2010-08-28 00:14:48 +04:00
|
|
|
} else if ($this->targetPlatform->prefersIdentityColumns()) {
|
2009-02-05 20:34:44 +03:00
|
|
|
$class->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_IDENTITY);
|
2008-12-18 17:08:11 +03:00
|
|
|
} else {
|
2009-02-05 20:34:44 +03:00
|
|
|
$class->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_TABLE);
|
2008-12-18 17:08:11 +03:00
|
|
|
}
|
|
|
|
}
|
2009-03-30 23:43:05 +04:00
|
|
|
|
|
|
|
// Create & assign an appropriate ID generator instance
|
2009-05-14 14:03:09 +04:00
|
|
|
switch ($class->generatorType) {
|
2009-03-30 23:43:05 +04:00
|
|
|
case ClassMetadata::GENERATOR_TYPE_IDENTITY:
|
2010-05-08 16:08:18 +04:00
|
|
|
// For PostgreSQL IDENTITY (SERIAL) we need a sequence name. It defaults to
|
|
|
|
// <table>_<column>_seq in PostgreSQL for SERIAL columns.
|
|
|
|
// Not pretty but necessary and the simplest solution that currently works.
|
2012-06-06 00:10:44 +04:00
|
|
|
$sequenceName = null;
|
2013-01-20 23:31:22 +04:00
|
|
|
$fieldName = $class->identifier ? $class->getSingleIdentifierFieldName() : null;
|
2012-06-18 22:24:52 +04:00
|
|
|
|
2013-10-21 11:44:48 +04:00
|
|
|
if ($this->targetPlatform instanceof Platforms\PostgreSqlPlatform) {
|
2012-06-06 00:10:44 +04:00
|
|
|
$columnName = $class->getSingleIdentifierColumnName();
|
|
|
|
$quoted = isset($class->fieldMappings[$fieldName]['quoted']) || isset($class->table['quoted']);
|
|
|
|
$sequenceName = $class->getTableName() . '_' . $columnName . '_seq';
|
2012-06-18 22:24:52 +04:00
|
|
|
$definition = array(
|
2012-06-06 00:10:44 +04:00
|
|
|
'sequenceName' => $this->targetPlatform->fixSchemaElementName($sequenceName)
|
|
|
|
);
|
2012-06-18 22:24:52 +04:00
|
|
|
|
2012-06-06 00:10:44 +04:00
|
|
|
if ($quoted) {
|
|
|
|
$definition['quoted'] = true;
|
|
|
|
}
|
2012-06-18 22:24:52 +04:00
|
|
|
|
2012-06-12 03:14:17 +04:00
|
|
|
$sequenceName = $this->em->getConfiguration()->getQuoteStrategy()->getSequenceName($definition, $class, $this->targetPlatform);
|
2012-06-06 00:10:44 +04:00
|
|
|
}
|
2012-06-18 22:24:52 +04:00
|
|
|
|
2012-08-20 05:43:27 +04:00
|
|
|
$generator = ($fieldName && $class->fieldMappings[$fieldName]['type'] === 'bigint')
|
2013-01-20 23:31:22 +04:00
|
|
|
? new BigIntegerIdentityGenerator($sequenceName)
|
|
|
|
: new IdentityGenerator($sequenceName);
|
|
|
|
|
|
|
|
$class->setIdGenerator($generator);
|
|
|
|
|
2009-03-30 23:43:05 +04:00
|
|
|
break;
|
2012-06-23 21:19:47 +04:00
|
|
|
|
2009-03-30 23:43:05 +04:00
|
|
|
case ClassMetadata::GENERATOR_TYPE_SEQUENCE:
|
|
|
|
// If there is no sequence definition yet, create a default definition
|
2010-03-15 20:19:00 +03:00
|
|
|
$definition = $class->sequenceGeneratorDefinition;
|
2012-06-18 22:24:52 +04:00
|
|
|
|
2009-03-30 23:43:05 +04:00
|
|
|
if ( ! $definition) {
|
2012-06-06 00:10:44 +04:00
|
|
|
$fieldName = $class->getSingleIdentifierFieldName();
|
|
|
|
$columnName = $class->getSingleIdentifierColumnName();
|
|
|
|
$quoted = isset($class->fieldMappings[$fieldName]['quoted']) || isset($class->table['quoted']);
|
|
|
|
$sequenceName = $class->getTableName() . '_' . $columnName . '_seq';
|
2012-06-18 22:24:52 +04:00
|
|
|
$definition = array(
|
|
|
|
'sequenceName' => $this->targetPlatform->fixSchemaElementName($sequenceName),
|
|
|
|
'allocationSize' => 1,
|
|
|
|
'initialValue' => 1,
|
|
|
|
);
|
|
|
|
|
2012-06-06 00:10:44 +04:00
|
|
|
if ($quoted) {
|
|
|
|
$definition['quoted'] = true;
|
|
|
|
}
|
2012-06-18 22:24:52 +04:00
|
|
|
|
2009-03-30 23:43:05 +04:00
|
|
|
$class->setSequenceGeneratorDefinition($definition);
|
|
|
|
}
|
2012-06-18 22:24:52 +04:00
|
|
|
|
2009-03-30 23:43:05 +04:00
|
|
|
$sequenceGenerator = new \Doctrine\ORM\Id\SequenceGenerator(
|
2012-06-12 03:14:17 +04:00
|
|
|
$this->em->getConfiguration()->getQuoteStrategy()->getSequenceName($definition, $class, $this->targetPlatform),
|
2009-03-30 23:43:05 +04:00
|
|
|
$definition['allocationSize']
|
|
|
|
);
|
|
|
|
$class->setIdGenerator($sequenceGenerator);
|
|
|
|
break;
|
2012-06-23 21:19:47 +04:00
|
|
|
|
2009-03-30 23:43:05 +04:00
|
|
|
case ClassMetadata::GENERATOR_TYPE_NONE:
|
2010-03-31 01:14:17 +04:00
|
|
|
$class->setIdGenerator(new \Doctrine\ORM\Id\AssignedGenerator());
|
2009-03-30 23:43:05 +04:00
|
|
|
break;
|
2012-06-23 21:19:47 +04:00
|
|
|
|
2011-09-19 20:29:24 +04:00
|
|
|
case ClassMetadata::GENERATOR_TYPE_UUID:
|
|
|
|
$class->setIdGenerator(new \Doctrine\ORM\Id\UuidGenerator());
|
|
|
|
break;
|
2012-06-23 21:19:47 +04:00
|
|
|
|
2009-03-30 23:43:05 +04:00
|
|
|
case ClassMetadata::GENERATOR_TYPE_TABLE:
|
2009-12-09 15:37:57 +03:00
|
|
|
throw new ORMException("TableGenerator not yet implemented.");
|
2009-03-30 23:43:05 +04:00
|
|
|
break;
|
2012-06-23 21:19:47 +04:00
|
|
|
|
2011-11-23 14:06:34 +04:00
|
|
|
case ClassMetadata::GENERATOR_TYPE_CUSTOM:
|
|
|
|
$definition = $class->customGeneratorDefinition;
|
2012-06-23 21:19:47 +04:00
|
|
|
if ( ! class_exists($definition['class'])) {
|
2011-11-23 14:51:45 +04:00
|
|
|
throw new ORMException("Can't instantiate custom generator : " .
|
2011-11-23 14:06:34 +04:00
|
|
|
$definition['class']);
|
2011-11-20 17:15:40 +04:00
|
|
|
}
|
2012-01-08 16:20:35 +04:00
|
|
|
$class->setIdGenerator(new $definition['class']);
|
2011-11-23 14:06:34 +04:00
|
|
|
break;
|
2012-06-23 21:19:47 +04:00
|
|
|
|
2009-03-30 23:43:05 +04:00
|
|
|
default:
|
2009-12-09 15:37:57 +03:00
|
|
|
throw new ORMException("Unknown generator type: " . $class->generatorType);
|
2009-03-30 23:43:05 +04:00
|
|
|
}
|
2008-02-04 00:29:57 +03:00
|
|
|
}
|
2011-09-25 16:41:56 +04:00
|
|
|
|
2012-01-02 18:30:25 +04:00
|
|
|
/**
|
2012-04-07 23:20:17 +04:00
|
|
|
* {@inheritDoc}
|
2012-01-02 18:30:25 +04:00
|
|
|
*/
|
2012-04-07 23:20:17 +04:00
|
|
|
protected function wakeupReflection(ClassMetadataInterface $class, ReflectionService $reflService)
|
2012-01-02 18:30:25 +04:00
|
|
|
{
|
2012-06-09 17:17:45 +04:00
|
|
|
/* @var $class ClassMetadata */
|
2012-04-07 23:20:17 +04:00
|
|
|
$class->wakeupReflection($reflService);
|
2012-01-02 18:30:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-04-07 23:20:17 +04:00
|
|
|
* {@inheritDoc}
|
2012-01-02 18:30:25 +04:00
|
|
|
*/
|
2012-04-07 23:20:17 +04:00
|
|
|
protected function initializeReflection(ClassMetadataInterface $class, ReflectionService $reflService)
|
2012-01-02 18:30:25 +04:00
|
|
|
{
|
2012-06-09 17:17:45 +04:00
|
|
|
/* @var $class ClassMetadata */
|
2012-04-07 23:20:17 +04:00
|
|
|
$class->initializeReflection($reflService);
|
2012-01-02 18:30:25 +04:00
|
|
|
}
|
2012-01-02 18:36:36 +04:00
|
|
|
|
|
|
|
/**
|
2012-04-07 23:20:17 +04:00
|
|
|
* {@inheritDoc}
|
2012-01-02 18:36:36 +04:00
|
|
|
*/
|
2012-04-07 23:20:17 +04:00
|
|
|
protected function getFqcnFromAlias($namespaceAlias, $simpleClassName)
|
2012-01-02 18:36:36 +04:00
|
|
|
{
|
2012-04-07 23:20:17 +04:00
|
|
|
return $this->em->getConfiguration()->getEntityNamespace($namespaceAlias) . '\\' . $simpleClassName;
|
2012-01-02 18:36:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-04-07 23:20:17 +04:00
|
|
|
* {@inheritDoc}
|
2012-01-02 18:36:36 +04:00
|
|
|
*/
|
2012-04-07 23:20:17 +04:00
|
|
|
protected function getDriver()
|
2012-01-02 18:36:36 +04:00
|
|
|
{
|
2012-04-07 23:20:17 +04:00
|
|
|
return $this->driver;
|
|
|
|
}
|
2012-07-07 00:18:06 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
protected function isEntity(ClassMetadataInterface $class)
|
|
|
|
{
|
|
|
|
return isset($class->isMappedSuperclass) && $class->isMappedSuperclass === false;
|
|
|
|
}
|
2009-07-20 16:05:19 +04:00
|
|
|
}
|