1
0
mirror of synced 2025-01-18 22:41:43 +03:00

[DDC-1907] Add generation of remove method for collections

This commit is contained in:
Benjamin Eberlei 2012-07-04 21:03:50 +02:00
parent f20a95fdc0
commit 5cdb0ae8be
2 changed files with 21 additions and 1 deletions

View File

@ -219,6 +219,20 @@ public function <methodName>(<methodTypeHint>$<variableName>)
<spaces>$this-><fieldName>[] = $<variableName>;
<spaces>return $this;
}';
/**
* @var string
*/
private static $removeMethodTemplate =
'/**
* <description>
*
* @param <variableType$<variableName>
*/
public function <methodName>(<methodTypeHint>$<variableName>)
{
<spaces>$this-><fieldName>->removeElement($<variableName>);
}';
/**
@ -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);
}

View File

@ -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()