From 40f09e52d9cd348c1be4b1acddf99620a1360feb Mon Sep 17 00:00:00 2001 From: Christopher Mota Date: Mon, 8 Apr 2019 09:42:00 -0300 Subject: [PATCH] Improving the filter. --- src/ActiveRecord.php | 31 ++++++++++++++++--------------- src/FilterBuilder.php | 8 +++++--- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/ActiveRecord.php b/src/ActiveRecord.php index 1b809d5..9aeff8e 100644 --- a/src/ActiveRecord.php +++ b/src/ActiveRecord.php @@ -153,7 +153,6 @@ protected function insertInternal($attributes = null) $primaryKey = static::primaryKey(); $values = $this->getDirtyAttributes($attributes); $dn = $values[$primaryKey[0]]; - //$dn = $this->getPrimaryKey(); unset($values[$primaryKey[0]]); static::getDb()->open(); @@ -184,25 +183,22 @@ protected function updateInternal($attributes = null) return false; } $values = $this->getDirtyAttributes($attributes); - if (isset($values[self::primaryKey()[0]])) { - unset($values[self::primaryKey()[0]]); - } - if (empty($values)) { - $this->afterSave(false, $values); - return 0; - } if ($this->getOldPrimaryKey() !== $this->getPrimaryKey()) { - static::getDb()->open(); - static::getDb()->rename($this->getOldPrimaryKey(), $this->getPrimaryKey(), $newParent, true); - static::getDb()->close(); - if (!$this->refresh()) { - Yii::info('Model not refresh.', __METHOD__); - return 0; - } +// static::getDb()->open(); +// static::getDb()->rename($this->getOldPrimaryKey(), $this->getPrimaryKey(), $newParent, true); +// static::getDb()->close(); +// if (!$this->refresh()) { +// Yii::info('Model not refresh.', __METHOD__); +// return 0; +// } } + $attributes = []; foreach ($values as $key => $value) { + if ($key == 'dn') { + continue; + } if (empty($this->getOldAttribute($key)) && $value === '') { unset($values[$key]); } elseif ($value === '') { @@ -214,6 +210,11 @@ protected function updateInternal($attributes = null) } } + if (empty($attributes)) { + $this->afterSave(false, $attributes); + return 0; + } + // We do not check the return value of updateAll() because it's possible // that the UPDATE statement doesn't change anything and thus returns 0. $rows = static::updateAll($attributes, $condition); diff --git a/src/FilterBuilder.php b/src/FilterBuilder.php index 2baee36..145a8c2 100644 --- a/src/FilterBuilder.php +++ b/src/FilterBuilder.php @@ -9,10 +9,8 @@ namespace chrmorandi\ldap; -use Traversable; use yii\base\InvalidParamException; use yii\base\BaseObject; -use yii\helpers\ArrayHelper; /** * FilterBuilder builds a Filter for search in LDAP. @@ -92,7 +90,7 @@ public function buildHashCondition($condition) { $parts = []; foreach ($condition as $attribute => $value) { - if (ArrayHelper::isTraversable($value)) { + if (is_array($value)) { // IN condition $parts[] = $this->buildInCondition('IN', [$attribute, $value]); } elseif ($value === null) { @@ -114,6 +112,10 @@ public function buildHashCondition($condition) */ public function buildAndCondition($operator, $operands) { + if ($operator === 'NOT' && count($operands) !== 1) { + throw new InvalidParamException("Operator '$operator' requires exactly one operand."); + } + $parts = []; foreach ($operands as $operand) { if (is_array($operand)) {