core icon indicating copy to clipboard operation
core copied to clipboard

Exists filter doesn't work on array field

Open mehdibo opened this issue 4 years ago • 4 comments

API Platform version(s) affected: 2.6.6

Description
When using the ExistsFilter on an Entity that has a field of the type array:

#[ORM\Column(type: 'array', length: 255, nullable: true)]

The filter doesn't work, that's because the filter uses IS NULL to filter it out. But when I checked the database directly the value for null is N;

That's because serialize(null) gives N;

How to reproduce
An entity that has a field with the type array and the field's value is NULL Use the ExistsFilter on that field

Possible Solution

  • Make the filter use something like = N;
  • Or not serialize NULL to 'N;' (I prefer this approach)

If you agree or have a better solution I can work on a PR

mehdibo avatar Dec 20 '21 19:12 mehdibo

Hello, just a small reminder.

I can work on this I just need to know what's the approach you want to take

mehdibo avatar Apr 15 '22 03:04 mehdibo

Or not serialize NULL to 'N;' (I prefer this approach)

That would be a suggestion to doctrine/dbal repository, which I'm sure would reject because it's a breaking change.

But I've found this line (so maybe someday):

https://github.com/doctrine/dbal/blob/719663b15983278227669c8595151586a2ff3327/src/Types/ArrayType.php#L32

norkunas avatar Apr 15 '22 03:04 norkunas

Thanks for the answer.

Do you think that there is a solution that can be implemented on API platform level or is it better to just wait for Doctrine to implement it?

Make the filter use something like = N;

Is this a possible solution?

mehdibo avatar Apr 15 '22 04:04 mehdibo

I think yes, but we need some feedback from core maintainers :)

norkunas avatar Apr 15 '22 04:04 norkunas

  1. create a custom filter (maybe by using the code of our ExistsFilter)
  2. if you think it should benefit to everyone, please contribute it

Note that we want to improve doctrine filters alltogether at some point. Also check https://github.com/soyuka/esql which may be of interest.

soyuka avatar Sep 16 '22 10:09 soyuka