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

Class "string" does not exist #5479

Closed
dehy opened this issue Nov 21, 2022 · 5 comments
Closed

Class "string" does not exist #5479

dehy opened this issue Nov 21, 2022 · 5 comments

Comments

@dehy
Copy link

dehy commented Nov 21, 2022

Describe the bug
I'm searching in a CrudController and got Class "string" does not exist

To Reproduce

  • Use EasyAdminBundle (my version: 4.4.2) with DoctrineEnumBundle.
  • Define an Entity with a Doctrine property handled by DoctrineEnumBundle (see below)
  • Search breaks

Additional context
Why ? DoctrineEnumBundle defines classes that makes us able to use enum in entities in the form of string. So the doctrine type is a subclass of DoctrineEnumBundle\...\AbstractEnumType but the stored value is a string.

namespace App\Entity;

// use ...

class MyEntity
{
    // other properties

    // this custom type is registered in `config/packages/doctrine.yaml` as per DoctrineEnumBundle doc
    #[ORM\Column(type: 'ContestType', nullable: true)]
    private ?string $contestType = null;

    // setters and getters
}
namespace App\DBAL\Types;

use Fresh\DoctrineEnumBundle\DBAL\Types\AbstractEnumType;

/**
 * @extends AbstractEnumType<string, string>
 */
final class ContestType extends AbstractEnumType
{
    public const FEDERAL = 'federal';
    public const INTERNATIONAL = 'international';

    protected static array $choices = [
        self::FEDERAL => 'Fédéral',
        self::INTERNATIONAL => 'International',
    ];
}

The forms would display a <select> widget with those 2 options and I can easily render the human readable text with a simple twig filter. The effective property value stored by doctrine is one of the const values (strings).

Potential solution

The problematic line (L149) with context

if (!$isBoolean &&
!$isSmallIntegerProperty &&
!$isIntegerProperty &&
!$isNumericProperty &&
!$isTextProperty &&
!$isGuidProperty &&
!$isUlidProperty &&
!$isJsonProperty
) {
$idClassType = (new \ReflectionProperty($entityDto->getFqcn(), $propertyName))->getType();
if (null !== $idClassType) {
$idClassName = $idClassType->getName();
$isUlidProperty = (new \ReflectionClass($idClassName))->isSubclassOf(Ulid::class);
$isGuidProperty = (new \ReflectionClass($idClassName))->isSubclassOf(Uuid::class);
}
}

As I undertand, this lib search for the property Doctrine type, and if it doesn't find default one (string, boolean, array...), it thinks it should be a custom PHP Class and try to Reflect it. But in my case, it's a custom doctrine type with a php scalar type.

My workaround for my case

 $idClassType = (new \ReflectionProperty($entityDto->getFqcn(), $propertyName))->getType(); 
  
  // we should ideally check if it is scalar type (string, float, ...)
 if (null !== $idClassType && 'string' !== $idClassType->getName()) { 
     $idClassName = $idClassType->getName(); 
     $isUlidProperty = (new \ReflectionClass($idClassName))->isSubclassOf(Ulid::class); 
     $isGuidProperty = (new \ReflectionClass($idClassName))->isSubclassOf(Uuid::class); 
 }
@OskarStark
Copy link
Collaborator

Friendly ping @silasjoisten

@MirandaCalls
Copy link

I am also running into this bug. I'm using a custom Geography doctrine type from the Doctrine PostGIS package.

The crud controller search works until I add an entity attribute using one of the PostGIS doctrine types:

    #[ORM\Column(type: PostGISType::GEOGRAPHY)]
    private ?string $location = null;

@jroszkiewicz
Copy link
Contributor

Lol, bug from November and still not fixed?
I have a same situation O_O

@jroszkiewicz
Copy link
Contributor

@dehy @MirandaCalls @OskarStark

I made MR with fix: #5568

javiereguiluz added a commit that referenced this issue Jan 11, 2023
This PR was merged into the 4.x branch.

Discussion
----------

Reflect only class variable (#5479)

Working fix for this bug: #5479

Commits
-------

8c92600 Reflect only class variable
@javiereguiluz
Copy link
Collaborator

Fixed in #5568. Thanks a lot for fixing it!

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

5 participants