cakephp是2个领域的独特?

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了cakephp是2个领域的独特?脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个注册表,用户可以在其中填写两个邮件地址(email1& email2).市场营销的要求是它们需要是唯一的(如果我们有10个用户则是唯一的,那么将有10 * 2 = 20个唯一的电子邮件地址).

该系统已经基于cakePHP构建,所以我想知道的是,有什么类似于isunique功能(在一个字段中是唯一的)可以直接执行此操作吗?或者我注定要自己编码?提前致谢.

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

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);
}
我在CakePHP GOOGLE Group上发布了一个解决方案:

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

将以下内容添加到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); 
        } 
}

并在您的模型验证中使用:

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是2个领域的独特?全部内容,希望文章能够帮你解决cakephp是2个领域的独特?所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。