PHPackages                             ravage84/cakephp-multi-column-uniqueness - 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. [Validation &amp; Sanitization](/categories/validation)
4. /
5. ravage84/cakephp-multi-column-uniqueness

ActiveCakephp-plugin[Validation &amp; Sanitization](/categories/validation)

ravage84/cakephp-multi-column-uniqueness
========================================

A CakePHP Plugin for Validating Composite Unique Indices

0.4.0(9y ago)82.3k4[5 issues](https://github.com/ravage84/cakephp-multi-column-uniqueness/issues)MITPHPPHP &gt;=5.3.0

Since Nov 4Pushed 4y ago1 watchersCompare

[ Source](https://github.com/ravage84/cakephp-multi-column-uniqueness)[ Packagist](https://packagist.org/packages/ravage84/cakephp-multi-column-uniqueness)[ Docs](https://github.com/ravage84/cakephp-multi-column-uniqueness)[ RSS](/packages/ravage84-cakephp-multi-column-uniqueness/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (4)Versions (4)Used By (0)

MultiColumnUniqueness
=====================

[](#multicolumnuniqueness)

[![Travis-CI Build Status](https://camo.githubusercontent.com/4425a5670569d53ba5ab33044ae33520e37ded5df4b215a1089480952713b9b2/68747470733a2f2f7472617669732d63692e6f72672f72617661676538342f63616b657068702d6d756c74692d636f6c756d6e2d756e697175656e6573732e706e67)](https://travis-ci.org/ravage84/cakephp-multi-column-uniqueness)[![Coverage Status](https://camo.githubusercontent.com/71f9a357a4a937f9423b00310dfafa315b391acea8f7216ec53b34a85dcfc4fe/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f72617661676538342f63616b657068702d6d756c74692d636f6c756d6e2d756e697175656e6573732e737667)](https://coveralls.io/r/ravage84/cakephp-multi-column-uniqueness?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/e0c9dd41aa1f0bae057844fdd922127c7791cf747c8004c1141353d097d0cba6/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f72617661676538342f63616b657068702d6d756c74692d636f6c756d6e2d756e697175656e6573732f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/ravage84/cakephp-multi-column-uniqueness/?branch=master)[![Total Downloads](https://camo.githubusercontent.com/29ddc66015da9ff06ef5a732a4cd1500b4e0783acacbb24d5b553887ff383b75/68747470733a2f2f706f7365722e707567782e6f72672f72617661676538342f63616b657068702d6d756c74692d636f6c756d6e2d756e697175656e6573732f642f746f74616c2e706e67)](https://packagist.org/packages/ravage84/cakephp-multi-column-uniqueness)[![Latest Stable Version](https://camo.githubusercontent.com/a48df6c3173810bf10084d16d5d4df5f5f0747ecdefc2af6991622c4f236c4fc/68747470733a2f2f706f7365722e707567782e6f72672f72617661676538342f63616b657068702d6d756c74692d636f6c756d6e2d756e697175656e6573732f762f737461626c652e706e67)](https://packagist.org/packages/ravage84/cakephp-multi-column-uniqueness)

A CakePHP 2.x behavior plugin to validate the uniqueness of multiple columns of a model.

Requirements
------------

[](#requirements)

The plugin has been tested with the following setup(s)

- CakePHP 2.4.1 - 2.5.4
- PHP 5.4.7
- MySQL

But it should work with

- CakePHP 2.4.1 or greater.
- PHP 5.2.8 or greater.
- Other data sources

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

[](#installation)

- Clone/Copy the files in this directory into `app/Plugin/MultiColumnUniqueness`
- Ensure the plugin is loaded in `app/Config/bootstrap.php` by calling `CakePlugin::load('MultiColumnUniqueness');` or `CakePlugin::loadAll();`

### Using Composer

[](#using-composer)

See the [CakePHP plugin installation guide](http://book.cakephp.org/2.0/en/plugins/how-to-install-plugins.html#composer) in the CakePHP Cookbook.

Reporting issues
----------------

[](#reporting-issues)

If you have an problem with this plugin please [open an issue](https://github.com/ravage84/cakephp-multi-column-uniqueness/issues/new). But check if [the problem has already been reported](https://github.com/ravage84/cakephp-multi-column-uniqueness/issues).

Contributing
------------

[](#contributing)

If you'd like to contribute to this project, check out the [open issues](https://github.com/ravage84/cakephp-multi-column-uniqueness/issues) for any planned features or open bugs. You can fork the project, add features, documentation, and send pull requests; or [open issues](https://github.com/ravage84/cakephp-multi-column-uniqueness/issues/new).

Versioning
==========

[](#versioning)

This plugin adheres to [SemVer (Semantic Versioning)](http://semver.org/spec/v2.0.0.html). Since the API is still in development, the plugin hasn't reached 1.0 yet.

CakePHP Version Support
=======================

[](#cakephp-version-support)

There are no plans to backport this to CakePHP 1.x. The plugin should be updated to support future 2.x releases. For CakePHP 3.0 it will need a total rewrite most probably...

How To Use
----------

[](#how-to-use)

Let's say you have a model `Product` which has many fields but two of them need to be unique in conjunction with each other. Those fields are `name` and `manufacturer_id`.

As you may know you *could* use the CakePHP built-in data validation rule named [isUnique](http://book.cakephp.org/2.0/en/models/data-validation.html#Model::Validation::isUnique). This rule even takes multiple columns, which you could implement like this:

```
public $validate = array(
	'first_name' => array(
		'unique_first_last' => array(
			'rule'    => array('checkMultiColumnUnique', array('first_name', 'last_name'), false)
			'message' => 'The first and last name must be unique.'
		)
	),
	// Additionally/optionally the same for the 'last_name' field
;)

public function checkMultiColumnUnique($ignoredData, $fields, $or = true) {
		return $this->isUnique($fields, $or);
}
```

So why bother using this plugin? Because the solution above has a few downsides. First either you setup the same rule for both/all fields that need to be unique or you just set it for one of the fields.

If you set it for all fields the rule will be executed for each field resulting in multiple (and redundant) SQL queries. If you set it for only one field on the other hand, you have to include that field in the data array every time you want to save one of the fields that need to be unique. This plugins circumvents this by adding the rule dynamically.

To enable the MultiColumnUniquenessBehavior for these fields you need to set the `$actsAs` property in the `Product` model.

```
public $actsAs = array('MultiColumnUniqueness.MultiColumnUniqueness' => array(
	'fields' => array('name', 'manufacturer_id')
));
```

This is the simplest form of setup. It would output a `The fields mane and manufacturer_id must be unique.`error message when the validation fails.

Additionally you could also choose a custom validation error message like this:

```
public $actsAs = array('MultiColumnUniqueness.MultiColumnUniqueness' => array(
	'fields' => array('name', 'manufacturer_id'),
	'errMsg' => "This name and manufacturer ID can't be used twice."
));
```

Now let's say you also have three other fields named `field1`, `field2`, `field13`which need to be unique in conjunction with each other in the same model. If you don't need custom error messages, you can set it up like this:

```
public $actsAs = array('MultiColumnUniqueness.MultiColumnUniqueness' => array(
	'fields' => array(
		array('name', 'manufacturer_id'),
		array('field1', 'field2', 'field3'),
	)
));
```

For the same situation but with custom error messages, do it like this:

```
public $actsAs = array('MultiColumnUniqueness.MultiColumnUniqueness' => array(
	'fields' => array(
		array('name', 'manufacturer_id'),
		array('field1', 'field2', 'field3'),
	),
	'errMsg' => array(
		"This name and manufacturer ID can't be used twice.",
		"This field1, field2, fiel3 can't be used twice"),
));
```

By default the data validation rule will be added only to the first field which needs to be unique found in the data array. If you want to enforce the data validation on each unique field, set the `onlyOnce` option to false. This way the validation error message will be shown on each unique field.

```
public $actsAs = array('MultiColumnUniqueness.MultiColumnUniqueness' => array(
	'fields' => array(
		array('name', 'manufacturer_id'),
		array('field1', 'field2', 'field3'),
	),
	'onlyOnce' => false,
));
```

This option can only be set per model, not per unique field group.

TODOs
-----

[](#todos)

- Running the tests with other databases in Travis, [as CakePHP does](https://github.com/cakephp/cakephp/blob/master/.travis.yml)
- See the [open issues](https://github.com/ravage84/cakephp-multi-column-uniqueness/issues)

Background Story
----------------

[](#background-story)

I needed to validate the uniqueness of two fields of a model. Not separately as CakePHP supports built-in by the [isUnique data validation rule](http://book.cakephp.org/2.0/en/models/data-validation.html#Model::Validation::isUnique)but in conjunction with each other. After I didn't found any built-in solution for my problem I turned to Google and [found some articles about this](http://stackoverflow.com/questions/2461267/cakephp-isunique-for-2-fields). Also I asked [dereuromark](https://github.com/dereuromark) if he knew if there was an existing solution. He pointed me to [his extended model called MyModel](https://github.com/dereuromark/cakephp-tools/blob/0.4/Model/MyModel.php#L903) in his [Tools project](https://github.com/dereuromark/tools/).

Those solutions seemed to work but all of them either weren't up to date, weren't clean enough in their approach or forced me to add way too much code for my narrow needs. So I decided to write a behavior for it (my first by the way).

After I got that to work I thought other people could benefit from it, so I created this project and implemented it as a plugin.

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance8

Infrequent updates — may be unmaintained

Popularity24

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 92.7% 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 ~833 days

Total

2

Last Release

3373d ago

### Community

Maintainers

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

---

Top Contributors

[![ravage84](https://avatars.githubusercontent.com/u/625761?v=4)](https://github.com/ravage84 "ravage84 (101 commits)")[![TekkCraft](https://avatars.githubusercontent.com/u/21214298?v=4)](https://github.com/TekkCraft "TekkCraft (3 commits)")[![liviakuenzli](https://avatars.githubusercontent.com/u/7348876?v=4)](https://github.com/liviakuenzli "liviakuenzli (2 commits)")[![tenebrousedge](https://avatars.githubusercontent.com/u/616105?v=4)](https://github.com/tenebrousedge "tenebrousedge (2 commits)")[![steampilot](https://avatars.githubusercontent.com/u/6819141?v=4)](https://github.com/steampilot "steampilot (1 commits)")

---

Tags

cakephpcakephp-plugindata-validationuniquenessplugincakephpuniquemulti-columndata validationmulti column uniqueness

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/ravage84-cakephp-multi-column-uniqueness/health.svg)

```
[![Health](https://phpackages.com/badges/ravage84-cakephp-multi-column-uniqueness/health.svg)](https://phpackages.com/packages/ravage84-cakephp-multi-column-uniqueness)
```

###  Alternatives

[ichikaway/cakeplus

Cake plus is cakephp plugin and provides some functions for CakePHP.

52101.1k1](/packages/ichikaway-cakeplus)[chrisshick/cakephp3-html-purifier

This is a CakePHP3 Purifier Plugin Behavior that cleanses data before it is marshalled into the entity.

12168.1k](/packages/chrisshick-cakephp3-html-purifier)[dereuromark/cakephp-mailchimp

A CakePHP plugin for MailChimp

2217.9k](/packages/dereuromark-cakephp-mailchimp)

PHPackages © 2026

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