Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid Connection error when calling ClassMetadataFactor::getAllMetadata() #1294

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ public function setEntityManager(EntityManagerInterface $em)
protected function initialize()
{
$this->driver = $this->em->getConfiguration()->getMetadataDriverImpl();
$this->targetPlatform = $this->em->getConnection()->getDatabasePlatform();
$this->evm = $this->em->getEventManager();
$this->initialized = true;
}
Expand Down Expand Up @@ -613,9 +612,9 @@ private function completeIdGeneratorMapping(ClassMetadataInfo $class)
{
$idGenType = $class->generatorType;
if ($idGenType == ClassMetadata::GENERATOR_TYPE_AUTO) {
if ($this->targetPlatform->prefersSequences()) {
if ($this->getTargetPlatform()->prefersSequences()) {
$class->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_SEQUENCE);
} else if ($this->targetPlatform->prefersIdentityColumns()) {
} else if ($this->getTargetPlatform()->prefersIdentityColumns()) {
$class->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_IDENTITY);
} else {
$class->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_TABLE);
Expand All @@ -629,13 +628,13 @@ private function completeIdGeneratorMapping(ClassMetadataInfo $class)
$fieldName = $class->identifier ? $class->getSingleIdentifierFieldName() : null;

// Platforms that do not have native IDENTITY support need a sequence to emulate this behaviour.
if ($this->targetPlatform->usesSequenceEmulatedIdentityColumns()) {
if ($this->getTargetPlatform()->usesSequenceEmulatedIdentityColumns()) {
$columnName = $class->getSingleIdentifierColumnName();
$quoted = isset($class->fieldMappings[$fieldName]['quoted']) || isset($class->table['quoted']);
$sequencePrefix = $class->getSequencePrefix($this->targetPlatform);
$sequenceName = $this->targetPlatform->getIdentitySequenceName($sequencePrefix, $columnName);
$sequencePrefix = $class->getSequencePrefix($this->getTargetPlatform());
$sequenceName = $this->getTargetPlatform()->getIdentitySequenceName($sequencePrefix, $columnName);
$definition = array(
'sequenceName' => $this->targetPlatform->fixSchemaElementName($sequenceName)
'sequenceName' => $this->getTargetPlatform()->fixSchemaElementName($sequenceName)
);

if ($quoted) {
Expand All @@ -646,7 +645,7 @@ private function completeIdGeneratorMapping(ClassMetadataInfo $class)
->em
->getConfiguration()
->getQuoteStrategy()
->getSequenceName($definition, $class, $this->targetPlatform);
->getSequenceName($definition, $class, $this->getTargetPlatform());
}

$generator = ($fieldName && $class->fieldMappings[$fieldName]['type'] === 'bigint')
Expand All @@ -663,11 +662,11 @@ private function completeIdGeneratorMapping(ClassMetadataInfo $class)

if ( ! $definition) {
$fieldName = $class->getSingleIdentifierFieldName();
$sequenceName = $class->getSequenceName($this->targetPlatform);
$sequenceName = $class->getSequenceName($this->getTargetPlatform());
$quoted = isset($class->fieldMappings[$fieldName]['quoted']) || isset($class->table['quoted']);

$definition = array(
'sequenceName' => $this->targetPlatform->fixSchemaElementName($sequenceName),
'sequenceName' => $this->getTargetPlatform()->fixSchemaElementName($sequenceName),
'allocationSize' => 1,
'initialValue' => 1,
);
Expand All @@ -680,7 +679,7 @@ private function completeIdGeneratorMapping(ClassMetadataInfo $class)
}

$sequenceGenerator = new \Doctrine\ORM\Id\SequenceGenerator(
$this->em->getConfiguration()->getQuoteStrategy()->getSequenceName($definition, $class, $this->targetPlatform),
$this->em->getConfiguration()->getQuoteStrategy()->getSequenceName($definition, $class, $this->getTargetPlatform()),
$definition['allocationSize']
);
$class->setIdGenerator($sequenceGenerator);
Expand Down Expand Up @@ -753,4 +752,16 @@ protected function isEntity(ClassMetadataInterface $class)
{
return isset($class->isMappedSuperclass) && $class->isMappedSuperclass === false;
}

/**
* @return Platforms\AbstractPlatform
*/
private function getTargetPlatform()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a simple DocBlock here with return type, please? Would help the IDE I suppose...

{
if (!$this->targetPlatform) {
$this->targetPlatform = $this->em->getConnection()->getDatabasePlatform();
}

return $this->targetPlatform;
}
}
29 changes: 26 additions & 3 deletions tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,15 +194,38 @@ public function testAddDefaultDiscriminatorMap()
$rootMetadata = $cmf->getMetadataFor('Doctrine\Tests\Models\JoinedInheritanceType\RootClass');
}

protected function _createEntityManager($metadataDriver)
public function testGetAllMetadataWorksWithBadConnection()
{
// DDC-3551
$conn = $this->getMockBuilder('Doctrine\DBAL\Connection')
->disableOriginalConstructor()
->getMock();
$mockDriver = new MetadataDriverMock();
$em = $this->_createEntityManager($mockDriver, $conn);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This $mockDriver was never actually used in this function. So while it's unrelated to my change, I removed it.

$conn->expects($this->any())
->method('getDatabasePlatform')
->will($this->throwException(new \Exception('Exception thrown in test when calling getDatabasePlatform')));

$cmf = new ClassMetadataFactory();
$cmf->setEntityManager($em);

// getting all the metadata should work, even if get DatabasePlatform blows up
$metadata = $cmf->getAllMetadata();
// this will just be an empty array - there was no error
$this->assertEquals(array(), $metadata);
}

protected function _createEntityManager($metadataDriver, $conn = null)
{
$driverMock = new DriverMock();
$config = new \Doctrine\ORM\Configuration();
$config->setProxyDir(__DIR__ . '/../../Proxies');
$config->setProxyNamespace('Doctrine\Tests\Proxies');
$eventManager = new EventManager();
$conn = new ConnectionMock(array(), $driverMock, $config, $eventManager);
$mockDriver = new MetadataDriverMock();
if (!$conn) {
$conn = new ConnectionMock(array(), $driverMock, $config, $eventManager);
}
$config->setMetadataDriverImpl($metadataDriver);

return EntityManagerMock::create($conn, $config, $eventManager);
Expand Down