Skip to content

Commit

Permalink
bug #18360 [PropertyInfo] Extract nullable and collection key type fo…
Browse files Browse the repository at this point in the history
…r Doctrine associations (teohhanhui)

This PR was merged into the 2.8 branch.

Discussion
----------

[PropertyInfo] Extract nullable and collection key type for Doctrine associations

| Q             | A
| ------------- | ---
| Branch?       | 2.8
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | N/A
| License       | MIT
| Doc PR        | N/A

Commits
-------

3008228 [PropertyInfo] Extract nullable and collection key type for Doctrine associations
  • Loading branch information
fabpot committed Apr 7, 2016
2 parents 1db0925 + 3008228 commit cd86797
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 3 deletions.
48 changes: 46 additions & 2 deletions src/Symfony/Bridge/Doctrine/PropertyInfo/DoctrineExtractor.php
Expand Up @@ -71,20 +71,35 @@ public function getTypes($class, $property, array $context = array())

if ($metadata->isSingleValuedAssociation($property)) {
if ($metadata instanceof ClassMetadataInfo) {
$nullable = isset($metadata->discriminatorColumn['nullable']) ? $metadata->discriminatorColumn['nullable'] : false;
$associationMapping = $metadata->getAssociationMapping($property);

$nullable = $this->isAssociationNullable($associationMapping);
} else {
$nullable = false;
}

return array(new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, $class));
}

$collectionKeyType = Type::BUILTIN_TYPE_INT;

if ($metadata instanceof ClassMetadataInfo) {
$associationMapping = $metadata->getAssociationMapping($property);

if (isset($associationMapping['indexBy'])) {
$indexProperty = $associationMapping['indexBy'];
$typeOfField = $metadata->getTypeOfField($indexProperty);

$collectionKeyType = $this->getPhpType($typeOfField);
}
}

return array(new Type(
Type::BUILTIN_TYPE_OBJECT,
false,
'Doctrine\Common\Collections\Collection',
true,
new Type(Type::BUILTIN_TYPE_INT),
new Type($collectionKeyType),
new Type(Type::BUILTIN_TYPE_OBJECT, false, $class)
));
}
Expand Down Expand Up @@ -118,6 +133,35 @@ public function getTypes($class, $property, array $context = array())
}
}

/**
* Determines whether an association is nullable.
*
* @param array $associationMapping
*
* @return bool
*
* @see https://github.com/doctrine/doctrine2/blob/v2.5.4/lib/Doctrine/ORM/Tools/EntityGenerator.php#L1221-L1246
*/
private function isAssociationNullable(array $associationMapping)
{
if (isset($associationMapping['id']) && $associationMapping['id']) {
return false;
}

if (!isset($associationMapping['joinColumns'])) {
return true;
}

$joinColumns = $associationMapping['joinColumns'];
foreach ($joinColumns as $joinColumn) {
if (isset($joinColumn['nullable']) && !$joinColumn['nullable']) {
return false;
}
}

return true;
}

/**
* Gets the corresponding built-in PHP type.
*
Expand Down
Expand Up @@ -54,6 +54,7 @@ public function testGetProperties()
'customFoo',
'foo',
'bar',
'indexedBar',
),
$this->extractor->getProperties('Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineDummy')
);
Expand All @@ -75,7 +76,7 @@ public function typesProvider()
array('bool', array(new Type(Type::BUILTIN_TYPE_BOOL))),
array('binary', array(new Type(Type::BUILTIN_TYPE_RESOURCE))),
array('json', array(new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true))),
array('foo', array(new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation'))),
array('foo', array(new Type(Type::BUILTIN_TYPE_OBJECT, true, 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation'))),
array('bar', array(new Type(
Type::BUILTIN_TYPE_OBJECT,
false,
Expand All @@ -84,6 +85,14 @@ public function typesProvider()
new Type(Type::BUILTIN_TYPE_INT),
new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation')
))),
array('indexedBar', array(new Type(
Type::BUILTIN_TYPE_OBJECT,
false,
'Doctrine\Common\Collections\Collection',
true,
new Type(Type::BUILTIN_TYPE_STRING),
new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation')
))),
array('simpleArray', array(new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING)))),
array('customFoo', null),
array('notMapped', null),
Expand Down
Expand Up @@ -40,6 +40,11 @@ class DoctrineDummy
*/
public $bar;

/**
* @ManyToMany(targetEntity="DoctrineRelation", indexBy="guid")
*/
protected $indexedBar;

/**
* @Column(type="guid")
*/
Expand Down
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures;

use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Id;

/**
Expand All @@ -26,4 +27,9 @@ class DoctrineRelation
* @Column(type="smallint")
*/
public $id;

/**
* @Column(type="guid")
*/
protected $guid;
}

0 comments on commit cd86797

Please sign in to comment.