Skip to content
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

[Insight] Object parameters should be type hinted - in src/Helper/EmbeddedListHelper.php, line 134 #54

Open
alterphp opened this issue Aug 29, 2018 · 1 comment

Comments

@alterphp
Copy link
Owner

Violation reported by SensioLabInsight. We call getId method on unknwon object.
=> method may not exist.

Is it possible to get identifiers without parsing each of related object (from collection ?) ?

@rimi-itk any idea ?

in src/Helper/EmbeddedListHelper.php, line 134

The parameter entity, which is an object, should be typehinted.

                // ManyToMany association
                elseif (isset($assoc['joinTable'])) {
                    $relatedItems = PropertyAccess::createPropertyAccessor()->getValue(
                        $parentEntity, $parentEntityProperty
                    );
                    $itemIds = $relatedItems->map(function ($entity) {
                        return $entity->getId();
                    });

                    return ['entity.id' => $itemIds->toArray()];
                }

Posted from SensioLabsInsight

@rimi-itk
Copy link
Contributor

@alterphp, I'm not sure if/how we can get ids without getting (full) objects from the collection, but we should definitely clean up the code and not make assumptions on the existence of the getId method:

$fieldName = $assoc['joinTable']['joinColumns'][0]['referencedColumnName'];
$assocFieldPart = 'entity.'.$fieldName;
$accessor = PropertyAccess::createPropertyAccessor();
$relatedItems = $accessor->getValue(
    $parentEntity, $parentEntityProperty
);
$itemIds = $relatedItems->map(function ($entity) use ($accessor, $fieldName) {
    return $accessor->getValue(
        $entity,
        $fieldName
    );
});

return [$assocFieldPart => $itemIds->toArray()];

This matches the code in the OneToMany part which makes perfect sense.

In a very old post on https://groups.google.com/forum/m/#!topic/symfony2/BVgT2vmLbto Christophe Coevoet (stof) claims that ”Getting the identifier will not initialize the proxy anymore in 2.2”, but I have not been able to verify this. I will investigate further.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants