diff --git a/lib/Doctrine/ORM/Tools/EntityGenerator.php b/lib/Doctrine/ORM/Tools/EntityGenerator.php index 66d79a837..eae008d7f 100644 --- a/lib/Doctrine/ORM/Tools/EntityGenerator.php +++ b/lib/Doctrine/ORM/Tools/EntityGenerator.php @@ -86,6 +86,8 @@ class EntityGenerator /** Whether or not to re-generate entity class if it exists already */ private $_regenerateEntityIfExists = false; + private $_fieldVisibility = 'private'; + private static $_classTemplate = '() { } -'; +'; public function __construct() { @@ -297,6 +299,22 @@ public function () $this->_generateAnnotations = $bool; } + /** + * Set whether or not to generate annotations for the entity + * + * @param bool $bool + * @return void + */ + public function setFieldVisibility($visibility) + { + if($visibility != 'private' && $visibility != 'protected') + { + throw new \InvalidArgumentException('Invalid provided visibilty (only private and protected are allowed): ' . $visibility); + } + + $this->_fieldVisibility = $visibility; + } + /** * Set an annotation prefix. * @@ -699,7 +717,7 @@ public function () } $lines[] = $this->_generateAssociationMappingPropertyDocBlock($associationMapping, $metadata); - $lines[] = $this->_spaces . 'private $' . $associationMapping['fieldName'] + $lines[] = $this->_spaces . $this->_fieldVisibility . ' $' . $associationMapping['fieldName'] . ($associationMapping['type'] == 'manyToMany' ? ' = array()' : null) . ";\n"; } @@ -717,7 +735,7 @@ public function () } $lines[] = $this->_generateFieldMappingPropertyDocBlock($fieldMapping, $metadata); - $lines[] = $this->_spaces . 'private $' . $fieldMapping['fieldName'] + $lines[] = $this->_spaces . $this->_fieldVisibility . ' $' . $fieldMapping['fieldName'] . (isset($fieldMapping['default']) ? ' = ' . var_export($fieldMapping['default'], true) : null) . ";\n"; } diff --git a/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php b/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php index 4b02a94d1..3b6c1a803 100644 --- a/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php @@ -26,6 +26,7 @@ class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase $this->_generator->setGenerateStubMethods(true); $this->_generator->setRegenerateEntityIfExists(false); $this->_generator->setUpdateEntityIfExists(true); + $this->_generator->setFieldVisibility('protected'); } public function tearDown() @@ -133,7 +134,7 @@ class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase $this->assertTrue($reflClass->hasProperty('id'), "Regenerating keeps property 'id'."); $this->assertTrue($reflClass->hasProperty('test'), "Check for property test failed."); - $this->assertTrue($reflClass->getProperty('test')->isPrivate(), "Check for private property test failed."); + $this->assertTrue($reflClass->getProperty('test')->isProtected(), "Check for private property test failed."); $this->assertTrue($reflClass->hasMethod('getTest'), "Check for method 'getTest' failed."); $this->assertTrue($reflClass->getMethod('getTest')->isPublic(), "Check for public visibility of method 'getTest' failed."); $this->assertTrue($reflClass->hasMethod('setTest'), "Check for method 'getTest' failed.");