From 1ef61bae20e7b0f5abbd65d210a9d4c38a3df68b Mon Sep 17 00:00:00 2001 From: smarcet Date: Mon, 7 Oct 2019 11:39:38 -0300 Subject: [PATCH] Fixed params replacements Change-Id: I3e6dfbdd3c756f53dce0c2c6a5835a4c7dd6a098 --- .../Utils/Filters/DoctrineFilterMapping.php | 38 ++++++++++++++--- .../Filters/DoctrineJoinFilterMapping.php | 41 +++++++++++++++--- .../Filters/DoctrineLeftJoinFilterMapping.php | 42 ++++++++++++++++--- 3 files changed, 103 insertions(+), 18 deletions(-) diff --git a/app/Http/Utils/Filters/DoctrineFilterMapping.php b/app/Http/Utils/Filters/DoctrineFilterMapping.php index 226b2969..ed469200 100644 --- a/app/Http/Utils/Filters/DoctrineFilterMapping.php +++ b/app/Http/Utils/Filters/DoctrineFilterMapping.php @@ -46,9 +46,23 @@ class DoctrineFilterMapping extends FilterMapping */ public function apply(QueryBuilder $query, FilterElement $filter){ $param_count = $query->getParameters()->count() + 1; - $where = str_replace(":value", ":value_".$param_count, $this->where); - $where = str_replace(":operator", $filter->getOperator(), $where); - return $query->andWhere($where)->setParameter(":value_".$param_count, $filter->getValue()); + $where = $this->where; + $has_param = false; + + if(strstr($where,":value")) { + $where = str_replace(":value", ":value_" . $param_count, $where); + $has_param = true; + } + + if(strstr($where,":operator")) + $where = str_replace(":operator", $filter->getOperator(), $where); + + $query = $query->andWhere($where); + + if($has_param){ + $query = $query->setParameter(":value_".$param_count, $filter->getValue()); + } + return $query; } /** @@ -58,9 +72,21 @@ class DoctrineFilterMapping extends FilterMapping */ public function applyOr(QueryBuilder $query, FilterElement $filter){ $param_count = $query->getParameters()->count() + 1; - $where = str_replace(":value", ":value_".$param_count, $this->where); - $where = str_replace(":operator", $filter->getOperator(), $where); - $query->setParameter(":value_".$param_count, $filter->getValue()); + $where = $this->where; + $has_param = false; + + if(strstr($where,":value")) { + $where = str_replace(":value", ":value_" . $param_count, $where); + $has_param = true; + } + + if(strstr($where,":operator")) + $where = str_replace(":operator", $filter->getOperator(), $where); + + if($has_param){ + $query->setParameter(":value_".$param_count, $filter->getValue()); + } + return $where; } } \ No newline at end of file diff --git a/app/Http/Utils/Filters/DoctrineJoinFilterMapping.php b/app/Http/Utils/Filters/DoctrineJoinFilterMapping.php index d200d341..6654a45d 100644 --- a/app/Http/Utils/Filters/DoctrineJoinFilterMapping.php +++ b/app/Http/Utils/Filters/DoctrineJoinFilterMapping.php @@ -52,11 +52,28 @@ class DoctrineJoinFilterMapping extends FilterMapping */ public function apply(QueryBuilder $query, FilterElement $filter){ $param_count = $query->getParameters()->count() + 1; - $where = str_replace(":value", ":value_".$param_count, $this->where); - $where = str_replace(":operator", $filter->getOperator(), $where); + $where = $this->where; + $has_param = false; + + if(strstr($where,":value")) { + $where = str_replace(":value", ":value_" . $param_count, $where); + $has_param = true; + } + + if(strstr($where,":operator")) + $where = str_replace(":operator", $filter->getOperator(), $where); + if(!in_array($this->alias, $query->getAllAliases())) $query->innerJoin($this->table, $this->alias, Join::WITH); - return $query->andWhere($where)->setParameter(":value_".$param_count, $filter->getValue()); + + $query = $query->andWhere($where); + + if($has_param){ + $query = $query->setParameter(":value_".$param_count, $filter->getValue()); + } + + return $query; + } /** @@ -66,11 +83,23 @@ class DoctrineJoinFilterMapping extends FilterMapping */ public function applyOr(QueryBuilder $query, FilterElement $filter){ $param_count = $query->getParameters()->count() + 1; - $where = str_replace(":value", ":value_".$param_count, $this->where); - $where = str_replace(":operator", $filter->getOperator(), $where); + $where = $this->where; + $has_param = false; + + if(strstr($where,":value")) { + $where = str_replace(":value", ":value_" . $param_count, $where); + $has_param = true; + } + + if(strstr($where,":operator")) + $where = str_replace(":operator", $filter->getOperator(), $where); + if(!in_array($this->alias, $query->getAllAliases())) $query->innerJoin($this->table, $this->alias, Join::WITH); - $query->setParameter(":value_".$param_count, $filter->getValue()); + + if($has_param){ + $query->setParameter(":value_".$param_count, $filter->getValue()); + } return $where; } } \ No newline at end of file diff --git a/app/Http/Utils/Filters/DoctrineLeftJoinFilterMapping.php b/app/Http/Utils/Filters/DoctrineLeftJoinFilterMapping.php index 2e24e702..89982311 100644 --- a/app/Http/Utils/Filters/DoctrineLeftJoinFilterMapping.php +++ b/app/Http/Utils/Filters/DoctrineLeftJoinFilterMapping.php @@ -26,12 +26,27 @@ class DoctrineLeftJoinFilterMapping extends DoctrineJoinFilterMapping */ public function apply(QueryBuilder $query, FilterElement $filter){ $param_count = $query->getParameters()->count() + 1; - $where = str_replace(":value", ":value_".$param_count, $this->where); - $where = str_replace(":operator", $filter->getOperator(), $where); + $where = $this->where; + $has_param = false; + + if(strstr($where,":value")) { + $where = str_replace(":value", ":value_" . $param_count, $where); + $has_param = true; + } + + if(strstr($where,":operator")) + $where = str_replace(":operator", $filter->getOperator(), $where); + if(!in_array($this->alias, $query->getAllAliases())) $query->leftJoin($this->table, $this->alias, Join::WITH); - return $query->andWhere($where)->setParameter(":value_".$param_count, $filter->getValue()); + $query = $query->andWhere($where); + + if($has_param){ + $query = $query->setParameter(":value_".$param_count, $filter->getValue()); + } + + return $query; } /** @@ -41,11 +56,26 @@ class DoctrineLeftJoinFilterMapping extends DoctrineJoinFilterMapping */ public function applyOr(QueryBuilder $query, FilterElement $filter){ $param_count = $query->getParameters()->count() + 1; - $where = str_replace(":value", ":value_".$param_count, $this->where); - $where = str_replace(":operator", $filter->getOperator(), $where); + $where = $this->where; + $has_param = false; + + if(strstr($where,":value")) { + $where = str_replace(":value", ":value_" . $param_count, $where); + $has_param = true; + } + + if(strstr($where,":operator")) + $where = str_replace(":operator", $filter->getOperator(), $where); + if(!in_array($this->alias, $query->getAllAliases())) $query->leftJoin($this->table, $this->alias, Join::WITH); - $query->setParameter(":value_".$param_count, $filter->getValue()); + + if(!in_array($this->alias, $query->getAllAliases())) + $query->leftJoin($this->table, $this->alias, Join::WITH); + + if($has_param){ + $query->setParameter(":value_".$param_count, $filter->getValue()); + } return $where; } } \ No newline at end of file