PHPackages                             serj/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. serj/sortable

ActiveYii2-extension

serj/sortable
=============

Yii2 component to manage items order (sorting)

v1.2.2(7y ago)040MITPHPPHP &gt;=5.6.4

Since Nov 12Pushed 7y ago1 watchersCompare

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

READMEChangelogDependencies (2)Versions (7)Used By (0)

Sortable - Yii2 component to maintain sort column in relational database table.
===============================================================================

[](#sortable---yii2-component-to-maintain-sort-column-in-relational-database-table)

Installation
------------

[](#installation)

To import the component to your project, put the following line to the require section of your composer.json file:

```
"serj/sortable": "~1.2.0"
```

or run the command

```
$ composer require serj/sortable "~1.2.0"
```

Config
------

[](#config)

Let's assume a table of the following structure:

### cartoons

[](#cartoons)

```
 id |        title        | category_id | sort_local | sort_general | archived | color
----+---------------------+-------------+------------+--------------+----------+-------
  1 | Fiddlesticks        |          14 |       1000 |         7000 | t        | t
  2 | Trolley Troubles,   |          14 |       2000 |         8000 | f        | f
  3 | Fantasmagorie       |          14 |       3000 |         9000 | t        | f
  4 | Winnie the pooh     |          15 |       3000 |         3000 | f        | t
  5 | Kolobok (The loaf)  |          15 |       1000 |         2000 | f        | t
  6 | Hedgehog in the fog |          15 |       2000 |         1000 | f        | t
  7 | South Park          |          16 |       1000 |         4000 | f        | t
  8 | Futurama            |          16 |       2000 |         5000 | f        | t
  9 | Rick and Morty      |          16 |       3000 |         6000 | f        | t

```

When you want to query items in the sorted order, you must assume that items with lower sort values go first (**ASC**).

To initialize component via app config, with **minimal required settings**

```
'components' => [
    //...
    'sortableCartoons' => [
        'class' => 'serj\sortable\Sortable',
        'targetTable' => 'cartoons',
        'srtColumn' => 'sort_inner'
    ]
]
```

Let's look at more interesting scenario. Our table has 2 columns to maintain items order. *sort\_inner* - for sorting in bounds of a category, *sort\_general* - for sorting through out the entire table.

To maintain both columns we need two instances of the component, each one for its respective column.

```
'components' => [
    'sortInSingleCat' => [
        'class' => 'serj\sortable\Sortable',
        'targetTable' => 'cartoons',
        'grpColumn' => 'category_id',
        'pkColumn' => 'id',
        'srtColumn' => 'sort_inner',
        'skipRows' => [
            'archived' => true,
            'color' => false
        ]
    ],
    'sortThroughAllCat' => [
        'class' => 'serj\sortable\Sortable',
        'targetTable' => 'cartoons',
        'pkColumn' => 'id',
        'srtColumn' => 'sort_general',
        'skipRows' => [
            'archived' => true,
            'color' => false
        ]
    ]
]
```

Or if you want to use it directly without config

```
$sortThroughAllCat = new \serj\sortable\Sortable([
    'targetTable' => 'cartoons',
    'pkColumn' => 'id',
    'srtColumn' => 'sort_general',
    'skipRows' => [
        'archived' => true,
        'color' => false
    ]
]);
```

Usage
-----

[](#usage)

To get sort value for an item to be inserted **after** id:5

```
$sortValLocal = \Yii::$app->sortInSingleCat->getSortVal(5, 'after', 15);
$sortValGeneral = \Yii::$app->sortThroughAllCat->getSortVal(5, 'after');
```

To get sort value for an item to be inserted **before** id:5

```
$sortValLocal = \Yii::$app->sortInSingleCat->getSortVal(5, 'before', 15);
$sortValGeneral = \Yii::$app->sortThroughAllCat->getSortVal(5, 'before');
```

Then, if you use ActiveRecord, you may insert a new record like this

```
(new Cartoon)->setAttributes([
    'title' => 'Some title',
    'category_id' => 15,
    'sort_local' => $sortValLocal,
    'sort_general' => $sortValGeneral
])->save();
```

To get sort value for an item to be inserted **before** all items

```
// 15 is a category_id (srtColumn)
$sortValLocal = \Yii::$app->sortInSingleCat->getSortValBeforeAll(15);
$sortValGeneral = \Yii::$app->sortThroughAllCat->getSortValBeforeAll();
```

To get sort value for an item to be inserted **after** all items (in terms of specific category)

```
// 15 is a category_id (srtColumn)
$sortValLocal = \Yii::$app->sortableCartoons->getSortValAfterAll(15);
$sortValGeneral = \Yii::$app->sortThroughAllCat->getSortValAfterAll();
```

If you created a new category, say *category\_id*:17 and there are no items yet

```
$sortValLocal = \Yii::$app->sortableCartoons->getIniSortVal();

//to insert to the end of the list in terms of the entire table
$sortValGeneral = \Yii::$app->sortThroughAllCat->getSortValAfterAll();
```

If you table have a column or columns representing a state of a record (e.g. deleted, archived) which means you no longer use those records, or you just what to ignore them, you can specify it in the config as *skipRows*. In this particular case those are *archived*, *color*.

```
    'skipRows' => [
        'archived' => true,
        'color' => false
    ]
```

Thus, all tuples that have *archived* = *true* and *color* = *false* wont be taken in account. There is a gotcha: these states must be persistent, so once set they must not be reverted back. If you switch it back and forth, then do not use this option.

Alternative database connection
-------------------------------

[](#alternative-database-connection)

By default the component uses *\\Yii::$app-&gt;db*, if you have to use another connection

```
$connection = new \yii\db\Connection($config)
\Yii::$app->sortableCartoons->setDb($connection);
```

Or set it up in the component config

```
'components' => [
    'anotherDb' => [
        'class' => 'yii\db\Connection',
        ...
    ],
    ...
    'sortInSingleCat' => [
        'class' => 'serj\sortable\Sortable',
        'dbComponentId' => 'anotherDb'
        ...
    ],
    ...
]
```

Using with MySql database (by default it's Postgres)
----------------------------------------------------

[](#using-with-mysql-database-by-default-its-postgres)

```
'components' => [
    'sortInSingleCat' => [
        'class' => 'serj\sortable\Sortable',
        'databaseDriver' => serj\sortable\Sortable::DB_DRIVER_MYSQL
        ...
    ],
    ...
]
```

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity62

Established project with proven stability

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 ~103 days

Recently: every ~129 days

Total

6

Last Release

2582d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/839315f38e67485256d9ab677789ed5c05af436b72f8a3c5c1107d529c487053?d=identicon)[\_\_serj](/maintainers/__serj)

---

Tags

sortyii2extensionyiicomponentyii 2items orderdatabase sortmanage sort column

###  Code Quality

TestsCodeception

### Embed Badge

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

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

PHPackages © 2026

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