Cakephp isUnique for 2 fields?

I have a registration form in which users can fill out two email addresses (email1 and email2). The requirement for marketing is that they must be unique (unique, as if we had 10 users, then there would be 10 * 2 = 20 unique email addresses).

The system is already built on cakephp, so I would like to know if there is something similar to the isUnique function (unique in one field) that can do this right out of the box? Or am I doomed to encode this myself? Thanks in advance.

EDIT: built on Richard's example, this worked for me:

function checkUnique($data, $fields) { if (!is_array($fields)) { $fields = array($fields); } foreach($data as $key) { $checks = $key; } if (empty($checks)) { return true; //allow null } foreach($fields as $key) { $tmp[$key] = $checks; } if (isset($this->data[$this->name][$this->primaryKey])) { $tmp[$this->primaryKey] = "<>".$this->data[$this->name][$this->primaryKey]; } return $this->isUnique($tmp); } 
+7
cakephp unique-index cakephp-model
source share
6 answers

I posted the solution for this in the Google CakePHP group:

http://groups.google.com/group/cake-php/browse_frm/thread/b3a1e4ae3eeb6091/e168f54bac27c163?lnk=gst&q=checkUnique#e168f54bac27c163

Add the following to your AppModel:

  /** * checks is the field value is unqiue in the table * note: we are overriding the default cakephp isUnique test as the original appears to be broken * * @param string $data Unused ($this->data is used instead) * @param mnixed $fields field name (or array of field names) to validate * @return boolean true if combination of fields is unique */ function checkUnique($data, $fields) { if (!is_array($fields)) { $fields = array($fields); } foreach($fields as $key) { $tmp[$key] = $this->data[$this->name][$key]; } if (isset($this->data[$this->name][$this->primaryKey])) { $tmp[$this->primaryKey] = "<>".$this->data[$this->name][$this- >primaryKey]; } return $this->isUnique($tmp, false); } } 

and used in your model:

  var $validate = array( "name"=>array( "unique"=>array( "rule"=>array("checkUnique", array("name", "institution_id")), "message"=>"A contact with that name already exists for that institution" ) ) ); 
+12
source share

checkUnique can simply be written as a wrapper for isUnique .

 class AppModel extends Model { public function checkUnique($ignoredData, $fields, $or = true) { return $this->isUnique($fields, $or); } } 

and used in your model:

 public $validate = array( 'name' => array( 'unique' => array( 'rule' => array('checkUnique', array('name', 'institution_id'), false), 'message' => 'A contact with that name already exists for that institution' ) ) ); 
+11
source share

From the cakePHP 2.0 documentation:

You can verify that the set of fields is unique by providing multiple fields and setting $ or false:

 public $validate = array( 'email' => array( 'rule' => array('isUnique', array('email', 'username'), false), 'message' => 'This username & email combination has already been used.' ) ); 

Be sure to include the source field in the field list when creating a unique rule for multiple fields.

If the specified field is not included in the model data, then it is considered as a zero value. You may consider marking these fields.

+2
source share

Yes and no.

Yes, you have to code it yourself, but in the CakePHP validation component.

The validation component has a mechanism that allows you to configure custom validation rules. Essentially, you put the function name inside $ validate (as you usually did). You must define a function; in this case it is quite simple (just follow your double isUnique requirement).

http://book.cakephp.org/2.0/en/models/data-validation.html#custom-validation-rules

-one
source share

Due to the risk of being beaten around my head and shoulders for offering a solution other than CakePHP, let me introduce the following.

Create a unique index in your database on any column that you need.

The standard SQL syntax for this is:

 create unique index {IndexName} on {Table} ({Column}, {Column}, ...) 

Place the command "$ this-> Model-> save ()" inside the try / catch block. In the catch block, check the exception for the error code. In MySQL, a unique key violation is error code 23000, but you must also be prepared for other possible errors.

It is quick and easy and does not require counting the brackets of the array.

In any case, you should always place the database access code inside the try / catch block. Part of exception handling should include logging any unexpected error messages. You cannot expect CakePHP to do everything for you.

-one
source share

As far as I remember, you have such coercion using the beforeSave method in the model. I had a requirement for an object to have at least one of N fks, and I could only do that.

Edit: try this thread to see if something solves your problem.

-2
source share

All Articles