fix(criteria): compare backed enums by their scalar value
Using an enum as a criteria value reached PDO as an object and failed with "Object of class X could not be converted to string". Filtering by a state column - the most natural thing to do with an enum-mapped property - was impossible. CriteriaCompiler now unwraps \BackedEnum both for a plain value and inside an an IN list. Covered by CriteriaCompilerEnumTest.
This commit is contained in:
1 parent
316cbdc2cd
commit
318b5b9da2
3 files changed
+74
-1
No files matched your search
@@ -74,12 +74,21 @@ final class CriteriaCompiler
|
||||
return [$quotedColumn . ' IS NULL', []];
|
||||
}
|
||||
|
||||
// A backed enum is compared by its scalar value: PDO cannot bind the enum instance itself.
|
||||
if ($value instanceof \BackedEnum) {
|
||||
return [$quotedColumn . ' = ?', [$value->value]];
|
||||
}
|
||||
|
||||
if (is_array($value)) {
|
||||
if ($value === []) {
|
||||
return ['1 = 0', []];
|
||||
}
|
||||
|
||||
return $this->dialect->renderCondition($quotedColumn, Cond::in(array_values($value)));
|
||||
$values = array_map(static fn(mixed $entry): mixed => $entry instanceof \BackedEnum
|
||||
? $entry->value
|
||||
: $entry, array_values($value));
|
||||
|
||||
return $this->dialect->renderCondition($quotedColumn, Cond::in($values));
|
||||
}
|
||||
|
||||
return [$quotedColumn . ' = ?', [$value]];
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Camelia\Orm\Tests\Fixture;
|
||||
|
||||
enum EstadoPedido: string
|
||||
{
|
||||
case Pendiente = 'pendiente';
|
||||
case Confirmado = 'confirmado';
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Camelia\Orm\Tests\Unit\Query;
|
||||
|
||||
use Camelia\Orm\Metadata\MetadataFactory;
|
||||
use Camelia\Orm\Query\CriteriaCompiler;
|
||||
use Camelia\Orm\Query\MySqlDialect;
|
||||
use Camelia\Orm\Tests\Fixture\ArticuloConEstado;
|
||||
use Camelia\Orm\Tests\Fixture\EstadoPedido;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Regression: an enum used as a criteria value reached PDO as an object and failed with
|
||||
* "Object of class X could not be converted to string". A backed enum must be compared by its
|
||||
* scalar value, both alone and inside an IN list.
|
||||
*/
|
||||
final class CriteriaCompilerEnumTest extends TestCase
|
||||
{
|
||||
private CriteriaCompiler $compiler;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->compiler = new CriteriaCompiler(new MySqlDialect());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_compiles_an_enum_criterion_to_its_scalar_value(): void
|
||||
{
|
||||
$metadata = new MetadataFactory()->forClass(ArticuloConEstado::class);
|
||||
|
||||
[$sql, $params] = $this->compiler->compile($metadata, [
|
||||
'estado' => EstadoPedido::Confirmado,
|
||||
]);
|
||||
|
||||
self::assertSame('`estado` = ?', $sql);
|
||||
self::assertSame(['confirmado'], $params);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_compiles_a_list_of_enums_to_their_scalar_values(): void
|
||||
{
|
||||
$metadata = new MetadataFactory()->forClass(ArticuloConEstado::class);
|
||||
|
||||
[, $params] = $this->compiler->compile($metadata, [
|
||||
'estado' => [EstadoPedido::Pendiente, EstadoPedido::Confirmado],
|
||||
]);
|
||||
|
||||
self::assertSame(['pendiente', 'confirmado'], $params);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user