Git Product home page Git Product logo

cakephp-sequence's Introduction

Sequence plugin to maintain ordered list of records

Build Status Coverage Total Downloads License

Installation

Install this plugin into your CakePHP application using composer:

composer config repositories.repo-name vcs [email protected]:groovenectar/cakephp-sequence.git && composer require groovenectar/cakephp-sequence:dev-bake-behavior

Then load the plugin by either running:

./bin/cake plugin load ADmad/Sequence

or adding the following line to config/bootstrap.php:

\Cake\Core\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:

$this->addBehavior('ADmad/Sequence.Sequence');

You can customize various options as shown:

$this->addBehavior('ADmad/Sequence.Sequence', [
    'order' => 'position', // Field to use to store integer sequence. Default "position".
    'scope' => ['group_id'], // Array of field names to use for grouping records. Default [].
    'start' => 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:

$modelObject->moveUp($entity);

moveDown(\Cake\Datasource\EntityInterface $entity)

Move down record by one position:

$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.

cakephp-sequence's People

Contributors

adayth avatar admad avatar bravo-kernel avatar curtisgibby avatar dakota avatar groovenectar avatar josegonzalez avatar lorenzo avatar veganista avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.