Skip to content

Commit

Permalink
CS
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermeblanco committed Jul 6, 2015
1 parent 252b9e7 commit 56d19b9
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 76 deletions.
2 changes: 2 additions & 0 deletions lib/Doctrine/ORM/Mapping/ClassMetadata.php
Expand Up @@ -44,6 +44,7 @@
* get the whole class name, namespace inclusive, prepended to every property in
* the serialized representation).
*
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Roman Borschel <roman@code-factory.org>
* @author Jonathan H. Wage <jonwage@gmail.com>
* @since 2.0
Expand Down Expand Up @@ -678,6 +679,7 @@ public function getSingleIdReflectionProperty()
if ($this->isIdentifierComposite) {
throw new BadMethodCallException("Class " . $this->name . " has a composite identifier.");
}

return $this->reflFields[$this->identifier[0]];
}

Expand Down
152 changes: 85 additions & 67 deletions lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php
Expand Up @@ -77,7 +77,7 @@ protected function loadMetadata($name)
{
$loaded = parent::loadMetadata($name);

array_map([$this, 'resolveDiscriminatorValue'], array_map([$this, 'getMetadataFor'], $loaded));
array_map(array($this, 'resolveDiscriminatorValue'), array_map(array($this, 'getMetadataFor'), $loaded));

return $loaded;
}
Expand Down Expand Up @@ -105,7 +105,7 @@ protected function initialize()
*/
protected function onNotFoundMetadata($className)
{
if (! $this->evm->hasListeners(Events::onClassMetadataNotFound)) {
if ( ! $this->evm->hasListeners(Events::onClassMetadataNotFound)) {
return;
}

Expand Down Expand Up @@ -174,9 +174,8 @@ protected function doLoadMetadata($class, $parent, $rootEntityFound, array $nonS
$this->completeIdGeneratorMapping($class);
}

if (!$class->isMappedSuperclass) {
if ( ! $class->isMappedSuperclass) {
foreach ($class->embeddedClasses as $property => $embeddableClass) {

if (isset($embeddableClass['inherited'])) {
continue;
}
Expand Down Expand Up @@ -208,9 +207,7 @@ protected function doLoadMetadata($class, $parent, $rootEntityFound, array $nonS
$class->setPrimaryTable($parent->table);
}

if ($parent) {
$this->addInheritedIndexes($class, $parent);
}
$this->addInheritedIndexes($class, $parent);

if ($parent->cache) {
$class->cache = $parent->cache;
Expand Down Expand Up @@ -245,6 +242,7 @@ protected function doLoadMetadata($class, $parent, $rootEntityFound, array $nonS

if ($this->evm->hasListeners(Events::loadClassMetadata)) {
$eventArgs = new LoadClassMetadataEventArgs($class, $this->em);

$this->evm->dispatchEvent(Events::loadClassMetadata, $eventArgs);
}

Expand All @@ -263,7 +261,7 @@ protected function doLoadMetadata($class, $parent, $rootEntityFound, array $nonS
*/
protected function validateRuntimeMetadata($class, $parent)
{
if ( ! $class->reflClass ) {
if ( ! $class->reflClass) {
// only validate if there is a reflection class instance
return;
}
Expand All @@ -273,11 +271,12 @@ protected function validateRuntimeMetadata($class, $parent)
$class->validateLifecycleCallbacks($this->getReflectionService());

// verify inheritance
if ( ! $class->isMappedSuperclass && !$class->isInheritanceTypeNone()) {
if ( ! $class->isMappedSuperclass && ! $class->isInheritanceTypeNone()) {
if ( ! $parent) {
if (count($class->discriminatorMap) == 0) {
throw MappingException::missingDiscriminatorMap($class->name);
}

if ( ! $class->discriminatorColumn) {
throw MappingException::missingDiscriminatorColumn($class->name);
}
Expand Down Expand Up @@ -308,12 +307,8 @@ protected function newClassMetadataInstance($className)
*/
private function resolveDiscriminatorValue(ClassMetadata $metadata)
{
if ($metadata->discriminatorValue
|| ! $metadata->discriminatorMap
|| $metadata->isMappedSuperclass
|| ! $metadata->reflClass
|| $metadata->reflClass->isAbstract()
) {
if ($metadata->discriminatorValue || ! $metadata->discriminatorMap ||
$metadata->isMappedSuperclass || ! $metadata->reflClass || $metadata->reflClass->isAbstract()) {
return;
}

Expand Down Expand Up @@ -357,8 +352,8 @@ private function addDefaultDiscriminatorMap(ClassMetadata $class)
$allClasses = $this->driver->getAllClassNames();
$fqcn = $class->getName();
$map = array($this->getShortName($class->name) => $fqcn);

$duplicates = array();

foreach ($allClasses as $subClassCandidate) {
if (is_subclass_of($subClassCandidate, $fqcn)) {
$shortName = $this->getShortName($subClassCandidate);
Expand Down Expand Up @@ -409,11 +404,14 @@ private function addInheritedFields(ClassMetadata $subClass, ClassMetadata $pare
if ( ! isset($mapping['inherited']) && ! $parentClass->isMappedSuperclass) {
$mapping['inherited'] = $parentClass->name;
}

if ( ! isset($mapping['declared'])) {
$mapping['declared'] = $parentClass->name;
}

$subClass->addInheritedFieldMapping($mapping);
}

foreach ($parentClass->reflFields as $name => $field) {
$subClass->reflFields[$name] = $field;
}
Expand All @@ -436,16 +434,19 @@ private function addInheritedRelations(ClassMetadata $subClass, ClassMetadata $p
if ($mapping['type'] & ClassMetadata::TO_MANY && !$mapping['isOwningSide']) {
throw MappingException::illegalToManyAssociationOnMappedSuperclass($parentClass->name, $field);
}

$mapping['sourceEntity'] = $subClass->name;
}

//$subclassMapping = $mapping;
if ( ! isset($mapping['inherited']) && ! $parentClass->isMappedSuperclass) {
$mapping['inherited'] = $parentClass->name;
}

if ( ! isset($mapping['declared'])) {
$mapping['declared'] = $parentClass->name;
}

$subClass->addInheritedAssociationMapping($mapping);
}
}
Expand All @@ -456,6 +457,7 @@ private function addInheritedEmbeddedClasses(ClassMetadata $subClass, ClassMetad
if ( ! isset($embeddedClass['inherited']) && ! $parentClass->isMappedSuperclass) {
$embeddedClass['inherited'] = $parentClass->name;
}

if ( ! isset($embeddedClass['declared'])) {
$embeddedClass['declared'] = $parentClass->name;
}
Expand Down Expand Up @@ -502,19 +504,21 @@ private function addNestedEmbeddedClasses(ClassMetadata $subClass, ClassMetadata
*/
private function addInheritedIndexes(ClassMetadata $subClass, ClassMetadata $parentClass)
{
if (! $parentClass->isMappedSuperclass) {
if ( ! $parentClass->isMappedSuperclass) {
return;
}

foreach (array('uniqueConstraints', 'indexes') as $indexType) {
if (isset($parentClass->table[$indexType])) {
foreach ($parentClass->table[$indexType] as $indexName => $index) {
if (isset($subClass->table[$indexType][$indexName])) {
continue; // Let the inheriting table override indices
}
if ( ! isset($parentClass->table[$indexType])) {
continue;
}

$subClass->table[$indexType][$indexName] = $index;
foreach ($parentClass->table[$indexType] as $indexName => $index) {
if (isset($subClass->table[$indexType][$indexName])) {
continue; // Let the inheriting table override indices
}

$subClass->table[$indexType][$indexName] = $index;
}
}
}
Expand All @@ -532,12 +536,14 @@ private function addInheritedIndexes(ClassMetadata $subClass, ClassMetadata $par
private function addInheritedNamedQueries(ClassMetadata $subClass, ClassMetadata $parentClass)
{
foreach ($parentClass->namedQueries as $name => $query) {
if ( ! isset ($subClass->namedQueries[$name])) {
$subClass->addNamedQuery(array(
'name' => $query['name'],
'query' => $query['query']
));
if (isset($subClass->namedQueries[$name])) {
continue;
}

$subClass->addNamedQuery(array(
'name' => $query['name'],
'query' => $query['query']
));
}
}

Expand All @@ -554,15 +560,17 @@ private function addInheritedNamedQueries(ClassMetadata $subClass, ClassMetadata
private function addInheritedNamedNativeQueries(ClassMetadata $subClass, ClassMetadata $parentClass)
{
foreach ($parentClass->namedNativeQueries as $name => $query) {
if ( ! isset ($subClass->namedNativeQueries[$name])) {
$subClass->addNamedNativeQuery(array(
'name' => $query['name'],
'query' => $query['query'],
'isSelfClass' => $query['isSelfClass'],
'resultSetMapping' => $query['resultSetMapping'],
'resultClass' => $query['isSelfClass'] ? $subClass->name : $query['resultClass'],
));
if (isset($subClass->namedNativeQueries[$name])) {
continue;
}

$subClass->addNamedNativeQuery(array(
'name' => $query['name'],
'query' => $query['query'],
'isSelfClass' => $query['isSelfClass'],
'resultSetMapping' => $query['resultSetMapping'],
'resultClass' => $query['isSelfClass'] ? $subClass->name : $query['resultClass'],
));
}
}

Expand All @@ -579,23 +587,26 @@ private function addInheritedNamedNativeQueries(ClassMetadata $subClass, ClassMe
private function addInheritedSqlResultSetMappings(ClassMetadata $subClass, ClassMetadata $parentClass)
{
foreach ($parentClass->sqlResultSetMappings as $name => $mapping) {
if ( ! isset ($subClass->sqlResultSetMappings[$name])) {
$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'],
);
}
if (isset($subClass->sqlResultSetMappings[$name])) {
continue;
}

$entities = array();

$subClass->addSqlResultSetMapping(array(
'name' => $mapping['name'],
'columns' => $mapping['columns'],
'entities' => $entities,
));
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,
));
}
}

Expand All @@ -612,14 +623,16 @@ private function addInheritedSqlResultSetMappings(ClassMetadata $subClass, Class
private function completeIdGeneratorMapping(ClassMetadata $class)
{
$idGenType = $class->generatorType;

if ($idGenType == ClassMetadata::GENERATOR_TYPE_AUTO) {
if ($this->getTargetPlatform()->prefersSequences()) {
$class->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_SEQUENCE);
} else if ($this->getTargetPlatform()->prefersIdentityColumns()) {
$class->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_IDENTITY);
} else {
$class->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_TABLE);
}
$idGenType = $this->getTargetPlatform()->prefersSequences()
? ClassMetadata::GENERATOR_TYPE_SEQUENCE
: ($this->getTargetPlatform()->prefersIdentityColumns()
? ClassMetadata::GENERATOR_TYPE_IDENTITY
: ClassMetadata::GENERATOR_TYPE_TABLE
);

$class->setIdGeneratorType($idGenType);
}

// Create & assign an appropriate ID generator instance
Expand Down Expand Up @@ -667,9 +680,9 @@ private function completeIdGeneratorMapping(ClassMetadata $class)
$quoted = isset($class->fieldMappings[$fieldName]['quoted']) || isset($class->table['quoted']);

$definition = array(
'sequenceName' => $this->getTargetPlatform()->fixSchemaElementName($sequenceName),
'allocationSize' => 1,
'initialValue' => 1,
'sequenceName' => $this->getTargetPlatform()->fixSchemaElementName($sequenceName),
'allocationSize' => 1,
'initialValue' => 1,
);

if ($quoted) {
Expand All @@ -679,10 +692,14 @@ private function completeIdGeneratorMapping(ClassMetadata $class)
$class->setSequenceGeneratorDefinition($definition);
}

$sequenceGenerator = new \Doctrine\ORM\Id\SequenceGenerator(
$this->em->getConfiguration()->getQuoteStrategy()->getSequenceName($definition, $class, $this->getTargetPlatform()),
$definition['allocationSize']
);
$sequenceName = $this
->em
->getConfiguration()
->getQuoteStrategy()
->getSequenceName($definition, $class, $this->getTargetPlatform());

$sequenceGenerator = new \Doctrine\ORM\Id\SequenceGenerator($sequenceName, $definition['allocationSize']);

$class->setIdGenerator($sequenceGenerator);
break;

Expand All @@ -700,10 +717,11 @@ private function completeIdGeneratorMapping(ClassMetadata $class)

case ClassMetadata::GENERATOR_TYPE_CUSTOM:
$definition = $class->customGeneratorDefinition;

if ( ! class_exists($definition['class'])) {
throw new ORMException("Can't instantiate custom generator : " .
$definition['class']);
throw new ORMException("Can't instantiate custom generator : " . $definition['class']);
}

$class->setIdGenerator(new $definition['class']);
break;

Expand Down
11 changes: 3 additions & 8 deletions lib/Doctrine/ORM/Tools/EntityGenerator.php
Expand Up @@ -1159,17 +1159,12 @@ protected function generateEntityStubMethods(ClassMetadata $metadata)
$methods = array();

foreach ($metadata->fieldMappings as $fieldMapping) {
if (isset($fieldMapping['declaredField']) &&
isset($metadata->embeddedClasses[$fieldMapping['declaredField']])
) {
if (isset($fieldMapping['declaredField']) && isset($metadata->embeddedClasses[$fieldMapping['declaredField']])) {
continue;
}

if (( ! isset($fieldMapping['id']) ||
! $fieldMapping['id'] ||
$metadata->generatorType == ClassMetadata::GENERATOR_TYPE_NONE
) && (! $metadata->isEmbeddedClass || ! $this->embeddablesImmutable)
) {
if (( ! isset($fieldMapping['id']) || ! $fieldMapping['id'] || $metadata->generatorType == ClassMetadata::GENERATOR_TYPE_NONE) &&
( ! $metadata->isEmbeddedClass || ! $this->embeddablesImmutable)) {
if ($code = $this->generateEntityStubMethod($metadata, 'set', $fieldMapping['fieldName'], $fieldMapping['type']->getName())) {
$methods[] = $code;
}
Expand Down
Expand Up @@ -451,11 +451,13 @@ private function rebuildOrderByClauseForOuterScope(OrderByClause $orderByClause)

// Get the SQL table alias for the entity and field
$sqlTableAliasForFieldAlias = $dqlAliasToSqlTableAliasMap[$dqlAliasForFieldAlias];

if (isset($fieldMapping['declared']) && $fieldMapping['declared'] !== $class->name) {
// Field was declared in a parent class, so we need to get the proper SQL table alias
// for the joined parent table.
$otherClassMetadata = $this->em->getClassMetadata($fieldMapping['declared']);
if (!$otherClassMetadata->isMappedSuperclass) {

if ( ! $otherClassMetadata->isMappedSuperclass) {
$sqlTableAliasForFieldAlias = $this->getSQLTableAlias($otherClassMetadata->getTableName(), $dqlAliasForFieldAlias);
}
}
Expand Down

0 comments on commit 56d19b9

Please sign in to comment.