From 1f2ce21b5613d0cc2500946afc9553e514314c34 Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Sun, 27 May 2012 20:31:01 +0200 Subject: [PATCH] [DDC-1497] Change EntityGenerator add method generation for collections. --- UPGRADE_TO_2_3 | 6 ++++++ lib/Doctrine/ORM/Tools/EntityGenerator.php | 9 +++------ tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php | 5 ++--- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/UPGRADE_TO_2_3 b/UPGRADE_TO_2_3 index 5d9564d3b..7f860be93 100644 --- a/UPGRADE_TO_2_3 +++ b/UPGRADE_TO_2_3 @@ -1,3 +1,9 @@ +# EntityGenerator add*() method generation + +When generating an add*() method for a collection the EntityGenerator will now not +use the Type-Hint to get the singular for the collection name, but use the field-name +and strip a trailing "s" character if there is one. + # Merge copies non persisted properties too When merging an entity in UoW not only mapped properties are copied, but also others. diff --git a/lib/Doctrine/ORM/Tools/EntityGenerator.php b/lib/Doctrine/ORM/Tools/EntityGenerator.php index 38c8115c3..65169df45 100644 --- a/lib/Doctrine/ORM/Tools/EntityGenerator.php +++ b/lib/Doctrine/ORM/Tools/EntityGenerator.php @@ -873,12 +873,9 @@ public function () private function generateEntityStubMethod(ClassMetadataInfo $metadata, $type, $fieldName, $typeHint = null, $defaultValue = null) { - if ($type == "add") { - $addMethod = explode("\\", $typeHint); - $addMethod = end($addMethod); - $methodName = $type . $addMethod; - } else { - $methodName = $type . Inflector::classify($fieldName); + $methodName = $type . Inflector::classify($fieldName); + if ($type == "add" && substr($methodName, -1) == "s") { + $methodName = substr($methodName, 0, -1); } if ($this->hasMethod($methodName, $metadata)) { diff --git a/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php b/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php index d614b96a1..9b6c196a0 100644 --- a/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php @@ -115,7 +115,6 @@ class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase $metadata = $this->generateBookEntityFixture(); $book = $this->newInstance($metadata); - $this->assertTrue(class_exists($metadata->name), "Class does not exist."); $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', '__construct'), "EntityGeneratorBook::__construct() missing."); $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'getId'), "EntityGeneratorBook::getId() missing."); @@ -124,7 +123,7 @@ class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'setAuthor'), "EntityGeneratorBook::setAuthor() missing."); $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'getAuthor'), "EntityGeneratorBook::getAuthor() missing."); $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'getComments'), "EntityGeneratorBook::getComments() missing."); - $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'addEntityGeneratorComment'), "EntityGeneratorBook::addEntityGeneratorComment() missing."); + $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'addComment'), "EntityGeneratorBook::addComment() missing."); $this->assertEquals('published', $book->getStatus()); @@ -136,7 +135,7 @@ class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase $this->assertEquals($author, $book->getAuthor()); $comment = new EntityGeneratorComment(); - $book->addEntityGeneratorComment($comment); + $book->addComment($comment); $this->assertInstanceOf('Doctrine\Common\Collections\ArrayCollection', $book->getComments()); $this->assertEquals(new \Doctrine\Common\Collections\ArrayCollection(array($comment)), $book->getComments()); }