脚本宝典收集整理的这篇文章主要介绍了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); }
/** * 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,请注明来意。