/** * @param int $to * @param bool $forward */ protected function moveTo($to, $forward) { $this->owner->setAttribute($this->sortAttribute, $to); $tableName = $this->owner->tableName(); $path = $this->getParentPath($this->node->getAttribute($this->pathAttribute)); $like = strtr($path . $this->delimiter, ['%' => '\\%', '_' => '\\_', '\\' => '\\\\']); $unallocated = (new Query())->select("{$tableName}.[[{$this->sortAttribute}]]")->from("{$tableName}")->leftJoin("{$tableName} n", ['and', ['like', "n.[[{$this->pathAttribute}]]", $like . '%', false], ["n.[[{$this->depthAttribute}]]" => $this->node->getAttribute($this->depthAttribute), "n.[[{$this->sortAttribute}]]" => new Expression("{$tableName}.[[{$this->sortAttribute}]] " . ($forward ? '+' : '-') . " 1")]])->where(['and', ['like', "{$tableName}.[[{$this->pathAttribute}]]", $like . '%', false], [$forward ? '>=' : '<=', "{$tableName}.[[{$this->sortAttribute}]]", $to], ["{$tableName}.[[{$this->depthAttribute}]]" => $this->node->getAttribute($this->depthAttribute), "n.[[{$this->sortAttribute}]]" => null]])->orderBy(["{$tableName}.[[{$this->sortAttribute}]]" => $forward ? SORT_ASC : SORT_DESC])->limit(1)->scalar($this->owner->getDb()); $this->owner->updateAll([$this->sortAttribute => new Expression("[[{$this->sortAttribute}]] " . ($forward ? '+' : '-') . " 1")], ['and', ['like', "[[{$this->pathAttribute}]]", $like . '%', false], ["[[{$this->depthAttribute}]]" => $this->node->getAttribute($this->depthAttribute)], ['between', $this->sortAttribute, $forward ? $to : $unallocated, $forward ? $unallocated : $to]]); }
/** * @return \yii\db\ActiveQuery */ protected function getSortableQuery() { switch ($this->operation) { case self::OPERATION_PREPEND_TO: case self::OPERATION_APPEND_TO: $path = $this->node->getAttribute($this->pathAttribute); $depth = $this->node->getAttribute($this->depthAttribute) + 1; break; case self::OPERATION_INSERT_BEFORE: case self::OPERATION_INSERT_AFTER: $path = $this->node->getParentPath(); $depth = $this->node->getAttribute($this->depthAttribute); break; default: $path = $this->getParentPath(); $depth = $this->owner->getAttribute($this->depthAttribute); } $tableName = $this->owner->tableName(); return $this->owner->find()->andWhere($this->treeCondition())->andWhere($path !== null ? ['like', "{$tableName}.[[{$this->pathAttribute}]]", $this->getLike($path), false] : '1=0')->andWhere(["{$tableName}.[[{$this->depthAttribute}]]" => $depth]); }