DDC-1008, DDC-1002 - Create constructor and id setter if necessary.
This commit is contained in:
parent
f9c1464879
commit
4122abf558
@ -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
|
||||||
*
|
*
|
||||||
@ -345,7 +352,7 @@ public function <methodName>()
|
|||||||
|
|
||||||
private function _generateEntityBody(ClassMetadataInfo $metadata)
|
private function _generateEntityBody(ClassMetadataInfo $metadata)
|
||||||
{
|
{
|
||||||
$fieldMappingProperties = $this->_generateEntityFieldMappingProperties($metadata);
|
$fieldMappingProperties = $this->_generateEntityFieldMappingProperties($metadata);
|
||||||
$associationMappingProperties = $this->_generateEntityAssociationMappingProperties($metadata);
|
$associationMappingProperties = $this->_generateEntityAssociationMappingProperties($metadata);
|
||||||
$stubMethods = $this->_generateEntityStubMethods ? $this->_generateEntityStubMethods($metadata) : null;
|
$stubMethods = $this->_generateEntityStubMethods ? $this->_generateEntityStubMethods($metadata) : null;
|
||||||
$lifecycleCallbackMethods = $this->_generateEntityLifecycleCallbackMethods($metadata);
|
$lifecycleCallbackMethods = $this->_generateEntityLifecycleCallbackMethods($metadata);
|
||||||
@ -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;
|
||||||
}
|
}
|
||||||
|
@ -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()
|
||||||
|
Loading…
Reference in New Issue
Block a user