From ec785c126e08e9e504165af00c1c10734ac281ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dezs=C5=91=20BICZ=C3=93?= Date: Thu, 5 Oct 2023 10:14:02 +0000 Subject: [PATCH 1/2] EntityQuery: Add support for filtering by attribute value --- src/Entity/Query/Condition.php | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/Entity/Query/Condition.php b/src/Entity/Query/Condition.php index 241fc6bf..d4267cf1 100644 --- a/src/Entity/Query/Condition.php +++ b/src/Entity/Query/Condition.php @@ -194,15 +194,29 @@ protected static function matchProperty(array $condition): callable { */ public static function getProperty($item, string $property) { $normalized = ucfirst(implode('', array_map('ucfirst', explode('_', $property)))); - $getter_candidates = [ - "is{$normalized}", - "get{$normalized}", - $normalized, - ]; + if (str_starts_with($normalized, 'AttributeValue')) { + [$getter, $attribute_name] = explode('.', $normalized); + $getter_candidates[] = 'get' . $getter; + unset($getter); + $args = []; + $args[] = $attribute_name; + } + else { + $args = NULL; + $getter_candidates = [ + "is{$normalized}", + "get{$normalized}", + $normalized, + ]; + } foreach ($getter_candidates as $getter) { if (method_exists($item, $getter)) { - return call_user_func([$item, $getter]); + if ($args === NULL) { + return $item->$getter(); + } + + return $item->$getter(...$args); } } From 1f73611951fc16eb1dfa043af2765b28d64b3589 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?CS=C3=89CSY=20L=C3=A1szl=C3=B3?= Date: Mon, 25 Mar 2024 07:36:06 +0100 Subject: [PATCH 2/2] Allow underscored attributes in query conditions --- src/Entity/Query/Condition.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Entity/Query/Condition.php b/src/Entity/Query/Condition.php index d4267cf1..309ceb00 100644 --- a/src/Entity/Query/Condition.php +++ b/src/Entity/Query/Condition.php @@ -193,15 +193,15 @@ protected static function matchProperty(array $condition): callable { * Property value or NULL if not found. */ public static function getProperty($item, string $property) { - $normalized = ucfirst(implode('', array_map('ucfirst', explode('_', $property)))); - if (str_starts_with($normalized, 'AttributeValue')) { - [$getter, $attribute_name] = explode('.', $normalized); + if (str_starts_with($property, 'AttributeValue.')) { + [$getter, $attribute_name] = explode('.', $property); $getter_candidates[] = 'get' . $getter; unset($getter); $args = []; $args[] = $attribute_name; } else { + $normalized = ucfirst(implode('', array_map('ucfirst', explode('_', $property)))); $args = NULL; $getter_candidates = [ "is{$normalized}",