diff --git a/lib/Doctrine/ORM/Tools/EntityGenerator.php b/lib/Doctrine/ORM/Tools/EntityGenerator.php index 2c542d01a..5f404c0da 100644 --- a/lib/Doctrine/ORM/Tools/EntityGenerator.php +++ b/lib/Doctrine/ORM/Tools/EntityGenerator.php @@ -139,6 +139,13 @@ public function () // Add your code here }'; + private static $_constructorMethodTemplate = +'public function __construct() +{ + +} +'; + /** * Generate and write entity classes for the given array of ClassMetadataInfo instances * @@ -345,7 +352,7 @@ public function () 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 () $code[] = $associationMappingProperties; } + $code[] = $this->_generateEntityConstructor($metadata); + if ($stubMethods) { $code[] = $stubMethods; } @@ -371,6 +380,24 @@ public function () 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("", 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 () $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; } diff --git a/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php b/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php index 835fcd44b..c8960f6a7 100644 --- a/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php @@ -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()