From 5cdb0ae8beaf29d924f0362341d4965cca0f1dbe Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Wed, 4 Jul 2012 21:03:50 +0200 Subject: [PATCH] [DDC-1907] Add generation of remove method for collections --- lib/Doctrine/ORM/Tools/EntityGenerator.php | 19 ++++++++++++++++++- .../Tests/ORM/Tools/EntityGeneratorTest.php | 3 +++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/Doctrine/ORM/Tools/EntityGenerator.php b/lib/Doctrine/ORM/Tools/EntityGenerator.php index 3092a090d..889b57879 100644 --- a/lib/Doctrine/ORM/Tools/EntityGenerator.php +++ b/lib/Doctrine/ORM/Tools/EntityGenerator.php @@ -219,6 +219,20 @@ public function ($) $this->[] = $; return $this; +}'; + + /** + * @var string + */ + private static $removeMethodTemplate = +'/** + * + * + * @param + */ +public function ($) +{ +$this->->removeElement($); }'; /** @@ -794,6 +808,9 @@ public function __construct() if ($code = $this->generateEntityStubMethod($metadata, 'add', $associationMapping['fieldName'], $associationMapping['targetEntity'])) { $methods[] = $code; } + if ($code = $this->generateEntityStubMethod($metadata, 'remove', $associationMapping['fieldName'], $associationMapping['targetEntity'])) { + $methods[] = $code; + } if ($code = $this->generateEntityStubMethod($metadata, 'get', $associationMapping['fieldName'], 'Doctrine\Common\Collections\Collection')) { $methods[] = $code; } @@ -879,7 +896,7 @@ public function __construct() private function generateEntityStubMethod(ClassMetadataInfo $metadata, $type, $fieldName, $typeHint = null, $defaultValue = null) { $methodName = $type . Inflector::classify($fieldName); - if ($type == "add" && substr($methodName, -1) == "s") { + if (in_array($type, array("add", "remove")) && substr($methodName, -1) == "s") { $methodName = substr($methodName, 0, -1); } diff --git a/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php b/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php index 9b6c196a0..96d078c02 100644 --- a/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php @@ -124,6 +124,7 @@ class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase $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', 'addComment'), "EntityGeneratorBook::addComment() missing."); + $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'removeComment'), "EntityGeneratorBook::removeComment() missing."); $this->assertEquals('published', $book->getStatus()); @@ -138,6 +139,8 @@ class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase $book->addComment($comment); $this->assertInstanceOf('Doctrine\Common\Collections\ArrayCollection', $book->getComments()); $this->assertEquals(new \Doctrine\Common\Collections\ArrayCollection(array($comment)), $book->getComments()); + $book->removeComment($comment); + $this->assertEquals(new \Doctrine\Common\Collections\ArrayCollection(array()), $book->getComments()); } public function testEntityUpdatingWorks()