Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 31c8f92

Browse files
committed
[PropertyInfo] Extract nullable and collection key type for Doctrine associations
1 parent df01f06 commit 31c8f92

File tree

4 files changed

+65
-3
lines changed

4 files changed

+65
-3
lines changed

src/Symfony/Bridge/Doctrine/PropertyInfo/DoctrineExtractor.php

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,35 @@ public function getTypes($class, $property, array $context = array())
7171

7272
if ($metadata->isSingleValuedAssociation($property)) {
7373
if ($metadata instanceof ClassMetadataInfo) {
74-
$nullable = isset($metadata->discriminatorColumn['nullable']) ? $metadata->discriminatorColumn['nullable'] : false;
74+
$associationMapping = $metadata->getAssociationMapping($property);
75+
76+
$nullable = $this->isAssociationNullable($associationMapping);
7577
} else {
7678
$nullable = false;
7779
}
7880

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

84+
$collectionKeyType = Type::BUILTIN_TYPE_INT;
85+
86+
if ($metadata instanceof ClassMetadataInfo) {
87+
$associationMapping = $metadata->getAssociationMapping($property);
88+
89+
if (isset($associationMapping['indexBy'])) {
90+
$indexProperty = $associationMapping['indexBy'];
91+
$typeOfField = $metadata->getTypeOfField($indexProperty);
92+
93+
$collectionKeyType = $this->getPhpType($typeOfField);
94+
}
95+
}
96+
8297
return array(new Type(
8398
Type::BUILTIN_TYPE_OBJECT,
8499
false,
85100
'Doctrine\Common\Collections\Collection',
86101
true,
87-
new Type(Type::BUILTIN_TYPE_INT),
102+
new Type($collectionKeyType),
88103
new Type(Type::BUILTIN_TYPE_OBJECT, false, $class)
89104
));
90105
}
@@ -118,6 +133,33 @@ public function getTypes($class, $property, array $context = array())
118133
}
119134
}
120135

136+
/**
137+
* Determines whether an association is nullable.
138+
*
139+
* @param array $associationMapping
140+
*
141+
* @return bool
142+
*/
143+
private function isAssociationNullable(array $associationMapping)
144+
{
145+
if (isset($associationMapping['id']) && $associationMapping['id']) {
146+
return false;
147+
}
148+
149+
if (!isset($associationMapping['joinColumns'])) {
150+
return true;
151+
}
152+
153+
$joinColumns = $associationMapping['joinColumns'];
154+
foreach ($joinColumns as $joinColumn) {
155+
if (isset($joinColumn['nullable']) && !$joinColumn['nullable']) {
156+
return false;
157+
}
158+
}
159+
160+
return true;
161+
}
162+
121163
/**
122164
* Gets the corresponding built-in PHP type.
123165
*

src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/DoctrineExtractorTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public function testGetProperties()
5454
'customFoo',
5555
'foo',
5656
'bar',
57+
'indexedBar',
5758
),
5859
$this->extractor->getProperties('Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineDummy')
5960
);
@@ -75,7 +76,7 @@ public function typesProvider()
7576
array('bool', array(new Type(Type::BUILTIN_TYPE_BOOL))),
7677
array('binary', array(new Type(Type::BUILTIN_TYPE_RESOURCE))),
7778
array('json', array(new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true))),
78-
array('foo', array(new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation'))),
79+
array('foo', array(new Type(Type::BUILTIN_TYPE_OBJECT, true, 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation'))),
7980
array('bar', array(new Type(
8081
Type::BUILTIN_TYPE_OBJECT,
8182
false,
@@ -84,6 +85,14 @@ public function typesProvider()
8485
new Type(Type::BUILTIN_TYPE_INT),
8586
new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation')
8687
))),
88+
array('indexedBar', array(new Type(
89+
Type::BUILTIN_TYPE_OBJECT,
90+
false,
91+
'Doctrine\Common\Collections\Collection',
92+
true,
93+
new Type(Type::BUILTIN_TYPE_STRING),
94+
new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation')
95+
))),
8796
array('simpleArray', array(new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING)))),
8897
array('customFoo', null),
8998
array('notMapped', null),

src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/Fixtures/DoctrineDummy.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ class DoctrineDummy
4040
*/
4141
public $bar;
4242

43+
/**
44+
* @ManyToMany(targetEntity="DoctrineRelation", indexBy="guid")
45+
*/
46+
protected $indexedBar;
47+
4348
/**
4449
* @Column(type="guid")
4550
*/

src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/Fixtures/DoctrineRelation.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures;
1313

1414
use Doctrine\ORM\Mapping\Column;
15+
use Doctrine\ORM\Mapping\Entity;
1516
use Doctrine\ORM\Mapping\Id;
1617

1718
/**
@@ -26,4 +27,9 @@ class DoctrineRelation
2627
* @Column(type="smallint")
2728
*/
2829
public $id;
30+
31+
/**
32+
* @Column(type="guid")
33+
*/
34+
protected $guid;
2935
}

0 commit comments

Comments
 (0)