PHPackages                             hacheraw/sortable - 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. hacheraw/sortable

ActiveCakephp-plugin[Utility &amp; Helpers](/categories/utility)

hacheraw/sortable
=================

CakePHP Sortable Behavior

1.3.0(2y ago)05471MITPHP

Since Dec 26Pushed 2y ago1 watchersCompare

[ Source](https://github.com/hacheraw/sortable)[ Packagist](https://packagist.org/packages/hacheraw/sortable)[ Docs](https://github.com/hacheraw/sortable)[ RSS](/packages/hacheraw-sortable/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (1)Dependencies (1)Versions (7)Used By (0)

CakePHP Sortable Behavior
=========================

[](#cakephp-sortable-behavior)

Sortable allows to sort rows of a table by using a numeric field.

**TESTED ON CakePHP 4.x**(this should also work on CakePHP 3.x)

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

[](#installation)

- Install the plugin with composer from your CakePHP project's ROOT directory (where composer.json file is located)

```
php composer.phar require hacheraw/sortable
```

- Load the plugin by running command

```
bin/cake plugin load Sortable
```

Configuration and Usage
-----------------------

[](#configuration-and-usage)

### Sortable Behavior

[](#sortable-behavior)

Attach the `Sortable` behavior to your table class.

```
/**
 * @mixin \Sortable\Model\Behavior\SortableBehavior
 */
class ElementsTable extends Table
{
    /**
     * @param array $config
     * @return void
     */
    public function initialize(array $config): void
    {
        parent::initialize($config);

        // Add the behavior to your table
        $this->addBehavior('Sortable.Sortable', [
            'field' => 'name_of_the_field', // default name is `position`
            'group' => [], // If there are associations you must set the foreign keys. eg: ['post_id']
            'start' => 1, // First item of the list (where to start counting from)
            'steps' => 1 // How much to add or substract
            ]
        );
    }
```

### Controller action example

[](#controller-action-example)

Sortable updates all values when you edit or insert a new record. In addition, you can manually call the following functions:

```
// Controller::action()

    // Move the row to a new position
    $this->Elements->move($row_id, $new_place);

    // Move the row to the top
    $this->Elements->toTop($row_id);

    // Move the row to the bottom
    $this->Elements->toBottom($row_id);
```

### View input example

[](#view-input-example)

```
    // Controller::action()

    /**
     * If there are fields that must be included in where clauses, you must specify them.
     * For example, if you want to sort post images and images' table has a column named
     * post_id, then you must add it as a condition.
     */
    $conditions = ['post_id' => 7];

    // Passing variables to the View
    $this->set('min', $this->Proceedings->ProceedingColumns->getStart());
    $this->set('max', $this->Proceedings->ProceedingColumns->getLast($conditions)); // When editing order of existing row
    $this->set('max', $this->Proceedings->ProceedingColumns->getNew($conditions)); // When adding a new row
    $this->set('step', $this->Proceedings->ProceedingColumns->getStep());
```

```
    // View

    echo $this->Form->control('position', ['min' => $min, 'max' => $max, 'step' => $step, ]); // When editing order of existing row
    echo $this->Form->control('position', ['min' => $min, 'max' => $max, 'step' => $step, 'value' => $max]); // When adding a new row
```

Notes
-----

[](#notes)

- The column's type must be numeric (tinyint, smallint, int, double...) **Note that CakePHP assumes tiniInt(1) as boolean so you have to use tinyInt(2) or greater**
- Be aware that `order` is a MySQL reserved word. You should not use it. Consider using something like `position` or `weight`. Also, if you use `position` as field name you will not need to pass it to the Behavior as that is the default field name.

Disclaimer
----------

[](#disclaimer)

- It is my first public CakePHP Plugin / Composer Package.
- It lacks of unit tests... sorry.
- I have not tested it on tables with a large amount of rows.
- Polite Comments or questions are welcome on Issues section. Even Pull Request!

###  Health Score

28

—

LowBetter than 52% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 87.5% 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 ~225 days

Recently: every ~282 days

Total

6

Last Release

877d ago

### Community

Maintainers

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

---

Top Contributors

[![hacheraw](https://avatars.githubusercontent.com/u/10481749?v=4)](https://github.com/hacheraw "hacheraw (7 commits)")[![markwatney2016](https://avatars.githubusercontent.com/u/22563689?v=4)](https://github.com/markwatney2016 "markwatney2016 (1 commits)")

---

Tags

cakephpordersortablesortingcakephpsortablesortorder

### Embed Badge

![Health badge](/badges/hacheraw-sortable/health.svg)

```
[![Health](https://phpackages.com/badges/hacheraw-sortable/health.svg)](https://phpackages.com/packages/hacheraw-sortable)
```

###  Alternatives

[dereuromark/cakephp-tools

A CakePHP plugin containing lots of useful and reusable tools

333972.2k49](/packages/dereuromark-cakephp-tools)[kyslik/column-sortable

Package for handling column sorting in Laravel 6.x

6475.8M24](/packages/kyslik-column-sortable)[symbiote/silverstripe-gridfieldextensions

A collection of useful grid field components

951.9M264](/packages/symbiote-silverstripe-gridfieldextensions)[undefinedoffset/sortablegridfield

Adds drag and drop functionality to Silverstripe's GridField

911.2M52](/packages/undefinedoffset-sortablegridfield)[dereuromark/cakephp-dto

A CakePHP plugin for generating immutable Data Transfer Objects with full type safety

3096.0k5](/packages/dereuromark-cakephp-dto)[dereuromark/cakephp-setup

A CakePHP plugin containing lots of useful management tools

35184.7k2](/packages/dereuromark-cakephp-setup)

PHPackages © 2026

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