PHPackages                             kirillemko/yii2-order-behavior - 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. kirillemko/yii2-order-behavior

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

kirillemko/yii2-order-behavior
==============================

Manage model order

1.0.5(1y ago)090PHP

Since Jul 25Pushed 1y ago1 watchersCompare

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

READMEChangelogDependencies (1)Versions (5)Used By (0)

This extension provides support for ActiveRecord custom records order setup.

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

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
php composer.phar require --prefer-dist kirillemko/order-behavior

```

or add

```
"kirillemko/order-behavior": "*"
```

to the require section of your composer.json.

Usage
-----

[](#usage)

This extension provides support for custom records order setup via column-based position index.

This extension provides \[\[\\kirillemko\\ar\\position\\OrderBehavior\]\] ActiveRecord behavior for such solution support in Yii2. You may attach it to your model class in the following way:

```
class Item extends ActiveRecord
{
    public function behaviors()
    {
        return [
            'positionBehavior' => [
                'class' => OrderBehavior::className(),
                'positionAttribute' => 'order',
            ],
        ];
    }
}
```

Behavior uses the specific integer field of the database entity to set up position index. Due to this the database entity, which the model refers to, must contain field \[\[positionAttribute\]\].

In order to display custom list in correct order you should sort it by \[\[positionAttribute\]\] in ascending mode:

```
$records = Item::find()->orderBy(['position' => SORT_ASC])->all();
foreach ($records as $record) {
    echo $record->position . ', ';
}
// outputs: 1, 2, 3, 4, 5,...
```

Position saving
----------------

[](#position-saving-)

Being attached, behavior automatically fills up `positionAttribute` value fro the new record, placing it to the end of the list:

```
echo Item::find()->count(); // outputs: 4

$item = new Item();
$item->save();

echo $item->position // outputs: 5
```

However, you may setup position for the new record explicitly:

```
echo Item::find()->count(); // outputs: 4

$item = new Item();
$item->position = 2; // enforce position '2'
$item->save();

echo $item->position // outputs: 2 !!!
```

Position switching
-------------------

[](#position-switching-)

Existing record can be moved to another position using following methods:

- \[\[movePrev()\]\] - moves record by one position towards the start of the list.
- \[\[moveNext()\]\] - moves record by one position towards the end of the list.
- \[\[moveFirst()\]\] - moves record to the start of the list.
- \[\[moveLast()\]\] - moves record to the end of the list.
- \[\[moveToPosition()\]\] - moves owner record to the specific position.

You may as well change record position through the attribute, provided to `positionAttribute` directly:

```
$item = Item::find()->andWhere(['position' => 3])->one();
$item->position = 5; // switch position to '5'
$item->save();
```

Position in group
------------------

[](#position-in-group-)

Sometimes single database entity contains several listings, which require custom ordering, separated logically by grouping attributes. For example: FAQ questions may be grouped by categories, while inside single category questions should be ordered manually. For this case \[\[\\yii2tech\\ar\\position\\PositionBehavior::$groupAttributes\]\] can be used:

```
class FaqQuestion extends ActiveRecord
{
    public function behaviors()
    {
        return [
            'positionBehavior' => [
                'class' => PositionBehavior::className(),
                'positionAttribute' => 'position',
                'groupAttributes' => [
                    'categoryId' // multiple lists varying by 'categoryId'
                ],
            ],
        ];
    }
}
```

In this case behavior will use owner values of `groupAttributes` as additional condition for position calculation and changing:

```
echo FaqQuestion::find()->andWhere(['categoryId' => 1])->count(); // outputs: '4'
echo FaqQuestion::find()->andWhere(['categoryId' => 2])->count(); // outputs: '7'

$record = new FaqQuestion();
$record->categoryId = 1;
$record->save();
echo $record->position // outputs: '5'

$record = new FaqQuestion();
$record->categoryId = 2;
$record->save();
echo $record->position // outputs: '8'
```

List navigation
----------------

[](#list-navigation-)

Records with custom position order applied make a chained list, which you may navigate if necessary. You may use \[\[\\yii2tech\\ar\\position\\PositionBehavior::getIsFirst()\]\] and \[\[\\yii2tech\\ar\\position\\PositionBehavior::getIsLast()\]\] methods to determine if particular record is the first or last one in the list. For example:

```
echo Item::find()->count(); // outputs: 10

$firstItem = Item::find()->andWhere(['position' => 1])->one();
echo $firstItem->getIsFirst(); // outputs: true
echo $firstItem->getIsLast(); // outputs: false

$lastItem = Item::find()->andWhere(['position' => 10])->one();
echo $lastItem->getIsFirst(); // outputs: false
echo $lastItem->getIsLast(); // outputs: true
```

Having a particular record instance, you can always find record, which is located at next or previous position to it, using \[\[\\yii2tech\\ar\\position\\PositionBehavior::getNext()\]\] or \[\[\\yii2tech\\ar\\position\\PositionBehavior::getPrev()\]\] method. For example:

```
$item = Item::find()->andWhere(['position' => 5])->one();

$nextItem = $item->findNext();
echo $nextItem->position; // outputs: 6

$prevItem = $item->findPrev();
echo $prevItem->position; // outputs: 4
```

You may as well get the first and the last records in the list. For example:

```
echo Item::find()->count(); // outputs: 10
$item = Item::find()->andWhere(['position' => 5])->one();

$firstItem = $item->findFirst();
echo $firstItem->position; // outputs: 1

$lastItem = $item->findLast();
echo $lastItem->position; // outputs: 10
```

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance46

Moderate activity, may be stable

Popularity9

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~323 days

Total

4

Last Release

419d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/eed5ea0d1633eceef18c38b3b86451a0856c8853df57a3872ff56afc971320f5?d=identicon)[kirillemko](/maintainers/kirillemko)

---

Top Contributors

[![kirillemko](https://avatars.githubusercontent.com/u/7727765?v=4)](https://github.com/kirillemko "kirillemko (4 commits)")

---

Tags

yii2Behaviororder

### Embed Badge

![Health badge](/badges/kirillemko-yii2-order-behavior/health.svg)

```
[![Health](https://phpackages.com/badges/kirillemko-yii2-order-behavior/health.svg)](https://phpackages.com/packages/kirillemko-yii2-order-behavior)
```

###  Alternatives

[sjaakp/yii2-taggable

Manage tags of ActiveRecord in Yii2.

3030.6k](/packages/sjaakp-yii2-taggable)[mdmsoft/yii2-autonumber

Auto number extension for the Yii framework

1830.9k](/packages/mdmsoft-yii2-autonumber)[baibaratsky/yii2-serialized-attributes-behavior

Yii2 Serialized Attributes Behavior

1174.1k9](/packages/baibaratsky-yii2-serialized-attributes-behavior)[asinfotrack/yii2-audittrail

Yii2-audittrail is a behavior and a set of widgets to track all modifications performed on a model

1727.0k](/packages/asinfotrack-yii2-audittrail)

PHPackages © 2026

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