public function beforeInsert()
{
/** @var ActiveRecord $model */
$model = $this->owner;
if (!$model->hasAttribute($this->sortableAttribute)) {
throw new InvalidConfigException("Invalid sortable attribute `{$this->sortableAttribute}`.");
}
$query = $model::find();
if (is_callable($this->scope)) {
call_user_func($this->scope, $query);
}
$maxOrder = $query->max('{{' . trim($model::tableName(), '{}') . '}}.[[' . $this->sortableAttribute . ']]');
$model->{$this->sortableAttribute} = $maxOrder + 1;
}