From 896d29ceec8efb106054f85ae9beca84497e49f2 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Wed, 23 Sep 2026 11:23:33 +0200 Subject: [PATCH] Binary: Ignore expressions in case of postgres --- src/Behavior/Binary.php | 5 ++++- tests/Behavior/BinaryTest.php | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Behavior/Binary.php b/src/Behavior/Binary.php index 75fd294b..52b4a53f 100644 --- a/src/Behavior/Binary.php +++ b/src/Behavior/Binary.php @@ -8,6 +8,7 @@ use ipl\Orm\Exception\ValueConversionException; use ipl\Orm\Query; use ipl\Sql\Adapter\Pgsql; +use ipl\Sql\Expression; use ipl\Stdlib\Filter\Condition; use UnexpectedValueException; @@ -44,7 +45,7 @@ public function fromDb($value, $key, $_) */ public function toDb($value, $key, $_) { - if (! $this->isPostgres) { + if (! $this->isPostgres || $value instanceof Expression) { return $value; } @@ -82,6 +83,8 @@ public function rewriteCondition(Condition $condition, $relation = null) if ($this->isPostgres && is_resource($value)) { throw new UnexpectedValueException(sprintf('Unexpected resource for %s', $column)); + } elseif ($value instanceof Expression) { + return; } // ctype_xdigit expects strings. diff --git a/tests/Behavior/BinaryTest.php b/tests/Behavior/BinaryTest.php index fc97aa75..c2bacaf9 100644 --- a/tests/Behavior/BinaryTest.php +++ b/tests/Behavior/BinaryTest.php @@ -6,6 +6,7 @@ use ipl\Orm\Exception\ValueConversionException; use ipl\Orm\Query; use ipl\Sql\Connection; +use ipl\Sql\Expression; use ipl\Stdlib\Filter\Equal; use UnexpectedValueException; @@ -107,6 +108,27 @@ public function testRewriteConditionThrowsExceptionIfAdapterIsPostgreSQLAndValue $this->behavior(true)->rewriteCondition($this->condition(fopen('php://temp', 'r'))); } + public function testPersistPropertyReturnsExpressionValuesAsIsIfAdapterIsPostgreSQL(): void + { + // Expression values (e.g. from relation filters) must not be hex-encoded but passed through as-is + $expression = new Expression('NOW()'); + + $this->assertSame( + $expression, + $this->behavior(true)->persistProperty($expression, static::TEST_COLUMN) + ); + } + + public function testRewriteConditionLeavesExpressionValuesUnchanged(): void + { + $expression = new Expression('other_column'); + $condition = $this->condition($expression); + + $this->behavior(true)->rewriteCondition($condition); + + $this->assertSame($expression, $condition->getValue()); + } + protected function behavior(bool $postgres = false): Binary { return (new Binary([static::TEST_COLUMN]))