-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11776 from curry684/issue-9558
Respect referencedColumnName defaults in custom naming strategies
- Loading branch information
Showing
9 changed files
with
226 additions
and
4 deletions.
There are no files selected for viewing
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
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
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
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
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
101 changes: 101 additions & 0 deletions
101
tests/Tests/ORM/Mapping/NamingStrategy/CustomPascalNamingStrategy.php
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Tests\ORM\Mapping\NamingStrategy; | ||
|
||
use Doctrine\ORM\Mapping\NamingStrategy; | ||
|
||
/** | ||
* Fully customized naming strategy changing all namings to a PascalCase model. Included to test some behaviours | ||
* regarding fully custom naming strategies. | ||
*/ | ||
class CustomPascalNamingStrategy implements NamingStrategy | ||
{ | ||
/** | ||
* Returns a table name for an entity class. | ||
* | ||
* @param string $className The fully-qualified class name | ||
* @return string A table name | ||
*/ | ||
public function classToTableName(string $className): string | ||
{ | ||
if (str_contains($className, '\\')) { | ||
return substr($className, strrpos($className, '\\') + 1); | ||
} | ||
|
||
return $className; | ||
} | ||
|
||
/** | ||
* Returns a column name for a property. | ||
* | ||
* @param string $propertyName A property name | ||
* @param string|null $className The fully-qualified class name | ||
* | ||
* @return string A column name | ||
*/ | ||
public function propertyToColumnName(string $propertyName, ?string $className = null): string | ||
{ | ||
if (null !== $className && strtolower($propertyName) == strtolower($this->classToTableName($className)) . 'id') { | ||
return 'Id'; | ||
} | ||
|
||
return ucfirst($propertyName); | ||
} | ||
|
||
/** | ||
* Returns a column name for an embedded property. | ||
*/ | ||
public function embeddedFieldToColumnName(string $propertyName, string $embeddedColumnName, ?string $className = null, $embeddedClassName = null): string | ||
{ | ||
throw new \LogicException(sprintf('Method %s is not implemented', __METHOD__)); | ||
} | ||
|
||
/** | ||
* Returns the default reference column name. | ||
* | ||
* @return string A column name | ||
*/ | ||
public function referenceColumnName(): string | ||
{ | ||
return 'Id'; | ||
} | ||
|
||
/** | ||
* Returns a join column name for a property. | ||
* | ||
* @return string A join column name | ||
*/ | ||
public function joinColumnName(string $propertyName, string $className): string | ||
{ | ||
return ucfirst($propertyName) . $this->referenceColumnName(); | ||
} | ||
|
||
/** | ||
* Returns a join table name. | ||
* | ||
* @param string $sourceEntity The source entity | ||
* @param string $targetEntity The target entity | ||
* @param string|null $propertyName A property name | ||
* | ||
* @return string A join table name | ||
*/ | ||
public function joinTableName(string $sourceEntity, string $targetEntity, ?string $propertyName = null): string | ||
{ | ||
return $this->classToTableName($sourceEntity) . $this->classToTableName($targetEntity); | ||
} | ||
|
||
/** | ||
* Returns the foreign key column name for the given parameters. | ||
* | ||
* @param string $entityName An entity | ||
* @param string|null $referencedColumnName A property | ||
* | ||
* @return string A join column name | ||
*/ | ||
public function joinKeyColumnName(string $entityName, ?string $referencedColumnName = null): string | ||
{ | ||
return $this->classToTableName($entityName) . ($referencedColumnName ?: $this->referenceColumnName()); | ||
} | ||
} |
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
16 changes: 16 additions & 0 deletions
16
tests/Tests/ORM/Mapping/xml/Doctrine.Tests.ORM.Mapping.BlogPost.dcm.xml
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping | ||
https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd"> | ||
|
||
<entity name="Doctrine\Tests\ORM\Mapping\BlogPost"> | ||
|
||
<id name="id" type="integer" column="id"> | ||
<generator strategy="NONE"/> | ||
</id> | ||
|
||
</entity> | ||
|
||
</doctrine-mapping> |
22 changes: 22 additions & 0 deletions
22
tests/Tests/ORM/Mapping/xml/Doctrine.Tests.ORM.Mapping.BlogPostComment.dcm.xml
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping | ||
https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd"> | ||
|
||
<entity name="Doctrine\Tests\ORM\Mapping\BlogPostComment"> | ||
|
||
<id name="id" type="integer"> | ||
<generator strategy="NONE"/> | ||
</id> | ||
|
||
<many-to-one field="blogPost" target-entity="Doctrine\Tests\ORM\Mapping\BlogPost"> | ||
<join-columns> | ||
<join-column nullable="false"/> | ||
</join-columns> | ||
</many-to-one> | ||
|
||
</entity> | ||
|
||
</doctrine-mapping> |