Download the PHP package kirillemko/yii2-order-behavior without Composer
On this page you can find all versions of the php package kirillemko/yii2-order-behavior. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download kirillemko/yii2-order-behavior
More information about kirillemko/yii2-order-behavior
Files in kirillemko/yii2-order-behavior
Informations about the package yii2-order-behavior
This extension provides support for ActiveRecord custom records order setup.
Installation
The preferred way to install this extension is through composer.
Either run
or add
to the require section of your composer.json.
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:
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:
Position saving
Being attached, behavior automatically fills up positionAttribute
value fro the new record, placing it to the end
of the list:
However, you may setup position for the new record explicitly:
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:
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:
In this case behavior will use owner values of groupAttributes
as additional condition for position
calculation and changing:
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:
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:
You may as well get the first and the last records in the list. For example: