File tree Expand file tree Collapse file tree 1 file changed +13
-2
lines changed Expand file tree Collapse file tree 1 file changed +13
-2
lines changed Original file line number Diff line number Diff line change 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}
You can’t perform that action at this time.
0 commit comments