Skip to content

Commit 2d98b18

Browse files
committed
Fixed #19: Wrong sort calculation when prepending new model to one model
1 parent 3845c66 commit 2d98b18

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

src/behaviors/numerical/BaseNumericalSortableBehavior.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ protected function resolveConflict($newPosition, $updateCurrentModel = true)
271271
foreach ($models as $model) {
272272
$isCurrentModel = $model->primaryKey == $this->model->primaryKey;
273273

274-
if ($position == $newPosition && $position != $this->getSortableCount()) {
274+
if ($position == $newPosition) {
275275
$position++;
276276
}
277277

tests/_data/Category.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Category extends ActiveRecord
1919
public function behaviors()
2020
{
2121
return [
22-
[
22+
'sort' => [
2323
'class' => ContinuousNumericalSortableBehavior::className(),
2424
'scope' => function ($model) {
2525
/* @var $model Category */
@@ -37,6 +37,16 @@ public static function tableName()
3737
return 'categories';
3838
}
3939

40+
/**
41+
* @inheritdoc
42+
*/
43+
public function rules()
44+
{
45+
return [
46+
['name', 'string', 'max' => 255],
47+
];
48+
}
49+
4050
/**
4151
* @return \yii\db\ActiveQuery
4252
*/

tests/_data/dump.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,6 @@ INSERT INTO "categories" ("id", "parent_id", "name", "sort") VALUES (5, NULL, 'C
6363
INSERT INTO "categories" ("id", "parent_id", "name", "sort") VALUES (6, 2, 'Category 2.1', 1);
6464
INSERT INTO "categories" ("id", "parent_id", "name", "sort") VALUES (7, 2, 'Category 2.2', 2);
6565
INSERT INTO "categories" ("id", "parent_id", "name", "sort") VALUES (8, 2, 'Category 2.3', 3);
66+
INSERT INTO "categories" ("id", "parent_id", "name", "sort") VALUES (9, 8, 'Category 2.3.1', 1);
6667

6768
COMMIT;

tests/unit/ContinuousTest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,21 @@ public function testCreatePrependAdded()
5050
$question->save();
5151

5252
$this->checkQuestion($question, 1, [2, 3, 4, 5, 6]);
53+
54+
// Prepend to scope with single model
55+
$category = new Category();
56+
$behaviorConfig = $category->behaviors()['sort'];
57+
$behaviorConfig['prependAdded'] = true;
58+
$category->detachBehavior('sort');
59+
$category->attachBehavior('sort', $behaviorConfig);
60+
$category->setAttributes([
61+
'parent_id' => 8,
62+
'name' => 'Category 2.3.2',
63+
], false);
64+
$category->save();
65+
$sort = Category::find()->select('sort')->orderBy(['id' => SORT_ASC])->column();
66+
67+
$this->assertEquals([1, 1, 2, 3, 2, 1, 2, 3, 2, 1], $sort);
5368
}
5469

5570
public function testUpdate()
@@ -101,7 +116,7 @@ public function testMoveToOtherScope()
101116
$category->moveAfter(7);
102117
$sort = Category::find()->select('sort')->orderBy(['id' => SORT_ASC])->column();
103118

104-
$this->assertEquals([1, 1, 3, 2, 2, 1, 2, 4], $sort);
119+
$this->assertEquals([1, 1, 3, 2, 2, 1, 2, 4, 1], $sort);
105120
}
106121

107122
public function testMoveToPosition()

0 commit comments

Comments
 (0)