1
0
mirror of synced 2025-02-02 13:31:45 +03:00

Only getting the target platform when it's *actually* needed to avoid errors

initialize() is called sometimes, even when the following code doesn't need
the targetPlatform property. Specifically, in AbstractClassMetadataFactory::getAllMetadata().

But as of DBAL 2.5.0, calling Connection::getDatabasePlatform() will make a
connection to the database, which means that sometimes it may fail (e.g. you
haven't configured your database yet). As a result, calling a method like
AbstractClassMetadataFactory::getAllMetadata() - which does not need the
targetPlatform - will fail, because determining the targetPlatform requires
a connection, which fails.

This avoids that - we only get the targetPlatform *when* we need it, which
are cases where we're doing things that do indeed need a connection.
This commit is contained in:
Ryan Weaver 2015-01-28 20:10:22 -05:00 committed by Marco Pivetta
parent 97afe00d0f
commit 52b3e21969

View File

@ -78,7 +78,6 @@ class ClassMetadataFactory extends AbstractClassMetadataFactory
protected function initialize() protected function initialize()
{ {
$this->driver = $this->em->getConfiguration()->getMetadataDriverImpl(); $this->driver = $this->em->getConfiguration()->getMetadataDriverImpl();
$this->targetPlatform = $this->em->getConnection()->getDatabasePlatform();
$this->evm = $this->em->getEventManager(); $this->evm = $this->em->getEventManager();
$this->initialized = true; $this->initialized = true;
} }
@ -432,9 +431,9 @@ class ClassMetadataFactory extends AbstractClassMetadataFactory
{ {
$idGenType = $class->generatorType; $idGenType = $class->generatorType;
if ($idGenType == ClassMetadata::GENERATOR_TYPE_AUTO) { if ($idGenType == ClassMetadata::GENERATOR_TYPE_AUTO) {
if ($this->targetPlatform->prefersSequences()) { if ($this->getTargetPlatform()->prefersSequences()) {
$class->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_SEQUENCE); $class->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_SEQUENCE);
} else if ($this->targetPlatform->prefersIdentityColumns()) { } else if ($this->getTargetPlatform()->prefersIdentityColumns()) {
$class->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_IDENTITY); $class->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_IDENTITY);
} else { } else {
$class->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_TABLE); $class->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_TABLE);
@ -450,19 +449,25 @@ class ClassMetadataFactory extends AbstractClassMetadataFactory
$sequenceName = null; $sequenceName = null;
$fieldName = $class->identifier ? $class->getSingleIdentifierFieldName() : null; $fieldName = $class->identifier ? $class->getSingleIdentifierFieldName() : null;
if ($this->targetPlatform instanceof Platforms\PostgreSQLPlatform) { // Platforms that do not have native IDENTITY support need a sequence to emulate this behaviour.
if ($this->getTargetPlatform()->usesSequenceEmulatedIdentityColumns()) {
$columnName = $class->getSingleIdentifierColumnName(); $columnName = $class->getSingleIdentifierColumnName();
$quoted = isset($class->fieldMappings[$fieldName]['quoted']) || isset($class->table['quoted']); $quoted = isset($class->fieldMappings[$fieldName]['quoted']) || isset($class->table['quoted']);
$sequenceName = $class->getTableName() . '_' . $columnName . '_seq'; $sequencePrefix = $class->getSequencePrefix($this->getTargetPlatform());
$sequenceName = $this->getTargetPlatform()->getIdentitySequenceName($sequencePrefix, $columnName);
$definition = array( $definition = array(
'sequenceName' => $this->targetPlatform->fixSchemaElementName($sequenceName) 'sequenceName' => $this->getTargetPlatform()->fixSchemaElementName($sequenceName)
); );
if ($quoted) { if ($quoted) {
$definition['quoted'] = true; $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') $generator = ($fieldName && $class->fieldMappings[$fieldName]['type'] === 'bigint')
@ -479,11 +484,11 @@ class ClassMetadataFactory extends AbstractClassMetadataFactory
if ( ! $definition) { if ( ! $definition) {
$fieldName = $class->getSingleIdentifierFieldName(); $fieldName = $class->getSingleIdentifierFieldName();
$columnName = $class->getSingleIdentifierColumnName(); $sequenceName = $class->getSequenceName($this->getTargetPlatform());
$quoted = isset($class->fieldMappings[$fieldName]['quoted']) || isset($class->table['quoted']); $quoted = isset($class->fieldMappings[$fieldName]['quoted']) || isset($class->table['quoted']);
$sequenceName = $class->getTableName() . '_' . $columnName . '_seq';
$definition = array( $definition = array(
'sequenceName' => $this->targetPlatform->fixSchemaElementName($sequenceName), 'sequenceName' => $this->getTargetPlatform()->fixSchemaElementName($sequenceName),
'allocationSize' => 1, 'allocationSize' => 1,
'initialValue' => 1, 'initialValue' => 1,
); );
@ -496,7 +501,7 @@ class ClassMetadataFactory extends AbstractClassMetadataFactory
} }
$sequenceGenerator = new \Doctrine\ORM\Id\SequenceGenerator( $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'] $definition['allocationSize']
); );
$class->setIdGenerator($sequenceGenerator); $class->setIdGenerator($sequenceGenerator);
@ -569,4 +574,13 @@ class ClassMetadataFactory extends AbstractClassMetadataFactory
{ {
return isset($class->isMappedSuperclass) && $class->isMappedSuperclass === false; return isset($class->isMappedSuperclass) && $class->isMappedSuperclass === false;
} }
private function getTargetPlatform()
{
if ($this->targetPlatform === null) {
$this->targetPlatform = $this->em->getConnection()->getDatabasePlatform();
}
return $this->targetPlatform;
}
} }