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

[DoctrineBridge] indexBy could reference to association columns #38628

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 9, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[DoctrineBridge] indexBy could reference to association columns
  • Loading branch information
juanmiguelbesada authored and nicolas-grekas committed Nov 9, 2020
commit 4c361456644d7d7f7fadc79c8633572c33a94d72
18 changes: 16 additions & 2 deletions 18 src/Symfony/Bridge/Doctrine/PropertyInfo/DoctrineExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,24 @@ public function getTypes($class, $property, array $context = [])
$associationMapping = $metadata->getAssociationMapping($property);

if (isset($associationMapping['indexBy'])) {
$indexColumn = $associationMapping['indexBy'];
/** @var ClassMetadataInfo $subMetadata */
$subMetadata = $this->classMetadataFactory->getMetadataFor($associationMapping['targetEntity']);
$typeOfField = $subMetadata->getTypeOfField($subMetadata->getFieldForColumn($indexColumn));

// Check if indexBy value is a property
$fieldName = $associationMapping['indexBy'];
if (null === ($typeOfField = $subMetadata->getTypeOfField($fieldName))) {
$fieldName = $subMetadata->getFieldForColumn($associationMapping['indexBy']);
//Not a property, maybe a column name?
if (null === ($typeOfField = $subMetadata->getTypeOfField($fieldName))) {
//Maybe the column name is the association join column?
$associationMapping = $subMetadata->getAssociationMapping($fieldName);

/** @var ClassMetadataInfo $subMetadata */
$indexProperty = $subMetadata->getSingleAssociationReferencedJoinColumnName($fieldName);
$subMetadata = $this->classMetadataFactory->getMetadataFor($associationMapping['targetEntity']);
$typeOfField = $subMetadata->getTypeOfField($indexProperty);
}
}

if (!$collectionKeyType = $this->getPhpType($typeOfField)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ public function testGetProperties()
$expected = array_merge($expected, [
'foo',
'bar',
'indexedRguid',
'indexedBar',
'indexedFoo',
'indexedBaz',
'indexedByDt',
'indexedByCustomType',
]);
Expand Down Expand Up @@ -151,6 +153,14 @@ public function typesProvider()
new Type(Type::BUILTIN_TYPE_INT),
new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation')
)]],
['indexedRguid', [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')
)]],
['indexedBar', [new Type(
Type::BUILTIN_TYPE_OBJECT,
false,
Expand All @@ -167,6 +177,14 @@ public function typesProvider()
new Type(Type::BUILTIN_TYPE_STRING),
new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation')
)]],
['indexedBaz', [new Type(
Type::BUILTIN_TYPE_OBJECT,
false,
Collection::class,
true,
new Type(Type::BUILTIN_TYPE_INT),
new Type(Type::BUILTIN_TYPE_OBJECT, false, DoctrineRelation::class)
)]],
['simpleArray', [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING))]],
['customFoo', null],
['notMapped', null],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ class DoctrineDummy
*/
public $bar;

/**
* @ManyToMany(targetEntity="DoctrineRelation", indexBy="rguid")
*/
protected $indexedRguid;

/**
* @ManyToMany(targetEntity="DoctrineRelation", indexBy="rguid_column")
*/
Expand All @@ -51,6 +56,11 @@ class DoctrineDummy
*/
protected $indexedFoo;

/**
* @OneToMany(targetEntity="DoctrineRelation", mappedBy="baz", indexBy="baz_id")
*/
protected $indexedBaz;

/**
* @Column(type="guid")
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ class DoctrineRelation
*/
protected $foo;

/**
* @ManyToOne(targetEntity="DoctrineDummy")
*/
protected $baz;

/**
* @Column(type="datetime")
*/
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.