Skip to content

Commit

Permalink
Improving the filter.
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher Mota committed Apr 8, 2019
1 parent 90dc34a commit 40f09e5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
31 changes: 16 additions & 15 deletions src/ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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 === '') {
Expand All @@ -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);
Expand Down
8 changes: 5 additions & 3 deletions src/FilterBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand All @@ -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)) {
Expand Down

0 comments on commit 40f09e5

Please sign in to comment.