Last active
March 14, 2023 11:58
-
-
Save Thinkscape/6713196 to your computer and use it in GitHub Desktop.
Doctrine2 ORM strategy for quoting all identifiers by default.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Doctrine\ORM\Mapping; | |
use Doctrine\ORM\Mapping\ClassMetadata; | |
use Doctrine\DBAL\Platforms\AbstractPlatform; | |
/** | |
* A set of rules for determining the physical column, alias and table quotes | |
*/ | |
class EagerQuoteStrategy implements QuoteStrategy | |
{ | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getColumnName($fieldName, ClassMetadata $class, AbstractPlatform $platform) | |
{ | |
return $platform->quoteIdentifier($class->fieldMappings[$fieldName]['columnName']); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getTableName(ClassMetadata $class, AbstractPlatform $platform) | |
{ | |
return $platform->quoteIdentifier($class->table['name']); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getSequenceName(array $definition, ClassMetadata $class, AbstractPlatform $platform) | |
{ | |
return $platform->quoteIdentifier($definition['sequenceName']); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getJoinColumnName(array $joinColumn, ClassMetadata $class, AbstractPlatform $platform) | |
{ | |
return $platform->quoteIdentifier($joinColumn['name']); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getReferencedJoinColumnName(array $joinColumn, ClassMetadata $class, AbstractPlatform $platform) | |
{ | |
return $platform->quoteIdentifier($joinColumn['referencedColumnName']); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getJoinTableName(array $association, ClassMetadata $class, AbstractPlatform $platform) | |
{ | |
return $platform->quoteIdentifier($association['joinTable']['name']); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getIdentifierColumnNames(ClassMetadata $class, AbstractPlatform $platform) | |
{ | |
$quotedColumnNames = array(); | |
foreach ($class->identifier as $fieldName) { | |
if (isset($class->fieldMappings[$fieldName])) { | |
$quotedColumnNames[] = $this->getColumnName($fieldName, $class, $platform); | |
continue; | |
} | |
// Association defined as Id field | |
$joinColumns = $class->associationMappings[$fieldName]['joinColumns']; | |
$assocQuotedColumnNames = array_map( | |
function ($joinColumn) use ($platform) | |
{ | |
return $platform->quoteIdentifier($joinColumn['name']); | |
}, | |
$joinColumns | |
); | |
$quotedColumnNames = array_merge($quotedColumnNames, $assocQuotedColumnNames); | |
} | |
return $quotedColumnNames; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getColumnAlias($columnName, $counter, AbstractPlatform $platform, ClassMetadata $class = null) | |
{ | |
// 1 ) Concatenate column name and counter | |
// 2 ) Trim the column alias to the maximum identifier length of the platform. | |
// If the alias is to long, characters are cut off from the beginning. | |
// 3 ) Strip non alphanumeric characters | |
// 4 ) Prefix with "_" if the result its numeric | |
$columnName = $columnName . $counter; | |
$columnName = substr($columnName, -$platform->getMaxIdentifierLength()); | |
$columnName = preg_replace('/[^A-Za-z0-9_]/', '', $columnName); | |
$columnName = is_numeric($columnName) ? '_' . $columnName : $columnName; | |
return $platform->getSQLResultCasing($columnName); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fix:
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/basic-mapping.html#quoting-reserved-words