2017 © Pedro PelĆ”ez
 

cakephp-plugin cakephp-multi-column-uniqueness

A CakePHP Plugin for Validating Composite Unique Indices

image

ravage84/cakephp-multi-column-uniqueness

A CakePHP Plugin for Validating Composite Unique Indices

  • Friday, October 13, 2017
  • by ravage84
  • Repository
  • 2 Watchers
  • 8 Stars
  • 2,142 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 5 Forks
  • 5 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

MultiColumnUniqueness

Travis-CI Build Status Coverage Status Scrutinizer Code Quality Total Downloads Latest Stable Version, (*1)

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

Requirements

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

  • CakePHP 2.4.1 - 2.5.4
  • PHP 5.4.7
  • MySQL

But it should work with, (*4)

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

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

See the CakePHP plugin installation guide in the CakePHP Cookbook., (*5)

Reporting issues

If you have an problem with this plugin please open an issue. But check if the problem has already been reported., (*6)

Contributing

If you'd like to contribute to this project, check out the open issues for any planned features or open bugs. You can fork the project, add features, documentation, and send pull requests; or open issues., (*7)

Versioning

This plugin adheres to SemVer (Semantic Versioning). Since the API is still in development, the plugin hasn't reached 1.0 yet., (*8)

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..., (*9)

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., (*10)

As you may know you could use the CakePHP built-in data validation rule named isUnique. This rule even takes multiple columns, which you could implement like this:, (*11)

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., (*12)

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., (*13)

To enable the MultiColumnUniquenessBehavior for these fields you need to set the $actsAs property in the Product model., (*14)

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., (*15)

Additionally you could also choose a custom validation error message like this:, (*16)

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:, (*17)

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:, (*18)

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., (*19)

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., (*20)

TODOs

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 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. Also I asked dereuromark if he knew if there was an existing solution. He pointed me to his extended model called MyModel in his Tools project., (*21)

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)., (*22)

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., (*23)

The Versions

13/10 2017

dev-master

9999999-dev https://github.com/ravage84/cakephp-multi-column-uniqueness

A CakePHP Plugin for Validating Composite Unique Indices

  Sources   Download

MIT

The Requires

 

The Development Requires

plugin cakephp unique data validation multi column uniqueness multi column

21/02 2017

0.4.0

0.4.0.0 https://github.com/ravage84/cakephp-multi-column-uniqueness

A CakePHP Plugin for Validating Composite Unique Indices

  Sources   Download

MIT

The Requires

 

The Development Requires

plugin cakephp unique data validation multi column uniqueness multi column

10/11 2014

0.3.0

0.3.0.0 https://github.com/ravage84/cakephp-multi-column-uniqueness

A CakePHP Plugin for Validating Composite Unique Indices

  Sources   Download

MIT

The Requires

 

The Development Requires

plugin cakephp unique data validation multi column uniqueness multi column

04/11 2014

dev-appveyor

dev-appveyor https://github.com/ravage84/MultiColumnUniqueness

A CakePHP Plugin for Validating Composite Unique Indices

  Sources   Download

MIT

The Requires

 

The Development Requires

plugin cakephp unique data validation multi column uniqueness multi column