diff --git a/CHANGELOG.md b/CHANGELOG.md index e3b30d98..7a4f960e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to this project will be documented in this file. +## [2.232.0] - 2023-11-14 +- Changed: backported Utils/DatabaseUtil from v3 to v2 +- Deprecated: old DatabaseUtil class + ## [2.231.0] - 2023-10-24 - Added: Utils/ModelUtil::findParentsRecursively() - Deprecated: ModelUtil::findParentsRecursively() diff --git a/src/Database/DatabaseUtil.php b/src/Database/DatabaseUtil.php index 6fe82e4e..239a647a 100644 --- a/src/Database/DatabaseUtil.php +++ b/src/Database/DatabaseUtil.php @@ -323,9 +323,13 @@ function ($val) { * - inline_values: (bool) Inline the values in the sql part instead of using ? ('REGEXP (':"3"')' instead of 'REGEXP (?)'). Return value not change (still an array with an values index) * * @return array + * + * @deprecated Use Utils service instead */ public function createWhereForSerializedBlob(string $field, array $values, string $connective = self::SQL_CONDITION_OR, array $options = []) { + trigger_deprecation('heimrichhannot/contao-utils-bundle', '2.232.0', 'Using %s is deprecated and will no longer work in Utils Bundle 3.0. Use the Utils service instead.', __METHOD__); + $where = null; $returnValues = []; $inlineValues = $options['inline_values'] ?? false; diff --git a/src/Util/DatabaseUtil.php b/src/Util/DatabaseUtil.php new file mode 100644 index 00000000..72a69e76 --- /dev/null +++ b/src/Util/DatabaseUtil.php @@ -0,0 +1,19 @@ +field = $field; + $this->values = $values; + } + + /** + * Return the where query with AND operation for each value. Values are inlined in the query ('REGEXP (':"3"')' instead of 'REGEXP (?)'). + */ + public function createInlineAndWhere(): string + { + return '('.$this->field.' REGEXP ('.implode(") AND ".$this->field.' REGEXP (', $this->getValueList()).'))'; + } + + /** + * Return the where query with OR operation for each value. Values are inlined in the query ('REGEXP (':"3"')' instead of 'REGEXP (?)'). + */ + public function createInlineOrWhere(): string + { + return '('.$this->field.' REGEXP ('.implode(") OR ".$this->field.' REGEXP (', $this->getValueList()).'))'; + } + + /** + * Return the where query with AND operation and placeholder for each value ('REGEXP (?)'). Values can be obtained from the values property. + */ + public function createAndWhere(): string + { + + return '('.$this->field.' REGEXP ('.implode(") AND ".$this->field.' REGEXP (', array_fill(0, count($this->getValueList()), '?')).'))'; + } + + /** + * Return the where query with OR operation and placeholder for each value ('REGEXP (?)'). Values can be obtained from the values property. + */ + public function createOrWhere(): string + { + return '('.$this->field.' REGEXP ('.implode(") OR ".$this->field.' REGEXP (', array_fill(0, count($this->getValueList()), '?')).'))'; + } + + private function getValueList(): array + { + $returnValues = []; + + foreach ($this->values as $val) { + $returnValues[] = ":\"$val\""; + } + + return $returnValues; + } + +} \ No newline at end of file diff --git a/src/Util/Utils.php b/src/Util/Utils.php index 5caa3e2a..f951139b 100644 --- a/src/Util/Utils.php +++ b/src/Util/Utils.php @@ -53,6 +53,11 @@ public function container(): ContainerUtil return $this->locator->get(ContainerUtil::class); } + public function database(): DatabaseUtil + { + return $this->locator->get(DatabaseUtil::class); + } + public function dca(): DcaUtil { return $this->locator->get(DcaUtil::class); @@ -109,6 +114,7 @@ public static function getSubscribedServices() AccordionUtil::class, ArrayUtil::class, ContainerUtil::class, + DatabaseUtil::class, DcaUtil::class, FileUtil::class, HtmlUtil::class,