Skip to content

Commit

Permalink
bug #20051 Fix indexBy type extraction (lemoinem)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.8 branch.

Discussion
----------

Fix indexBy type extraction

| 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

Bug found and patched by @ksom

Since 3008228, the Doctrine Bridge's PropertyInfo Extractor tries to extract the type of the key in an indexed association. However, as you can see in 3008228#diff-7a8fb8072d57f95ea6e37898b05895bcR91, the extractor was using the metadata of the class containing the association instead of the target entity's metadata to retrieve the type of the index.

The tests were green because in 3008228#diff-c7e914ed89ceffd939733efe08c039c2R44, the property used to `indexBy` was present in the classes on both sides of the relation with the same type.

Once the test is fixed (by renaming the property in the targetEntity), the test provided at 3008228#diff-1b2e044d1df011c00caf802a07895bdbR88 gives the error

    1) Symfony\Bridge\Doctrine\PropertyInfo\Tests\DoctrineExtractorTest::testExtract with data set #9 ('indexedBar', array(Symfony\Component\PropertyInfo\Type Object (...)))
    InvalidArgumentException: "" is not a valid PHP type.

The fix is to fetch the metadata of the target entity and check there for the property type.

Commits
-------

138c6e3 Fix indexBy type extraction
  • Loading branch information
fabpot committed Sep 28, 2016
2 parents aef5008 + 138c6e3 commit 6717f2c
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 3 deletions.
Expand Up @@ -88,7 +88,8 @@ public function getTypes($class, $property, array $context = array())

if (isset($associationMapping['indexBy'])) {
$indexProperty = $associationMapping['indexBy'];
$typeOfField = $metadata->getTypeOfField($indexProperty);
$subMetadata = $this->classMetadataFactory->getMetadataFor($associationMapping['targetEntity']);
$typeOfField = $subMetadata->getTypeOfField($indexProperty);

$collectionKeyType = $this->getPhpType($typeOfField);
}
Expand Down
Expand Up @@ -41,7 +41,7 @@ class DoctrineDummy
public $bar;

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

Expand Down
Expand Up @@ -31,5 +31,5 @@ class DoctrineRelation
/**
* @Column(type="guid")
*/
protected $guid;
protected $rguid;
}

0 comments on commit 6717f2c

Please sign in to comment.