PHPackages                             burriko/cake-state-machine - 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. [Framework](/categories/framework)
4. /
5. burriko/cake-state-machine

ActiveCakephp-plugin[Framework](/categories/framework)

burriko/cake-state-machine
==========================

A finite state machine behavior for CakePHP 2.x

241.0k6PHP

Since Feb 9Pushed 11y ago3 watchersCompare

[ Source](https://github.com/burriko/cakephp-state-machine-behavior)[ Packagist](https://packagist.org/packages/burriko/cake-state-machine)[ RSS](/packages/burriko-cake-state-machine/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependenciesVersions (1)Used By (0)

CakePHP Finite State Machine Behavior
=====================================

[](#cakephp-finite-state-machine-behavior)

A basic finite state machine behavior for CakePHP 2.x. A version for CakePHP 1 can be found in the branch cakephp1.

Describe a model's states and the events that cause transitions between them, then trigger these events to change your model to a new state.

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

[](#installation)

If you're using composer then just add the following to your require block.

```
	"burriko/cake-state-machine": "2.0.*@dev"

```

If you're not, then clone/copy the contents of this directory to app/Plugins/CakeStateMachine.

Configure
---------

[](#configure)

1. Add the following line to your app/Config/bootstrap.php.

    ```
     CakePlugin::load('CakeStateMachine');

    ```
2. In your model add:

    ```
     public $actsAs = array('CakeStateMachine.StateMachine');

    ```
3. Create a new database table to store the states using the following schema, adjusting the table name and foreign key to match your model. For example, if you are adding states to a model name Placement you would use the following.

    ```
     CREATE TABLE `placement_states` (
       `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
       `placement_id` int(11) unsigned NOT NULL,
       `state` varchar(50) NOT NULL DEFAULT '',
       `created` datetime NOT NULL,
       PRIMARY KEY (`id`)
     ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    ```
4. Add a new varchar field to your model's table called 'state'.

    ```
     ALTER TABLE `placements` ADD `state` VARCHAR(50);

    ```

Usage
-----

[](#usage)

1. In your model describe the states and transitions you need like so.

    ```
     public $states = array('state' => array('event' => 'new_state'));

    ```

    Here's an example.

    ```
     public $states = array(
       'advertised' => array(
         'select_appropriate_applicants' => 'shortlisted',
         'no_appropriate_applicants' => 'unplaced'
       ),
       'shortlisted' => array(
         'select_for_interview' => 'interviews',
         'no_appropriate_applicants' => 'unplaced'
       ),
       'interviews' => array(
         'select_successful_candidate' => 'placed',
         'no_appropriate_applicants' => 'unplaced'
       ),
       'placed' => array(
         'placement_complete' => 'complete',
         'problem_with_placement' => 'unplaced'
       ),
       'unplaced' => array(
         'readvertise' => 'advertised'
       ),
       'complete'
     );

    ```
2. When a new record is created by that model it will be assigned the first state in the list as it's initial state.
3. To transition to a new state call pass the name of an event to the transition() method.

    ```
     $this->Model->transition('event');

    ```

    In this example if the Placement model was in the 'interviews' state it would transition to the 'placed' state.

    ```
     $this->Placement->transition('select_successful_candidate');

    ```
4. Every state change is recorded in the database. Every model that uses the StateMachine behavior will have an appropriate state model named ModelnameState. The model is related to this with a hasMany relationship. You can therefore use this model to look up state changes however you want.

    ```
     $this->Placement->PlacementState->findByPlacementId($placement_id);

    ```

    You can also find the current state of a model from the getCurrentState() method.

    ```
     // set model id if not already set
     $this->Placement->id = $placement_id;
     $this->Placement->getCurrentState(); // returns 'placed'

    ```

Notes
-----

[](#notes)

- The current state of a record is also stored in the 'state' column of the model. This is to make it easier when querying the model by state.
- A callback method is supported for state changes. This should be titled \_onState(). For example, to run a method when the model changes to a state named 'placed' add the following method to your model.

    ```
      public function _onStatePlaced() {}

    ```
- You can check whether a state is currently set by calling is&lt;NameOfState&gt;(). For example to check whether a Placement record is currently set to shortlisted call the following.

    ```
      $this->Placement->isShortlisted();

    ```

###  Health Score

27

—

LowBetter than 47% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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.

### Community

Maintainers

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

---

Top Contributors

[![graemetait](https://avatars.githubusercontent.com/u/64014?v=4)](https://github.com/graemetait "graemetait (30 commits)")

### Embed Badge

![Health badge](/badges/burriko-cake-state-machine/health.svg)

```
[![Health](https://phpackages.com/badges/burriko-cake-state-machine/health.svg)](https://phpackages.com/packages/burriko-cake-state-machine)
```

###  Alternatives

[laravel/socialite

Laravel wrapper around OAuth 1 &amp; OAuth 2 libraries.

5.7k104.3M836](/packages/laravel-socialite)[laravel/dusk

Laravel Dusk provides simple end-to-end testing and browser automation.

1.9k38.6M289](/packages/laravel-dusk)[nineinchnick/edatatables

Grid widget for the Yii Framework, wrapper for the DataTables jQuery plugin

173.2k](/packages/nineinchnick-edatatables)[link-cloud/fast-hyperf

LinkCloud Fast Hyperf

241.2k1](/packages/link-cloud-fast-hyperf)

PHPackages © 2026

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