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

Fix underscore naming strategy behaviour with numbers #7856

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: 20 additions & 13 deletions lib/Doctrine/ORM/Mapping/UnderscoreNamingStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,29 @@

namespace Doctrine\ORM\Mapping;

use const CASE_LOWER;
use const CASE_UPPER;
use function preg_replace;
use function strpos;
use function strrpos;
use function strtolower;
use function strtoupper;
use function substr;

/**
* Naming strategy implementing the underscore naming convention.
* Converts 'MyEntity' to 'my_entity' or 'MY_ENTITY'.
*
*
*
* @link www.doctrine-project.org
* @since 2.3
* @author Fabio B. Silva <fabio.bat.silva@gmail.com>
*/
class UnderscoreNamingStrategy implements NamingStrategy
{
private const DEFAULT_PATTERN = '/(?<=[a-z])([A-Z])/';
private const PATTERN_FOR_PROPERTIES = '/(?<=[a-z0-9])([A-Z])/';

/**
* @var integer
*/
Expand All @@ -57,7 +69,7 @@ public function getCase()
/**
* Sets string case CASE_LOWER | CASE_UPPER.
* Alphabetic characters converted to lowercase or uppercase.
*
*
* @param integer $case
*
* @return void
Expand All @@ -84,7 +96,7 @@ public function classToTableName($className)
*/
public function propertyToColumnName($propertyName, $className = null)
{
return $this->underscore($propertyName);
return $this->underscore($propertyName, self::PATTERN_FOR_PROPERTIES);
}

/**
Expand All @@ -108,7 +120,7 @@ public function referenceColumnName()
*/
public function joinColumnName($propertyName, $className = null)
{
return $this->underscore($propertyName) . '_' . $this->referenceColumnName();
return $this->underscore($propertyName, self::PATTERN_FOR_PROPERTIES) . '_' . $this->referenceColumnName();
}

/**
Expand All @@ -118,7 +130,7 @@ public function joinTableName($sourceEntity, $targetEntity, $propertyName = null
{
return $this->classToTableName($sourceEntity) . '_' . $this->classToTableName($targetEntity);
}

/**
* {@inheritdoc}
*/
Expand All @@ -127,15 +139,10 @@ public function joinKeyColumnName($entityName, $referencedColumnName = null)
return $this->classToTableName($entityName) . '_' .
($referencedColumnName ?: $this->referenceColumnName());
}

/**
* @param string $string
*
* @return string
*/
private function underscore($string)

private function underscore(string $string, string $pattern = self::DEFAULT_PATTERN) : string
{
$string = preg_replace('/(?<=[a-z])([A-Z])/', '_$1', $string);
$string = preg_replace($pattern, '_$1', $string);

if ($this->case === CASE_UPPER) {
return strtoupper($string);
Expand Down
Loading