https://github.com/ADmad/cakephp-sequence
CakePHP plugin for maintaining a contiguous sequence of records
https://github.com/ADmad/cakephp-sequence
cakephp cakephp-plugin php sequence sorting
Last synced: 5 months ago
JSON representation
CakePHP plugin for maintaining a contiguous sequence of records
- Host: GitHub
- URL: https://github.com/ADmad/cakephp-sequence
- Owner: ADmad
- License: mit
- Created: 2015-02-12T18:45:01.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2023-09-29T07:00:25.000Z (over 1 year ago)
- Last Synced: 2024-11-07T16:42:47.497Z (5 months ago)
- Topics: cakephp, cakephp-plugin, php, sequence, sorting
- Language: PHP
- Homepage:
- Size: 98.6 KB
- Stars: 44
- Watchers: 4
- Forks: 16
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE.txt
Awesome Lists containing this project
- awesome-cakephp - ADmad/Sequence plugin - Behavior for maintaining ordered list of records. (ORM / Database / Datamapping)
README
# Sequence plugin to maintain ordered list of records
[](https://github.com/ADmad/cakephp-sequence/actions/workflows/ci.yml)
[](https://codecov.io/github/ADmad/cakephp-sequence)
[](https://packagist.org/packages/admad/cakephp-sequence)
[](LICENSE.txt)## Installation
Install this plugin into your CakePHP application using [composer](http://getcomposer.org):
```
composer require admad/cakephp-sequence
```Then load the plugin by running CLI command:
```bash
./bin/cake plugin load ADmad/Sequence
```## How it works
`SequenceBehavior` provided by this plugin maintains a contiguous sequence of
integers in a selected column, for records in a table records (optionally with grouping)
when adding, editing (including moving groups) or deleting records.## Usage
Add the `SequenceBehavior` for your table and viola:
```php
$this->addBehavior('ADmad/Sequence.Sequence');
```You can customize various options as shown:
```php
$this->addBehavior('ADmad/Sequence.Sequence', [
'sequenceField' => 'position', // Field to use to store integer sequence. Default "position".
'scope' => ['group_id'], // Array of field names to use for grouping records. Default [].
'startAt' => 1, // Initial value for sequence. Default 1.
]);
```Now whenever to add a new record its `position` field will be automatically
set to current largest value in sequence plus one.When editing records you can set the position to a new value and the position of
other records in the list will be automatically updated to maintain proper
sequence.When doing a find on the table an order clause is automatically added to the
query to order by the position field if a order clause has not already been set.### Methods
#### moveUp(\Cake\Datasource\EntityInterface $entity)
Move up record by one position:```php
$modelObject->moveUp($entity);
```#### moveDown(\Cake\Datasource\EntityInterface $entity)
Move down record by one position:```php
$modelObject->moveDown($entity);
```#### setOrder(array $record)
Set order for list of records provided. Records can be provided as array of
entities or array of associative arrays like `[['id' => 1], ['id' => 2]]` or
array of primary key values like `[1, 2]`.## Acknowledgement
Shout out to @neilcrookes for his wonderful Sequence Behavior for CakePHP 1.3
which was the inspiration for this plugin.