/**
* Recalculate sorting
*/
public function recalculateSort()
{
$owner = $this->owner;
$db = $this->owner->getDb();
$builder = new QueryBuilder($db);
$orderFields = ['sort' => 'asc'];
foreach ($owner->primaryKey() as $field) {
if ($field != 'sort') {
$orderFields[$field] = 'asc';
}
}
// recalculate sort
$query = $builder->update($owner->tableName(), [$this->sortAttribute => new Expression('(@sortingCount:=(@sortingCount+1))')], $this->getCondition(), $params) . ' ' . $builder->buildOrderBy($orderFields);
$db->createCommand('set @sortingCount=-1;' . $query, $params)->execute();
// update in current record
if (!$owner->getIsNewRecord()) {
$owner->{$this->sortAttribute} = $owner->findOne($owner->getPrimaryKey())->{$this->sortAttribute};
}
}