2010-03-17 51 views
7

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

该系统已经构建在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); 
} 
+1

请注意,较新版本的CakePHP2.x支持http://book.cakephp.org/2.0/en/models/data-validation.html#Model::Validation::isUnique的数组,以便默认允许多个字段现在。 – mark 2014-06-06 10:51:04

回答

12

我贴在CakePHP的谷歌集团解决这个:

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

感谢您的入门!尽管如此,我不得不修改它以使其适用于我的情况。将使用最终代码编辑原始问题。 – jodeci 2010-03-18 07:13:37

+0

'if(isset($ this-> data [$ this-> name] [$ this-> primaryKey])){$ this-> primaryKey] =“<>”。$ this-> data [$ this-> name] [$ this- > primaryKey]; '应该被删除,因为Cake 2.x在'isUnique'方法中自行检查。更糟糕的是,它使得isUnique对我来说返回不正确。 – OlivierH 2016-01-06 14:36:39

-2

至于我记得,你必须使用在模型中beforeSave方法这种执法。我有一个要求,一个对象至少有一个N fks被设置,我只能这样做。

编辑:尝试this thread看看是否有任何解决您的问题。

-1

是的,没有。

是的,你必须自己编码,但在CakePHP验证组件中。

验证组件具有允许自定义验证规则的机制。基本上,你把一个函数名称放在$ validate中(就像你通常那样)。您必须定义该功能;在这种情况下,它非常简单(只需执行你的double isUnique要求)。

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

11

checkUnique只能写成isUnique的包装。

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

,并在模型验证时:

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' 
     ) 
    ) 
); 
+0

这比最初接受的答案简单得多。 – 2013-12-03 09:13:19

+1

@KimStacks当我编写我原来的答案 – RichardAtHome 2014-06-13 15:57:48

+0

明白这一功能在CakePHP中不可用。感谢告诉我@RichardAtHome – 2014-06-14 05:27:08

-1

在被打在头上,肩上的提供非CakePHP的解决方案的风险,让我来介绍以下。

在您的数据库中创建一个唯一的索引,但需要多列。此

标准的SQL语法是:

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

了 “的try/catch” 块内将您的 “$这个 - >型号 - >保存()” 命令。在“catch”块中,测试错误代码的异常。在MySQL中,唯一的密钥违规是错误代码23000,但您应该为其他可能的错误做好准备。

它快速简单,不涉及计数数组括号。

您应该总是将数据库访问代码置于“try/catch”块中。部分异常处理应包括记录任何意外的错误消息。你不能指望CakePHP为你做所有的东西

+0

我看到我的回答被拒绝了。不管。我认为我的解决方案更简单,可能更快,并且确实涉及比指定的“正确”答案更少的编码。 – UncaAlby 2017-12-31 20:53:43

2

From cakePHP 2.0 documentation:

您可以验证一组领域是独一无二的,提供多个字段,并设置$或假:

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

确保包括在字段时列表中的原始字段在多个领域制定独特的规则。

如果列出的字段未包含在模型数据中,则将其视为空值。您可以考虑根据需要标记列出的字段。