Skip to content

Commit

Permalink
Merge branch 'hotfix/#1252-include-class-name-when-calling-naming-str…
Browse files Browse the repository at this point in the history
…ategy-join-column-name'

Close #1252
  • Loading branch information
Ocramius committed Jan 17, 2015
2 parents 4cde35d + 0255650 commit 84562bc
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 21 deletions.
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 @@ -1553,7 +1553,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 @@ -1571,7 +1571,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
4 changes: 3 additions & 1 deletion lib/Doctrine/ORM/Mapping/NamingStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,12 @@ 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.
* This parameter is omitted from the signature due to BC
*
* @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
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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\Tests\ORM\Mapping\NamingStrategy;

use Doctrine\ORM\Mapping\DefaultNamingStrategy;

/**
* Stub naming strategy to verify `joinColumnName` proper behavior
*/
class JoinColumnClassNamingStrategy extends DefaultNamingStrategy
{
/**
* {@inheritdoc}
*/
public function joinColumnName($propertyName, $className = null)
{
return strtolower($this->classToTableName($className))
. '_' . $propertyName
. '_' . $this->referenceColumnName();
}
}
24 changes: 10 additions & 14 deletions tests/Doctrine/Tests/ORM/Mapping/NamingStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Doctrine\ORM\Mapping\UnderscoreNamingStrategy;
use Doctrine\ORM\Mapping\DefaultNamingStrategy;
use Doctrine\ORM\Mapping\NamingStrategy;
use Doctrine\Tests\ORM\Mapping\NamingStrategy\JoinColumnClassNamingStrategy;

/**
* @group DDC-559
Expand Down Expand Up @@ -162,20 +163,15 @@ static public function dataJoinColumnName()
{
return array(
// DefaultNamingStrategy
array(self::defaultNaming(), 'someColumn_id',
'someColumn', null,
),
array(self::defaultNaming(), 'some_column_id',
'some_column', null,
),
array(self::defaultNaming(), 'someColumn_id', 'someColumn', null),
array(self::defaultNaming(), 'some_column_id', 'some_column', null),

// UnderscoreNamingStrategy
array(self::underscoreNamingLower(), 'some_column_id',
'someColumn', null,
),
array(self::underscoreNamingUpper(), 'SOME_COLUMN_ID',
'someColumn', null,
),
array(self::underscoreNamingLower(), 'some_column_id', 'someColumn', null),
array(self::underscoreNamingUpper(), 'SOME_COLUMN_ID', 'someColumn', null),
// JoinColumnClassNamingStrategy
array(new JoinColumnClassNamingStrategy(), 'classname_someColumn_id', 'someColumn', 'Some\ClassName'),
array(new JoinColumnClassNamingStrategy(), 'classname_some_column_id', 'some_column', 'ClassName'),
);
}

Expand All @@ -186,9 +182,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

0 comments on commit 84562bc

Please sign in to comment.