Download the PHP package serj/sortable without Composer
On this page you can find all versions of the php package serj/sortable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download serj/sortable
More information about serj/sortable
Files in serj/sortable
Package sortable
Short Description Yii2 component to manage items order (sorting)
License MIT
Informations about the package sortable
Sortable - Yii2 component to maintain sort column in relational database table.
Installation
To import the component to your project, put the following line to the require section of your composer.json file:
or run the command
Config
Let's assume a table of the following structure:
cartoons
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
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.
Or if you want to use it directly without config
Usage
To get sort value for an item to be inserted after id:5
To get sort value for an item to be inserted before id:5
Then, if you use ActiveRecord, you may insert a new record like this
To get sort value for an item to be inserted before all items
To get sort value for an item to be inserted after all items (in terms of specific category)
If you created a new category, say category_id:17 and there are no items yet
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.
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
By default the component uses \Yii::$app->db, if you have to use another connection
Or set it up in the component config