public function moveUpstairs($withCurrentElement = true)
    {
        $operator = $withCurrentElement ? '>=' : '>';
        $startIndex = $withCurrentElement ? (int) $this->sort_index : (int) $this->sort_index + 1;
        foreach (self::find()->where('sort_index '.$operator.' :index', ['index' => (int) $this->sort_index])->andWhere(['nav_container_id' => $this->nav_container_id, 'parent_nav_id' => $this->parent_nav_id])->orderBy('sort_index ASC')->asArray()->all() as $item) {
            ++$startIndex;
            Yii::$app->db->createCommand()->update(self::tableName(), ['sort_index' => $startIndex], 'id=:id', ['id' => $item['id']])->execute();
        }
    }
public function moveUpstairs($withCurrentElement = true)
    {
        $operator = $withCurrentElement ? '>=' : '>';
        $startIndex = $withCurrentElement ? (int) $this->sort_index : (int) $this->sort_index + 1;
        foreach (self::find()->where('sort_index '.$operator.' :index', ['index' => (int) $this->sort_index])->andWhere(['nav_container_id' => $this->nav_container_id, 'parent_nav_id' => $this->parent_nav_id])->orderBy('sort_index ASC')->asArray()->all() as $item) {
            ++$startIndex;
            Yii::$app->db->createCommand()->update(self::tableName(), ['sort_index' => $startIndex], 'id=:id', ['id' => $item['id']])->execute();
        }
    }
public function moveUpstairs ( $ withCurrentElement = true ) { $ operator = $ withCurrentElement ? '>=' : '>' ; $ startIndex = $ withCurrentElement ? ( int ) $ this -> sort_index : ( int ) $ this -> sort_index + 1 ; foreach ( self :: find ( ) -> where ( 'sort_index ' . $ operator . ' :index' , [ 'index' => ( int ) $ this -> sort_index ] ) -> andWhere ( [ 'nav_container_id' => $ this -> nav_container_id , 'parent_nav_id' => $ this -> parent_nav_id ] ) -> orderBy ( 'sort_index ASC' ) -> asArray ( ) -> all ( ) as $ item ) { ++ $ startIndex ; Yii :: $ app -> db -> createCommand ( ) -> update ( self :: tableName ( ) , [ 'sort_index' => $ startIndex ] , 'id=:id' , [ 'id' => $ item [ 'id' ] ] ) -> execute ( ) ; } }
Raise the sort_index value for all elements where the current sort index is larger or equal.

id|sort_index
--------
1|0
2|1
3|2
4|3

apply moveUpstairs() to id 2 would produce the following output:

id|sort_index
1|0
2|2
3|3
4|4

if you disabled $withCurrentElement the output would be:

id|sort_index
1|0
2|1
3|3
4|4

@param $withCurrentElement boolean Whether the current element should be moved along with the others or not.
Raise the sort_index value for all elements where the current sort index is larger or equal .