diff --git a/lib/Doctrine/Common/Cli/CliController.php b/lib/Doctrine/Common/Cli/CliController.php index e78e26bba..027cdcc29 100644 --- a/lib/Doctrine/Common/Cli/CliController.php +++ b/lib/Doctrine/Common/Cli/CliController.php @@ -84,7 +84,7 @@ class CliController extends AbstractNamespace ->addTask('schema-tool', $ns . '\SchemaToolTask') ->addTask('version', $ns . '\VersionTask') ->addTask('convert-d1-schema', $ns . '\ConvertDoctrine1SchemaTask') - ->addTask('generate-entity-stubs', $ns . '\GenerateEntityStubsTask'); + ->addTask('generate-entities', $ns . '\GenerateEntitiesTask'); $ns = 'Doctrine\DBAL\Tools\Cli\Tasks'; $this->addNamespace('Dbal') diff --git a/lib/Doctrine/ORM/Tools/Cli/Tasks/GenerateEntityStubsTask.php b/lib/Doctrine/ORM/Tools/Cli/Tasks/GenerateEntitiesTask.php similarity index 96% rename from lib/Doctrine/ORM/Tools/Cli/Tasks/GenerateEntityStubsTask.php rename to lib/Doctrine/ORM/Tools/Cli/Tasks/GenerateEntitiesTask.php index ea5712b2a..3e9bbd0e6 100644 --- a/lib/Doctrine/ORM/Tools/Cli/Tasks/GenerateEntityStubsTask.php +++ b/lib/Doctrine/ORM/Tools/Cli/Tasks/GenerateEntitiesTask.php @@ -40,7 +40,7 @@ use Doctrine\Common\Cli\Tasks\AbstractTask, * @author Jonathan Wage * @author Roman Borschel */ -class GenerateEntityStubsTask extends ConvertMappingTask +class GenerateEntitiesTask extends ConvertMappingTask { /** * @inheritdoc @@ -53,7 +53,7 @@ class GenerateEntityStubsTask extends ConvertMappingTask )); $doc = $this->getDocumentation(); - $doc->setName('generate-entity-stubs') + $doc->setName('generate-entities') ->setDescription('Generate entity classes and method stubs from your mapping information.') ->getOptionGroup() ->addOption($options); @@ -82,7 +82,7 @@ class GenerateEntityStubsTask extends ConvertMappingTask $dest = realpath($arguments['dest']); $generator = new EntityGenerator(); - $generator->setGenerateAnnotations(true); + $generator->setGenerateAnnotations(false); $generator->setGenerateStubMethods(true); $generator->setRegenerateEntityIfExists(false); $generator->setUpdateEntityIfExists(true); diff --git a/lib/Doctrine/ORM/Tools/EntityGenerator.php b/lib/Doctrine/ORM/Tools/EntityGenerator.php index 50b980b1e..4133b52a2 100644 --- a/lib/Doctrine/ORM/Tools/EntityGenerator.php +++ b/lib/Doctrine/ORM/Tools/EntityGenerator.php @@ -54,8 +54,8 @@ class EntityGenerator /** Whether or not the current ClassMetadataInfo instance is new or old */ private $_isNew; - /** If $_isNew is false then current code contains the current code from disk */ - private $_currentCode; + /** If isNew is false then this variable contains instance of ReflectionClass for current entity */ + private $_reflection; /** Number of spaces to use for indention in generated code */ private $_numSpaces = 4; @@ -119,6 +119,11 @@ class EntityGenerator $this->_isNew = ! file_exists($path); + if ( ! $this->_isNew) { + require_once $path; + $this->_reflection = new \ReflectionClass($metadata->name); + } + // If entity doesn't exist or we're re-generating the entities entirely if ($this->_isNew || ( ! $this->_isNew && $this->_regenerateEntityIfExists)) { file_put_contents($path, $this->generateEntityClass($metadata)); @@ -166,12 +171,12 @@ class EntityGenerator */ public function generateUpdatedEntityClass(ClassMetadataInfo $metadata, $path) { - $this->_currentCode = file_get_contents($path); + $currentCode = file_get_contents($path); $body = $this->_generateEntityBody($metadata); - $last = strrpos($this->_currentCode, '}'); - $code = substr($this->_currentCode, 0, $last) . $body . '}'; + $last = strrpos($currentCode, '}'); + $code = substr($currentCode, 0, $last) . $body . '}'; return $code; } @@ -300,7 +305,7 @@ class EntityGenerator if ($this->_isNew) { return false; } else { - return strpos($this->_currentCode, '$' . $property) !== false ? true : false; + return $this->_reflection->hasProperty($property); } } @@ -309,7 +314,7 @@ class EntityGenerator if ($this->_isNew) { return false; } else { - return strpos($this->_currentCode, 'function ' . $method) !== false ? true : false; + return $this->_reflection->hasMethod($method); } } @@ -394,7 +399,9 @@ class EntityGenerator private function _generateTableAnnotation($metadata) { $table = array(); - $table[] = 'name="' . $metadata->primaryTable['name'] . '"'; + if ($metadata->primaryTable['name']) { + $table[] = 'name="' . $metadata->primaryTable['name'] . '"'; + } if (isset($metadata->primaryTable['schema'])) { $table[] = 'schema="' . $metadata->primaryTable['schema'] . '"'; diff --git a/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php b/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php index c6cda38fc..82cc6eb36 100644 --- a/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php @@ -11,6 +11,17 @@ require_once __DIR__ . '/../../TestInit.php'; class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase { + private $_generator; + + public function setUp() + { + $this->_generator = new EntityGenerator(); + $this->_generator->setGenerateAnnotations(true); + $this->_generator->setGenerateStubMethods(true); + $this->_generator->setRegenerateEntityIfExists(false); + $this->_generator->setUpdateEntityIfExists(true); + } + public function testWriteEntityClass() { $metadata = new ClassMetadataInfo('EntityGeneratorBook'); @@ -33,22 +44,20 @@ class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase )); $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO); - $generator = new EntityGenerator(); - $generator->setGenerateAnnotations(true); - $generator->setGenerateStubMethods(true); - $generator->setRegenerateEntityIfExists(false); - $generator->setUpdateEntityIfExists(true); - $generator->writeEntityClass($metadata, __DIR__); + $this->_generator->writeEntityClass($metadata, __DIR__); $path = __DIR__ . '/EntityGeneratorBook.php'; $this->assertTrue(file_exists($path)); require_once $path; + + return $metadata; } /** * @depends testWriteEntityClass + * @param ClassMetadata $metadata */ - public function testGeneratedEntityClassMethods() + public function testGeneratedEntityClassMethods($metadata) { $this->assertTrue(method_exists('\EntityGeneratorBook', 'getId')); $this->assertTrue(method_exists('\EntityGeneratorBook', 'setName')); @@ -74,6 +83,24 @@ class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase $book->addAuthor('Test'); $this->assertEquals(array('Test'), $book->getAuthor()); + return $metadata; + } + + /** + * @depends testGeneratedEntityClassMethods + * @param ClassMetadata $metadata + */ + public function testEntityUpdatingWorks($metadata) + { + $metadata->mapField(array('fieldName' => 'test', 'type' => 'varchar')); + $this->_generator->writeEntityClass($metadata, __DIR__); + + $code = file_get_contents(__DIR__ . '/EntityGeneratorBook.php'); + $this->assertTrue(strstr($code, 'private $test;') !== false); + $this->assertTrue(strstr($code, 'private $test;') !== false); + $this->assertTrue(strstr($code, 'public function getTest(') !== false); + $this->assertTrue(strstr($code, 'public function setTest(') !== false); + unlink(__DIR__ . '/EntityGeneratorBook.php'); } } \ No newline at end of file