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

Include className in calls to NamingStrategy joinColumnName method #1252

Closed
wants to merge 4 commits into from
Closed
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
4 changes: 2 additions & 2 deletions docs/en/reference/namingstrategy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ a "naming standard" for database tables and columns.
* @param string $propertyName A property
* @return string A join column name
*/
function joinColumnName($propertyName);
function joinColumnName($propertyName, $className = null);

/**
* Return a join table name
Expand Down Expand Up @@ -124,7 +124,7 @@ You need to implements NamingStrategy first. Following is an example
{
return 'id';
}
public function joinColumnName($propertyName)
public function joinColumnName($propertyName, $className = null)
{
return $propertyName . '_' . $this->referenceColumnName();
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -1552,7 +1552,7 @@ protected function _validateAndCompleteOneToOneMapping(array $mapping)
if ( ! isset($mapping['joinColumns']) || ! $mapping['joinColumns']) {
// Apply default join column
$mapping['joinColumns'] = array(array(
'name' => $this->namingStrategy->joinColumnName($mapping['fieldName']),
'name' => $this->namingStrategy->joinColumnName($mapping['fieldName'], $this->name),
'referencedColumnName' => $this->namingStrategy->referenceColumnName()
));
}
Expand All @@ -1570,7 +1570,7 @@ protected function _validateAndCompleteOneToOneMapping(array $mapping)
}

if (empty($joinColumn['name'])) {
$joinColumn['name'] = $this->namingStrategy->joinColumnName($mapping['fieldName']);
$joinColumn['name'] = $this->namingStrategy->joinColumnName($mapping['fieldName'], $this->name);
}

if (empty($joinColumn['referencedColumnName'])) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/Mapping/DefaultNamingStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function referenceColumnName()
/**
* {@inheritdoc}
*/
public function joinColumnName($propertyName)
public function joinColumnName($propertyName, $className = null)
{
return $propertyName . '_' . $this->referenceColumnName();
}
Expand Down
40 changes: 40 additions & 0 deletions lib/Doctrine/ORM/Mapping/JoinColumnClassNamingStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace Doctrine\ORM\Mapping;

/**
* The default NamingStrategy
*
*
* @link www.doctrine-project.org
* @since 2.3
* @author Fabio B. Silva <[email protected]>
*/
class JoinColumnClassNamingStrategy extends DefaultNamingStrategy
{
/**
* {@inheritdoc}
*/
public function joinColumnName($propertyName, $className = null)
{
return strtolower($this->classToTableName($className)) . '_' . $propertyName . '_' . $this->referenceColumnName();
}
}
3 changes: 2 additions & 1 deletion lib/Doctrine/ORM/Mapping/NamingStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@ function referenceColumnName();
* Returns a join column name for a property.
*
* @param string $propertyName A property name.
* @param string|null $className The fully-qualified class name.
*
* @return string A join column name.
*/
function joinColumnName($propertyName);
function joinColumnName($propertyName, $className = null);

/**
* Returns a join table name.
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/Mapping/UnderscoreNamingStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function referenceColumnName()
/**
* {@inheritdoc}
*/
public function joinColumnName($propertyName)
public function joinColumnName($propertyName, $className = null)
{
return $this->underscore($propertyName) . '_' . $this->referenceColumnName();
}
Expand Down
21 changes: 19 additions & 2 deletions tests/Doctrine/Tests/ORM/Mapping/NamingStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Doctrine\ORM\Mapping\UnderscoreNamingStrategy;
use Doctrine\ORM\Mapping\DefaultNamingStrategy;
use Doctrine\ORM\Mapping\JoinColumnClassNamingStrategy;
use Doctrine\ORM\Mapping\NamingStrategy;

/**
Expand All @@ -19,6 +20,14 @@ static private function defaultNaming()
return new DefaultNamingStrategy();
}

/**
* @return JoinColumnClassNamingStrategy
*/
static private function joinColumnClassNaming()
{
return new JoinColumnClassNamingStrategy();
}

/**
* @return UnderscoreNamingStrategy
*/
Expand Down Expand Up @@ -176,6 +185,14 @@ static public function dataJoinColumnName()
array(self::underscoreNamingUpper(), 'SOME_COLUMN_ID',
'someColumn', null,
),

// JoinColumnClassNamingStrategy
array(self::joinColumnClassNaming(), 'classname_someColumn_id',
'someColumn', 'Some\ClassName',
),
array(self::joinColumnClassNaming(), 'classname_some_column_id',
'some_column', 'ClassName',
),
);
}

Expand All @@ -186,9 +203,9 @@ static public function dataJoinColumnName()
* @param string $expected
* @param string $propertyName
*/
public function testJoinColumnName(NamingStrategy $strategy, $expected, $propertyName)
public function testJoinColumnName(NamingStrategy $strategy, $expected, $propertyName, $className = null)
{
$this->assertEquals($expected, $strategy->joinColumnName($propertyName));
$this->assertEquals($expected, $strategy->joinColumnName($propertyName, $className));
}

/**
Expand Down