Skip to content

Commit 9bcc403

Browse files
committed
add scope
1 parent e4601aa commit 9bcc403

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

SortableGridBehavior.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
* return [
2323
* 'sort' => [
2424
* 'class' => SortableGridBehavior::className(),
25-
* 'sortableAttribute' => 'sortOrder'
25+
* 'sortableAttribute' => 'sortOrder',
26+
* 'scope' => function ($query) {
27+
* $query->andWhere(['group_id' => $this->group_id]);
28+
* },
2629
* ],
2730
* ];
2831
* }
@@ -36,6 +39,9 @@ class SortableGridBehavior extends Behavior
3639
/** @var string database field name for row sorting */
3740
public $sortableAttribute = 'sortOrder';
3841

42+
/** @var callable */
43+
public $scope;
44+
3945
public function events()
4046
{
4147
return [ActiveRecord::EVENT_BEFORE_INSERT => 'beforeInsert'];
@@ -71,7 +77,12 @@ public function beforeInsert()
7177
throw new InvalidConfigException("Invalid sortable attribute `{$this->sortableAttribute}`.");
7278
}
7379

74-
$maxOrder = $model->find()->max($model->tableName() . '.' . $this->sortableAttribute);
80+
$query = $model::find();
81+
if (is_callable($this->scope)) {
82+
call_user_func($this->scope, $query);
83+
}
84+
85+
$maxOrder = $query->max($model::tableName() . '.' . $this->sortableAttribute);
7586
$model->{$this->sortableAttribute} = $maxOrder + 1;
7687
}
7788
}

0 commit comments

Comments
 (0)