{"id":16847739,"url":"https://github.com/ravage84/cakephp-multi-column-uniqueness","last_synced_at":"2025-07-11T18:18:00.378Z","repository":{"id":11005565,"uuid":"13329979","full_name":"ravage84/cakephp-multi-column-uniqueness","owner":"ravage84","description":"A CakePHP 2.x behavior plugin to validate the uniqueness of multiple columns of a model","archived":false,"fork":false,"pushed_at":"2022-03-04T10:40:48.000Z","size":83,"stargazers_count":8,"open_issues_count":5,"forks_count":4,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-20T15:40:17.559Z","etag":null,"topics":["cakephp","cakephp-plugin","data-validation","uniqueness"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ravage84.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-10-04T17:17:36.000Z","updated_at":"2017-03-04T03:34:30.000Z","dependencies_parsed_at":"2022-09-04T13:10:57.076Z","dependency_job_id":null,"html_url":"https://github.com/ravage84/cakephp-multi-column-uniqueness","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ravage84%2Fcakephp-multi-column-uniqueness","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ravage84%2Fcakephp-multi-column-uniqueness/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ravage84%2Fcakephp-multi-column-uniqueness/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ravage84%2Fcakephp-multi-column-uniqueness/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ravage84","download_url":"https://codeload.github.com/ravage84/cakephp-multi-column-uniqueness/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244912800,"owners_count":20530764,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cakephp","cakephp-plugin","data-validation","uniqueness"],"created_at":"2024-10-13T13:08:56.711Z","updated_at":"2025-03-22T05:31:26.748Z","avatar_url":"https://github.com/ravage84.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"MultiColumnUniqueness\n=====================\n[![Travis-CI Build Status](https://travis-ci.org/ravage84/cakephp-multi-column-uniqueness.png)](https://travis-ci.org/ravage84/cakephp-multi-column-uniqueness)\n[![Coverage Status](https://img.shields.io/coveralls/ravage84/cakephp-multi-column-uniqueness.svg)](https://coveralls.io/r/ravage84/cakephp-multi-column-uniqueness?branch=master)\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/ravage84/cakephp-multi-column-uniqueness/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/ravage84/cakephp-multi-column-uniqueness/?branch=master)\n[![Total Downloads](https://poser.pugx.org/ravage84/cakephp-multi-column-uniqueness/d/total.png)](https://packagist.org/packages/ravage84/cakephp-multi-column-uniqueness)\n[![Latest Stable Version](https://poser.pugx.org/ravage84/cakephp-multi-column-uniqueness/v/stable.png)](https://packagist.org/packages/ravage84/cakephp-multi-column-uniqueness)\n\nA CakePHP 2.x behavior plugin to validate the uniqueness of multiple columns of a model.\n\n## Requirements\n\nThe plugin has been tested with the following setup(s)\n\n* CakePHP 2.4.1 - 2.5.4\n* PHP 5.4.7\n* MySQL\n\nBut it should work with\n\n* CakePHP 2.4.1 or greater.\n* PHP 5.2.8 or greater.\n* Other data sources\n\n## Installation\n\n* Clone/Copy the files in this directory into `app/Plugin/MultiColumnUniqueness`\n* Ensure the plugin is loaded in `app/Config/bootstrap.php` by calling `CakePlugin::load('MultiColumnUniqueness');` or `CakePlugin::loadAll();`\n\n### Using Composer\n\nSee the [CakePHP plugin installation guide](http://book.cakephp.org/2.0/en/plugins/how-to-install-plugins.html#composer) in the CakePHP Cookbook.\n\n## Reporting issues\n\nIf you have an problem with this plugin please [open an issue](https://github.com/ravage84/cakephp-multi-column-uniqueness/issues/new).\nBut check if [the problem has already been reported](https://github.com/ravage84/cakephp-multi-column-uniqueness/issues).\n\n## Contributing\n\nIf 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.\nYou can fork the project, add features, documentation, and send pull requests; or [open issues](https://github.com/ravage84/cakephp-multi-column-uniqueness/issues/new).\n\n# Versioning\n\nThis plugin adheres to [SemVer (Semantic Versioning)](http://semver.org/spec/v2.0.0.html).\nSince the API is still in development, the plugin hasn't reached 1.0 yet.\n\n# CakePHP Version Support\n\nThere are no plans to backport this to CakePHP 1.x.\nThe plugin should be updated to support future 2.x releases.\nFor CakePHP 3.0 it will need a total rewrite most probably...\n\n## How To Use\n\nLet's say you have a model ``Product`` which has many fields\nbut two of them need to be unique in conjunction with each other.\nThose fields are ``name`` and ``manufacturer_id``.\n\nAs you may know you *could* use the CakePHP built-in data validation rule named\n[isUnique](http://book.cakephp.org/2.0/en/models/data-validation.html#Model::Validation::isUnique).\nThis rule even takes multiple columns, which you could implement like this:\n\n````php\npublic $validate = array(\n\t'first_name' =\u003e array(\n\t\t'unique_first_last' =\u003e array(\n\t\t\t'rule'    =\u003e array('checkMultiColumnUnique', array('first_name', 'last_name'), false)\n\t\t\t'message' =\u003e 'The first and last name must be unique.'\n\t\t)\n\t),\n\t// Additionally/optionally the same for the 'last_name' field\n;)\n\n\npublic function checkMultiColumnUnique($ignoredData, $fields, $or = true) {\n\t\treturn $this-\u003eisUnique($fields, $or);\n}\n````\n\nSo why bother using this plugin?\nBecause the solution above has a few downsides.\nFirst either you setup the same rule for both/all fields that need to be unique\nor you just set it for one of the fields.\n\nIf you set it for all fields the rule will be executed for each field\nresulting in multiple (and redundant) SQL queries.\nIf you set it for only one field on the other hand, you have to include that field\nin the data array every time you want to save one of the fields that need to be unique.\nThis plugins circumvents this by adding the rule dynamically.\n\nTo enable the MultiColumnUniquenessBehavior for these fields\nyou need to set the ``$actsAs`` property in the ``Product`` model.\n\n````php\npublic $actsAs = array('MultiColumnUniqueness.MultiColumnUniqueness' =\u003e array(\n\t'fields' =\u003e array('name', 'manufacturer_id')\n));\n````\n\nThis is the simplest form of setup.\nIt would output a ``The fields mane and manufacturer_id must be unique.``\nerror message when the validation fails.\n\nAdditionally you could also choose a custom validation error message like this:\n\n````php\npublic $actsAs = array('MultiColumnUniqueness.MultiColumnUniqueness' =\u003e array(\n\t'fields' =\u003e array('name', 'manufacturer_id'),\n\t'errMsg' =\u003e \"This name and manufacturer ID can't be used twice.\"\n));\n````\n\nNow let's say you also have three other fields named ``field1``, ``field2``, ``field13``\nwhich need to be unique in conjunction with each other in the same model.\nIf you don't need custom error messages, you can set it up like this:\n\n````php\npublic $actsAs = array('MultiColumnUniqueness.MultiColumnUniqueness' =\u003e array(\n\t'fields' =\u003e array(\n\t\tarray('name', 'manufacturer_id'),\n\t\tarray('field1', 'field2', 'field3'),\n\t)\n));\n````\n\nFor the same situation but with custom error messages, do it like this:\n\n````php\npublic $actsAs = array('MultiColumnUniqueness.MultiColumnUniqueness' =\u003e array(\n\t'fields' =\u003e array(\n\t\tarray('name', 'manufacturer_id'),\n\t\tarray('field1', 'field2', 'field3'),\n\t),\n\t'errMsg' =\u003e array(\n\t\t\"This name and manufacturer ID can't be used twice.\",\n\t\t\"This field1, field2, fiel3 can't be used twice\"),\n));\n````\n\nBy default the data validation rule will be added only to the first field\nwhich needs to be unique found in the data array.\nIf you want to enforce the data validation on each unique field,\nset the ``onlyOnce`` option to false.\nThis way the validation error message will be shown on each unique field.\n\n````php\npublic $actsAs = array('MultiColumnUniqueness.MultiColumnUniqueness' =\u003e array(\n\t'fields' =\u003e array(\n\t\tarray('name', 'manufacturer_id'),\n\t\tarray('field1', 'field2', 'field3'),\n\t),\n\t'onlyOnce' =\u003e false,\n));\n````\nThis option can only be set per model, not per unique field group.\n\n## TODOs\n\n* Running the tests with other databases in Travis, [as CakePHP does](https://github.com/cakephp/cakephp/blob/master/.travis.yml)\n* See the [open issues](https://github.com/ravage84/cakephp-multi-column-uniqueness/issues)\n\n## Background Story\n\nI needed to validate the uniqueness of two fields of a model.\nNot 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)\nbut in conjunction with each other.\nAfter 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).\nAlso I asked [dereuromark](https://github.com/dereuromark) if he knew if there was an existing solution.\nHe 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/).\n\nThose 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.\nSo I decided to write a behavior for it (my first by the way).\n\nAfter I got that to work I thought other people could benefit from it, so I created this project and implemented it as a plugin.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fravage84%2Fcakephp-multi-column-uniqueness","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fravage84%2Fcakephp-multi-column-uniqueness","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fravage84%2Fcakephp-multi-column-uniqueness/lists"}