Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3008228

Browse filesBrowse files
committed
[PropertyInfo] Extract nullable and collection key type for Doctrine associations
1 parent df01f06 commit 3008228
Copy full SHA for 3008228

File tree

4 files changed

+67
-3
lines changed
Filter options

4 files changed

+67
-3
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/PropertyInfo/DoctrineExtractor.php
+46-2Lines changed: 46 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,35 @@ 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+
* @see https://github.com/doctrine/doctrine2/blob/v2.5.4/lib/Doctrine/ORM/Tools/EntityGenerator.php#L1221-L1246
144+
*/
145+
private function isAssociationNullable(array $associationMapping)
146+
{
147+
if (isset($associationMapping['id']) && $associationMapping['id']) {
148+
return false;
149+
}
150+
151+
if (!isset($associationMapping['joinColumns'])) {
152+
return true;
153+
}
154+
155+
$joinColumns = $associationMapping['joinColumns'];
156+
foreach ($joinColumns as $joinColumn) {
157+
if (isset($joinColumn['nullable']) && !$joinColumn['nullable']) {
158+
return false;
159+
}
160+
}
161+
162+
return true;
163+
}
164+
121165
/**
122166
* Gets the corresponding built-in PHP type.
123167
*

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

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/DoctrineExtractorTest.php
+10-1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/Fixtures/DoctrineDummy.php
+5Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/Fixtures/DoctrineRelation.php
+6Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.