Skip to content

Commit

Permalink
Add SchemaIgnoreClasses property for #8195. (#9202)
Browse files Browse the repository at this point in the history
Co-authored-by: Simon Podlipsky <simon@podlipsky.net>

Co-authored-by: Iab Foulds <ianfoulds@x-act.co.uk>
  • Loading branch information
simPod and ianef authored Dec 12, 2021
1 parent ac5aea1 commit 4219506
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
13 changes: 13 additions & 0 deletions docs/en/reference/advanced-configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,19 @@ That will be available for all entities without a custom repository class.
The default value is ``Doctrine\ORM\EntityRepository``.
Any repository class must be a subclass of EntityRepository otherwise you got an ORMException

Ignoring entities (***OPTIONAL***)
-----------------------------------

Specifies the Entity FQCNs to ignore.
SchemaTool will then skip these (e.g. when comparing schemas).

.. code-block:: php
<?php
$config->setSchemaIgnoreClasses([$fqcn]);
$config->getSchemaIgnoreClasses();
Setting up the Console
----------------------

Expand Down
20 changes: 20 additions & 0 deletions lib/Doctrine/ORM/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -1012,4 +1012,24 @@ public function setDefaultQueryHint($name, $value)
{
$this->_attributes['defaultQueryHints'][$name] = $value;
}

/**
* Gets a list of entity class names to be ignored by the SchemaTool
*
* @return list<class-string>
*/
public function getSchemaIgnoreClasses(): array
{
return $this->_attributes['schemaIgnoreClasses'] ?? [];
}

/**
* Sets a list of entity class names to be ignored by the SchemaTool
*
* @param list<class-string> $schemaIgnoreClasses List of entity class names
*/
public function setSchemaIgnoreClasses(array $schemaIgnoreClasses): void
{
$this->_attributes['schemaIgnoreClasses'] = $schemaIgnoreClasses;
}
}
3 changes: 2 additions & 1 deletion lib/Doctrine/ORM/Tools/SchemaTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ private function processingNotRequired(
return isset($processedClasses[$class->name]) ||
$class->isMappedSuperclass ||
$class->isEmbeddedClass ||
($class->isInheritanceTypeSingleTable() && $class->name !== $class->rootEntityName);
($class->isInheritanceTypeSingleTable() && $class->name !== $class->rootEntityName) ||
in_array($class->name, $this->em->getConfiguration()->getSchemaIgnoreClasses());
}

/**
Expand Down
28 changes: 28 additions & 0 deletions tests/Doctrine/Tests/ORM/Tools/SchemaToolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,34 @@ public function testIncorrectUniqueConstraintsBasedOnFields(): void
$this->expectException(MappingException::class);
$schemaTool->getSchemaFromMetadata([$class]);
}

/**
* @group schema-configuration
*/
public function testConfigurationSchemaIgnoredEntity(): void
{
$em = $this->getTestEntityManager();
$schemaTool = new SchemaTool($em);

$classes = [
$em->getClassMetadata(FirstEntity::class),
$em->getClassMetadata(SecondEntity::class),
];

$schema = $schemaTool->getSchemaFromMetadata($classes);

self::assertTrue($schema->hasTable('first_entity'), 'Table first_entity should exist.');
self::assertTrue($schema->hasTable('second_entity'), 'Table second_entity should exist.');

$em->getConfiguration()->setSchemaIgnoreClasses([
SecondEntity::class,
]);

$schema = $schemaTool->getSchemaFromMetadata($classes);

self::assertTrue($schema->hasTable('first_entity'), 'Table first_entity should exist.');
self::assertFalse($schema->hasTable('second_entity'), 'Table second_entity should not exist.');
}
}

/**
Expand Down

0 comments on commit 4219506

Please sign in to comment.