diff --git a/lib/Doctrine/ORM/UnderscoreNamingStrategy.php b/lib/Doctrine/ORM/UnderscoreNamingStrategy.php index 409b4f0ee..b00b15990 100644 --- a/lib/Doctrine/ORM/UnderscoreNamingStrategy.php +++ b/lib/Doctrine/ORM/UnderscoreNamingStrategy.php @@ -19,7 +19,6 @@ */ namespace Doctrine\ORM; -use Doctrine\Common\Util\Inflector; /** * The default NamingStrategy @@ -122,12 +121,12 @@ class UnderscoreNamingStrategy implements NamingStrategy */ private function underscore($string) { - $string = Inflector::tableize($string); + $string = preg_replace('/(?<=[a-z])([A-Z])/', '_$1', $string); if ($this->case == self::CASE_UPPER) { return strtoupper($string); } - return $string; + return strtolower($string); } } \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php index 7a4de4ca4..da591238d 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php @@ -293,6 +293,51 @@ class ClassMetadataTest extends \Doctrine\Tests\OrmTestCase $this->assertEquals('cmsuser_id', $cm->associationMappings['user']['joinTable']['inverseJoinColumns'][0]['name']); } + /** + * @group DDC-559 + */ + public function testUnderscoreNamingStrategyDefaults() + { + $namingStrategy = new \Doctrine\ORM\UnderscoreNamingStrategy(\Doctrine\ORM\UnderscoreNamingStrategy::CASE_UPPER); + $oneToOneMetadata = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsAddress', $namingStrategy); + $manyToManyMetadata = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsAddress', $namingStrategy); + + $oneToOneMetadata->mapOneToOne(array( + 'fieldName' => 'user', + 'targetEntity' => 'CmsUser' + )); + + $manyToManyMetadata->mapManyToMany(array( + 'fieldName' => 'user', + 'targetEntity' => 'CmsUser' + )); + + $this->assertEquals(array('USER_ID'=>'ID'), $oneToOneMetadata->associationMappings['user']['sourceToTargetKeyColumns']); + $this->assertEquals(array('USER_ID'=>'USER_ID'), $oneToOneMetadata->associationMappings['user']['joinColumnFieldNames']); + $this->assertEquals(array('ID'=>'USER_ID'), $oneToOneMetadata->associationMappings['user']['targetToSourceKeyColumns']); + + $this->assertEquals('USER_ID', $oneToOneMetadata->associationMappings['user']['joinColumns'][0]['name']); + $this->assertEquals('ID', $oneToOneMetadata->associationMappings['user']['joinColumns'][0]['referencedColumnName']); + + + $this->assertEquals('CMS_ADDRESS_CMS_USER', $manyToManyMetadata->associationMappings['user']['joinTable']['name']); + + $this->assertEquals(array('CMS_ADDRESS_ID','CMS_USER_ID'), $manyToManyMetadata->associationMappings['user']['joinTableColumns']); + $this->assertEquals(array('CMS_ADDRESS_ID'=>'ID'), $manyToManyMetadata->associationMappings['user']['relationToSourceKeyColumns']); + $this->assertEquals(array('CMS_USER_ID'=>'ID'), $manyToManyMetadata->associationMappings['user']['relationToTargetKeyColumns']); + + $this->assertEquals('CMS_ADDRESS_ID', $manyToManyMetadata->associationMappings['user']['joinTable']['joinColumns'][0]['name']); + $this->assertEquals('CMS_USER_ID', $manyToManyMetadata->associationMappings['user']['joinTable']['inverseJoinColumns'][0]['name']); + + $this->assertEquals('ID', $manyToManyMetadata->associationMappings['user']['joinTable']['joinColumns'][0]['referencedColumnName']); + $this->assertEquals('ID', $manyToManyMetadata->associationMappings['user']['joinTable']['inverseJoinColumns'][0]['referencedColumnName']); + + + $cm = new ClassMetadata('DoctrineGlobal_Article', $namingStrategy); + $cm->mapManyToMany(array('fieldName' => 'author', 'targetEntity' => 'Doctrine\Tests\Models\CMS\CmsUser')); + $this->assertEquals('DOCTRINE_GLOBAL_ARTICLE_CMS_USER', $cm->associationMappings['author']['joinTable']['name']); + } + /** * @group DDC-886 */ diff --git a/tests/Doctrine/Tests/ORM/NamingStrategyTest.php b/tests/Doctrine/Tests/ORM/NamingStrategyTest.php index bf4ab7254..354ea67ff 100644 --- a/tests/Doctrine/Tests/ORM/NamingStrategyTest.php +++ b/tests/Doctrine/Tests/ORM/NamingStrategyTest.php @@ -106,6 +106,12 @@ class NamingStrategyTest extends \Doctrine\Tests\OrmTestCase array(self::underscoreNamingUpper(), 'SOME_PROPERTY', 'someProperty' ), + array(self::underscoreNamingUpper(), 'SOME_PROPERTY', + 'some_property' + ), + array(self::underscoreNamingUpper(), 'SOME_PROPERTY', + 'SOME_PROPERTY' + ), ); }