1
0
mirror of synced 2024-12-13 22:56: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
}';
private static $_constructorMethodTemplate =
'public function __construct()
{
<spaces><collections>
}
';
/**
* Generate and write entity classes for the given array of ClassMetadataInfo instances
*
@ -345,7 +352,7 @@ public function <methodName>()
private function _generateEntityBody(ClassMetadataInfo $metadata)
{
$fieldMappingProperties = $this->_generateEntityFieldMappingProperties($metadata);
$fieldMappingProperties = $this->_generateEntityFieldMappingProperties($metadata);
$associationMappingProperties = $this->_generateEntityAssociationMappingProperties($metadata);
$stubMethods = $this->_generateEntityStubMethods ? $this->_generateEntityStubMethods($metadata) : null;
$lifecycleCallbackMethods = $this->_generateEntityLifecycleCallbackMethods($metadata);
@ -360,6 +367,8 @@ public function <methodName>()
$code[] = $associationMappingProperties;
}
$code[] = $this->_generateEntityConstructor($metadata);
if ($stubMethods) {
$code[] = $stubMethods;
}
@ -371,6 +380,24 @@ public function <methodName>()
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.
* @param string $path
@ -539,7 +566,7 @@ public function <methodName>()
$methods = array();
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'])) {
$methods[] = $code;
}

View File

@ -91,6 +91,7 @@ class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase
$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.");
$this->assertTrue(method_exists($metadata->namespace . '\EntityGeneratorBook', 'setName'), "EntityGeneratorBook::setName() 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();
$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()