Skip to content

ORM SearchFilter: Custom doctrine type is ignored #4569

Closed
@kennaar

Description

@kennaar

API Platform version(s) affected: 2.6.5

Description
When performing a filtered GET collection request, where the filter property is a custom doctrine type, the convertToDatabaseValue method of the custom doctrine type is not called. This causes the generated query to return the wrong results.

http://localhost:8000/api/foos?page=1&itemsPerPage=30&status=PARTIAL

How to reproduce

#[Table(name:"foo")]
#[Entity]
#[ApiResource]
class Foo {
    #[ORM\Column(
        name:"status",
        type: 'status',
        nullable:false,
    )]
    #[ApiFilter(SearchFilter::class, SearchFilterInterface::STRATEGY_EXACT)]
    #[ApiFilter(OrderFilter::class)]
    public Status $status;
}

class Status extends Elao\Enum\Enum
{
    use AutoDiscoveredValuesTrait;

    public const OPEN = 'OPEN';
    public const CLOSED = 'CLOSED';
    public const PARTIAL = 'PARTIAL';
}

class StatusEnumType extends Elao\Enum\Bridge\Doctrine\DBAL\Types\AbstractEnumType
{
    public const PHP_TO_DB_MAPPING = [
        Status::OPEN    => 0,
        Status::CLOSED  => 1,
        Status::PARTIAL => 2,
    ];
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
    {
        $scalar = parent::convertToDatabaseValue($value, $platform);

        if (null === $scalar) {
            return null;
        }

        return self::PHP_TO_DB_MAPPING[$scalar];
    }

    public function convertToPHPValue($value, AbstractPlatform $platform)
    {
        if (null === $value) {
            return parent::convertToPHPValue($value, $platform);
        }

        return parent::convertToPHPValue(array_flip(self::PHP_TO_DB_MAPPING)[$value], $platform);
    }

    public function getName(): string
    {
        return 'status'
    }

    protected function getEnumClass(): string
    {
        return Status::class;
    }
}

Possible Solution
This seems to be caused by #4134. The design of the filter system seems to be in flux? I think this the PR introduced a BC because this used to work in previous 2.6.x versions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions