Skip to content

Commit

Permalink
Merge branch 'master' of github.com:doctrine/doctrine2
Browse files Browse the repository at this point in the history
  • Loading branch information
beberlei committed Nov 13, 2011
2 parents bb85372 + e9068a1 commit 6b0cd7b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
10 changes: 9 additions & 1 deletion lib/Doctrine/ORM/EntityRepository.php
Expand Up @@ -225,6 +225,14 @@ protected function getEntityName()
return $this->_entityName;
}

/**
* @return string
*/
public function getClassName()
{
return $this->getEntityName();
}

/**
* @return EntityManager
*/
Expand All @@ -240,4 +248,4 @@ protected function getClassMetadata()
{
return $this->_class;
}
}
}
33 changes: 28 additions & 5 deletions lib/Doctrine/ORM/Tools/EntityGenerator.php
Expand Up @@ -117,7 +117,7 @@ public function <methodName>()
* @param <variableType>$<variableName>
* @return <entity>
*/
public function <methodName>(<methodTypeHint>$<variableName>)
public function <methodName>(<methodTypeHint>$<variableName><variableDefault>)
{
<spaces>$this-><fieldName> = $<variableName>;
<spaces>return $this;
Expand Down Expand Up @@ -406,7 +406,7 @@ private function _generateEntityConstructor(ClassMetadataInfo $metadata)
}

if ($collections) {
return $this->_prefixCodeWithSpaces(str_replace("<collections>", implode("\n", $collections), self::$_constructorMethodTemplate));
return $this->_prefixCodeWithSpaces(str_replace("<collections>", implode("\n".$this->_spaces, $collections), self::$_constructorMethodTemplate));
}

return '';
Expand Down Expand Up @@ -634,7 +634,8 @@ private function _generateEntityStubMethods(ClassMetadataInfo $metadata)

foreach ($metadata->associationMappings as $associationMapping) {
if ($associationMapping['type'] & ClassMetadataInfo::TO_ONE) {
if ($code = $this->_generateEntityStubMethod($metadata, 'set', $associationMapping['fieldName'], $associationMapping['targetEntity'])) {
$nullable = $this->_isAssociationIsNullable($associationMapping) ? 'null' : null;
if ($code = $this->_generateEntityStubMethod($metadata, 'set', $associationMapping['fieldName'], $associationMapping['targetEntity'], $nullable)) {
$methods[] = $code;
}
if ($code = $this->_generateEntityStubMethod($metadata, 'get', $associationMapping['fieldName'], $associationMapping['targetEntity'])) {
Expand All @@ -653,6 +654,22 @@ private function _generateEntityStubMethods(ClassMetadataInfo $metadata)
return implode("\n\n", $methods);
}

private function _isAssociationIsNullable($associationMapping)
{
if (isset($associationMapping['joinColumns'])) {
$joinColumns = $associationMapping['joinColumns'];
} else {
//@todo thereis no way to retreive targetEntity metadata
$joinColumns = array();
}
foreach ($joinColumns as $joinColumn) {
if(isset($joinColumn['nullable']) && !$joinColumn['nullable']) {
return false;
}
}
return true;
}

private function _generateEntityLifecycleCallbackMethods(ClassMetadataInfo $metadata)
{
if (isset($metadata->lifecycleCallbacks) && $metadata->lifecycleCallbacks) {
Expand Down Expand Up @@ -707,7 +724,7 @@ private function _generateEntityFieldMappingProperties(ClassMetadataInfo $metada
return implode("\n", $lines);
}

private function _generateEntityStubMethod(ClassMetadataInfo $metadata, $type, $fieldName, $typeHint = null)
private function _generateEntityStubMethod(ClassMetadataInfo $metadata, $type, $fieldName, $typeHint = null, $defaultValue = null)
{
if ($type == "add") {
$addMethod = explode("\\", $typeHint);
Expand Down Expand Up @@ -737,6 +754,7 @@ private function _generateEntityStubMethod(ClassMetadataInfo $metadata, $type, $
'<variableName>' => Inflector::camelize($fieldName),
'<methodName>' => $methodName,
'<fieldName>' => $fieldName,
'<variableDefault>' => ($defaultValue !== null ) ? ('='.$defaultValue) : '',
'<entity>' => $this->_getClassName($metadata)
);

Expand Down Expand Up @@ -805,7 +823,12 @@ private function _generateAssociationMappingPropertyDocBlock(array $associationM
{
$lines = array();
$lines[] = $this->_spaces . '/**';
$lines[] = $this->_spaces . ' * @var ' . $associationMapping['targetEntity'];

if ($associationMapping['type'] & ClassMetadataInfo::TO_MANY) {
$lines[] = $this->_spaces . ' * @var \Doctrine\Common\Collections\ArrayCollection';
} else {
$lines[] = $this->_spaces . ' * @var ' . $associationMapping['targetEntity'];
}

if ($this->_generateAnnotations) {
$lines[] = $this->_spaces . ' *';
Expand Down

0 comments on commit 6b0cd7b

Please sign in to comment.