diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php b/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php index 9320d1b6d50..caba0b6374f 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php @@ -78,7 +78,6 @@ public function setEntityManager(EntityManager $em) protected function initialize() { $this->driver = $this->em->getConfiguration()->getMetadataDriverImpl(); - $this->targetPlatform = $this->em->getConnection()->getDatabasePlatform(); $this->evm = $this->em->getEventManager(); $this->initialized = true; } @@ -432,9 +431,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); @@ -450,19 +449,23 @@ private function completeIdGeneratorMapping(ClassMetadataInfo $class) $sequenceName = null; $fieldName = $class->identifier ? $class->getSingleIdentifierFieldName() : null; - if ($this->targetPlatform instanceof Platforms\PostgreSQLPlatform) { + if ($this->getTargetPlatform() instanceof Platforms\PostgreSQLPlatform) { $columnName = $class->getSingleIdentifierColumnName(); $quoted = isset($class->fieldMappings[$fieldName]['quoted']) || isset($class->table['quoted']); $sequenceName = $class->getTableName() . '_' . $columnName . '_seq'; $definition = array( - 'sequenceName' => $this->targetPlatform->fixSchemaElementName($sequenceName) + 'sequenceName' => $this->getTargetPlatform()->fixSchemaElementName($sequenceName) ); if ($quoted) { $definition['quoted'] = true; } - $sequenceName = $this->em->getConfiguration()->getQuoteStrategy()->getSequenceName($definition, $class, $this->targetPlatform); + $sequenceName = $this + ->em + ->getConfiguration() + ->getQuoteStrategy() + ->getSequenceName($definition, $class, $this->getTargetPlatform()); } $generator = ($fieldName && $class->fieldMappings[$fieldName]['type'] === 'bigint') @@ -483,7 +486,7 @@ private function completeIdGeneratorMapping(ClassMetadataInfo $class) $quoted = isset($class->fieldMappings[$fieldName]['quoted']) || isset($class->table['quoted']); $sequenceName = $class->getTableName() . '_' . $columnName . '_seq'; $definition = array( - 'sequenceName' => $this->targetPlatform->fixSchemaElementName($sequenceName), + 'sequenceName' => $this->getTargetPlatform()->fixSchemaElementName($sequenceName), 'allocationSize' => 1, 'initialValue' => 1, ); @@ -496,7 +499,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); @@ -569,4 +572,16 @@ protected function isEntity(ClassMetadataInterface $class) { return isset($class->isMappedSuperclass) && $class->isMappedSuperclass === false; } + + /** + * @return Platforms\AbstractPlatform + */ + private function getTargetPlatform() + { + if (!$this->targetPlatform) { + $this->targetPlatform = $this->em->getConnection()->getDatabasePlatform(); + } + + return $this->targetPlatform; + } } diff --git a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php index 7f07af2adc2..d865a886c60 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php @@ -192,15 +192,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); + + $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);