PHPackages                             paulzi/yii2-sortable - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. paulzi/yii2-sortable

ActiveYii2-extension[Utility &amp; Helpers](/categories/utility)

paulzi/yii2-sortable
====================

Sortable Behavior for Yii2

v1.0.3(4y ago)15310.5k↓12.5%3[2 issues](https://github.com/paulzi/yii2-sortable/issues)4MITPHPPHP &gt;=5.4.0

Since Dec 11Pushed 4y ago4 watchersCompare

[ Source](https://github.com/paulzi/yii2-sortable)[ Packagist](https://packagist.org/packages/paulzi/yii2-sortable)[ RSS](/packages/paulzi-yii2-sortable/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (3)Versions (5)Used By (4)

Yii2 Sortable Behavior
======================

[](#yii2-sortable-behavior)

It implements the ability to control the order of the ActiveRecord.

[![Packagist Version](https://camo.githubusercontent.com/e0c0f5eae2a6861cd76a9e435d1b4bc22ec02db355ae469e634fcee43de867a8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7061756c7a692f796969322d736f727461626c652e737667)](https://packagist.org/packages/paulzi/yii2-sortable)[![Code Coverage](https://camo.githubusercontent.com/364bbe9a9b1c42feea90008a33348c24f874f9839ae537e6dc68598b2e4f21ad/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f7061756c7a692f796969322d736f727461626c652f6d61737465722e737667)](https://scrutinizer-ci.com/g/paulzi/yii2-sortable/?branch=master)[![Build Status](https://camo.githubusercontent.com/90b3ad57483cafa886cf20b32de0d41b18e61ddbdabf68ecf5239929fb697f49/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f7061756c7a692f796969322d736f727461626c652f6d61737465722e737667)](https://travis-ci.org/paulzi/yii2-sortable)[![Total Downloads](https://camo.githubusercontent.com/2f1938bda357628924a4f27857ab215c470d00a367785d927ba46caebeed8bd4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7061756c7a692f796969322d736f727461626c652e737667)](https://packagist.org/packages/paulzi/yii2-sortable)

Install
-------

[](#install)

Install via Composer:

```
composer require paulzi/yii2-sortable
```

or add

```
"paulzi/yii2-sortable" : "^1.0"
```

to the `require` section of your `composer.json` file.

Migrations
----------

[](#migrations)

Add signed integer column to your model. For quick operation behavior, add index for scopes fields and sort attribute, example:

```
$this->createIndex('parent_sort', '{{%item}}', ['parent_id', 'sort']);
```

Configuring
-----------

[](#configuring)

```
use paulzi\sortable\SortableBehavior;

class Sample extends \yii\db\ActiveRecord
{
    public function behaviors() {
        return [
            [
                'class' => SortableBehavior::className(),
            ],
        ];
    }
}
```

Options
-------

[](#options)

- `$query = null` - list of attributes, callback or ActiveQuery, to find scope element. See below.
- `$sortAttribute = 'sort'` - sort attribute in table schema.
- `$step = 100` - gap size between elements.
- `$joinMode = true` - search method of unallocated value. When joinMode is true, using join table with self. Otherwise, use the search in the window. Window size defined by $windowSize property.
- `$windowSize = 1000` - defines the size of the search window, when joinMode is false.

**Details of `$query` option:**The list of attributes - a simple way to scope elements with the same content fields, the aliases do not need. You MUST use tableName() alias in ActiveQuery, when you are using joinMode.

For example,

```
public function behaviors()
{
    return [
        [
            'class' => SortableBehavior::className(),
            'query' => ['parent_id'],
        ]
    ];
}
```

This is equivalent to:

```
public function behaviors()
{
    return [
        [
            'class' => SortableBehavior::className(),
            'query' => function ($model) {
                $tableName = $model->tableName();
                return $model->find()->andWhere(["{$tableName}.[[parent_id]]" => $model->parent_id]);
            },
        ]
    ];
}
```

Usage
-----

[](#usage)

Getting sort attribute value:

```
$model = Sample::findOne(1);
$position = $model->getSortablePosition();
```

To move as the first item:

```
$model = new Sample(['parent_id' => 1]);
$model->moveFirst()->save(); // inserting new node
```

To move as the last item:

```
$model = Sample::findOne(1);
$model->moveLast()->save(); // move existing node
```

To move an item to a specific position:

```
$model = Sample::findOne(1);
$model->moveTo(-33, true)->save(); // move to position -33, and move existing items forward
$model = Sample::findOne(2);
$model->moveTo(4, false)->save(); // move to position 4, and move existing items backward
```

To move an item before another: *Note: If you need to change scope, do it manually*

```
$model1 = new Sample(['parent_id' => 1]);
$model2 = Sample::findOne(2);
$model1->moveBefore($model2)->save(); // move $model1 before $model2
```

To move an item after another: *Note: If you need to change scope, do it manually*

```
$model1 = Sample::findOne(1);
$model2 = Sample::findOne(2);
$model1->parent_id = $model2->parent_id;
$model1->moveAfter($model2)->save(); // move $model1 after $model2 with change scope
```

Reorder item with the neighboring elements:

```
$model = Sample::findOne(1);
$model->reorder(true); // reorder with center zero
$model = Sample::findOne(2);
$model->reorder(false); // reorder from zero
```

SortableTrait
-------------

[](#sortabletrait)

You can use optional `SortableTrait`, if you need it (for example, you are using something like `ISortable` interface).

###  Health Score

39

—

LowBetter than 86% of packages

Maintenance17

Infrequent updates — may be unmaintained

Popularity43

Moderate usage in the ecosystem

Community21

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 72.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~740 days

Total

4

Last Release

1589d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/9402208?v=4)[PaulZi](/maintainers/PaulZi)[@paulzi](https://github.com/paulzi)

---

Top Contributors

[![paulzi](https://avatars.githubusercontent.com/u/9402208?v=4)](https://github.com/paulzi "paulzi (8 commits)")[![HaruAtari](https://avatars.githubusercontent.com/u/3523420?v=4)](https://github.com/HaruAtari "HaruAtari (1 commits)")[![ioSeoGio](https://avatars.githubusercontent.com/u/41924972?v=4)](https://github.com/ioSeoGio "ioSeoGio (1 commits)")[![userlond](https://avatars.githubusercontent.com/u/7788670?v=4)](https://github.com/userlond "userlond (1 commits)")

---

Tags

sortableyii2

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/paulzi-yii2-sortable/health.svg)

```
[![Health](https://phpackages.com/badges/paulzi-yii2-sortable/health.svg)](https://phpackages.com/packages/paulzi-yii2-sortable)
```

###  Alternatives

[himiklab/yii2-sortable-grid-view-widget

Sortable modification of standard Yii2 GridView widget

80351.1k7](/packages/himiklab-yii2-sortable-grid-view-widget)[kartik-v/yii2-sortable

Create sortable lists and grids using HTML5 drag and drop API for Yii 2.0.

433.8M13](/packages/kartik-v-yii2-sortable)[kotchuprik/yii2-sortable-widgets

Implementation Rubaxa/Sortable for Yii2. Sortable grid view inside.

61132.2k6](/packages/kotchuprik-yii2-sortable-widgets)[kartik-v/yii2-sortable-input

Sortable input widget based on yii2-sortable extension.

24660.4k2](/packages/kartik-v-yii2-sortable-input)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
