From a1adee4c88d0b6a6d848872b8253a9dc94deb4d3 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Thu, 4 Aug 2022 12:34:54 +0200 Subject: [PATCH] Introduce class `ipl\Orm\Behavior\NonTextMatch` It's purpose is to automatically cast non-text values to text if the used filter is a `LIKE`. Though, I don't *like* this. But it's the only solution right now. It depends highly on the maintained column type. While this isn't that bad, since *I* believe there are more text columns than other types (so maintaining them isn't that difficult), but currentlyl there's no default type. So if the type is not maintained, it currently casts everything to text, even if not necessary. But is it a good idea to have the type default to text? This needs to be addressed. Also, is there an alternative implementation? Another bad thing is that each model has to provide it explicitly. This should be a default behavior somehow if you ask me. If you have several models seeing this everywhere makes you wonder: ``` $behaviors->add(new NonTextMatch()); ``` --- src/Behavior/NonTextMatch.php | 36 +++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/Behavior/NonTextMatch.php diff --git a/src/Behavior/NonTextMatch.php b/src/Behavior/NonTextMatch.php new file mode 100644 index 0000000..4ab19ff --- /dev/null +++ b/src/Behavior/NonTextMatch.php @@ -0,0 +1,36 @@ +getDb()->getAdapter() instanceof Pgsql) { + $this->query = $query; + } + + return $this; + } + + public function rewriteCondition(Filter\Condition $condition, $relation = null) + { + if ($this->query === null || (! $condition instanceof Filter\Like && ! $condition instanceof Filter\Unlike)) { + return null; + } + + $columnDefinition = $this->query->getResolver()->getColumnDefinition($condition->metaData()->get('columnPath')); + if ($columnDefinition->getType() !== 'text') { + $condition->setColumn($condition->getColumn() . '::text'); + } + } +}