EasyAdminBundle: Reflection exception: Property id does not exist in v3.1.8

Describe the bug Reflection exception: Property id does not exist is thrown when attempting to show a list view of an entity mapped to a user super class where the id visibility is private.

(OPTIONAL) Additional context Related to #3802


in ...\vendor\easycorp\easyadmin-bundle\src\Dto\EntityDto.php (line 83)

            if (null !== $this->primaryKeyValue) {
                return $this->primaryKeyValue;
            }
            $r = ClassUtils::newReflectionObject($this->instance);
            $primaryKeyProperty = $r->getProperty($this->primaryKeyName);  // line 83
            $primaryKeyProperty->setAccessible(true);
            $primaryKeyValue = $primaryKeyProperty->getValue($this->instance);
            return $this->primaryKeyValue = $primaryKeyValue;
        }

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 17 (3 by maintainers)

Commits related to this issue

Most upvoted comments

I posted a comment 2 days ago, seems like it didn’t submit.

I came up with another better solution, that is to use symfony propertyAccessor.

    public function getPrimaryKeyValue()
    {
        if (null === $this->instance) {
            return null;
        }

        if (null !== $this->primaryKeyValue) {
            return $this->primaryKeyValue;
        }

        $propertyAccessor = PropertyAccess::createPropertyAccessorBuilder()
            ->enableExceptionOnInvalidIndex()
            ->getPropertyAccessor();

        $primaryKeyValue = $propertyAccessor->getValue($this->instance, $this->primaryKeyName);

        return $this->primaryKeyValue = $primaryKeyValue;
    }

not only this solves the inheritance issue, it also solves the need for reflection hack. PropertyAccess uses getter method for getting value, which is the indented way to do it.

I’ll make a PR.

Ho, Sorry ! It’s working now !

Oh, did you replace the whole function?

The snippet I provided was meant to replace only this part:

        $r = ClassUtils::newReflectionObject($this->instance);
        $primaryKeyProperty = $r->getProperty($this->primaryKeyName);
        $primaryKeyProperty->setAccessible(true);
        $primaryKeyValue = $primaryKeyProperty->getValue($this->instance);

        return $this->primaryKeyValue = $primaryKeyValue;