本文介绍了cakephp isUnique for 2 fields?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个注册表单,用户可以填写两个电子邮件地址(email1& email2)。营销的要求是,他们需要是唯一的(如果我们有10个用户,那么将有10 * 2 = 20个唯一的电子邮件地址)。



系统已经建立在cakephp上,所以我想知道的是,有一些类似于isUnique特性(在一个字段中是唯一的),可以做这是开箱的吗?还是我注定要自己编码?提前感谢。



编辑:建立在理查德的例子上,这对我有用:

 code> 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 $ it-> isUnique($ tmp);
}


解决方案

在CakePHP Google群组上:



将以下内容添加到您的AppModel中:

  / ** 
*检查是字段值在表中是unqiue
*注意:我们覆盖默认cakephp isUnique测试为
原来似乎被打破
*
* @param string $ data未使用(使用$ this->数据)
* @param mnixed $ fields字段名称(或字段名称数组)到
validate
* @return boolean true if组合字段是唯一的
* /
function checkUnique($ data,$ fields){
if(!is_array($ fields)){
$ fields = array );
}
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);
}
}

并用于您的模型验证:

  var $ validate = array(
name=> array(
unique=> array (
rule=> array(checkUnique,array(name,institution_id)),
message=> b $ b机构


);


I have a registration form in which users can fill in two email address (email1 & email2). Marketing's requirement is that they need to be unique (unique as in if we had 10 users, then there would be 10*2=20 unique email address).

The system is already built on cakephp, so what I'd like to know is, is there something similar to the isUnique feature (unique in one field) that can do this right out of the box? Or am I doomed to code 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);
}
解决方案

I posted a solution to this on the CakePHP Google 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 is used in your model validate:

        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"
                        )
                )
       );

这篇关于cakephp isUnique for 2 fields?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 13:50