1
0
mirror of synced 2024-12-14 15:16:04 +03:00

DDC-1008, DDC-1002 - Create constructor and id setter if necessary.

This commit is contained in:
Benjamin Eberlei 2011-02-02 23:30:16 +01:00
parent f9c1464879
commit 4122abf558
2 changed files with 32 additions and 3 deletions

View File

@ -139,6 +139,13 @@ public function <methodName>()
<spaces>// Add your code here <spaces>// Add your code here
}'; }';
private static $_constructorMethodTemplate =
'public function __construct()
{
<spaces><collections>
}
';
/** /**
* Generate and write entity classes for the given array of ClassMetadataInfo instances * Generate and write entity classes for the given array of ClassMetadataInfo instances
* *
@ -360,6 +367,8 @@ public function <methodName>()
$code[] = $associationMappingProperties; $code[] = $associationMappingProperties;
} }
$code[] = $this->_generateEntityConstructor($metadata);
if ($stubMethods) { if ($stubMethods) {
$code[] = $stubMethods; $code[] = $stubMethods;
} }
@ -371,6 +380,24 @@ public function <methodName>()
return implode("\n", $code); return implode("\n", $code);
} }
private function _generateEntityConstructor(ClassMetadataInfo $metadata)
{
if ($this->_hasMethod('__construct', $metadata)) {
return '';
}
$collections = array();
foreach ($metadata->associationMappings AS $mapping) {
if ($mapping['type'] & ClassMetadataInfo::TO_MANY) {
$collections[] = '$this->'.$mapping['fieldName'].' = new \Doctrine\Common\Collections\ArrayCollection();';
}
}
if ($collections) {
return $this->_prefixCodeWithSpaces(str_replace("<collections>", implode("\n", $collections), self::$_constructorMethodTemplate));
}
return '';
}
/** /**
* @todo this won't work if there is a namespace in brackets and a class outside of it. * @todo this won't work if there is a namespace in brackets and a class outside of it.
* @param string $path * @param string $path
@ -539,7 +566,7 @@ public function <methodName>()
$methods = array(); $methods = array();
foreach ($metadata->fieldMappings as $fieldMapping) { foreach ($metadata->fieldMappings as $fieldMapping) {
if ( ! isset($fieldMapping['id']) || ! $fieldMapping['id']) { if ( ! isset($fieldMapping['id']) || ! $fieldMapping['id'] || $metadata->generatorType == ClassMetadataInfo::GENERATOR_TYPE_NONE) {
if ($code = $this->_generateEntityStubMethod($metadata, 'set', $fieldMapping['fieldName'], $fieldMapping['type'])) { if ($code = $this->_generateEntityStubMethod($metadata, 'set', $fieldMapping['fieldName'], $fieldMapping['type'])) {
$methods[] = $code; $methods[] = $code;
} }

View File

@ -91,6 +91,7 @@ class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase
$book = $this->newInstance($metadata); $book = $this->newInstance($metadata);
$this->assertTrue(class_exists($metadata->name), "Class does not exist."); $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."); $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'getId'), "EntityGeneratorBook::getId() missing.");
$this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'setName'), "EntityGeneratorBook::setName() missing."); $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'setName'), "EntityGeneratorBook::setName() missing.");
$this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'getName'), "EntityGeneratorBook::getName() missing."); $this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'getName'), "EntityGeneratorBook::getName() missing.");
@ -110,7 +111,8 @@ class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase
$comment = new EntityGeneratorComment(); $comment = new EntityGeneratorComment();
$book->addComments($comment); $book->addComments($comment);
$this->assertEquals(array($comment), $book->getComments()); $this->assertInstanceOf('Doctrine\Common\Collections\ArrayCollection', $book->getComments());
$this->assertEquals(new \Doctrine\Common\Collections\ArrayCollection(array($comment)), $book->getComments());
} }
public function testEntityUpdatingWorks() public function testEntityUpdatingWorks()