25

symfony自定义验证数据库某个字段数据唯一性

分类:Symfony | 1查看67次 | 给我留言

验证数据库的某个字段是否已经存在了相同的数据

首先,在lib目录建立一个 sfCustomUniqueValidator.php

  1. <?php
  2.   /**
  3. * sfCustomUniqueValidator checks if a record exist in the database with all the mentionned fields.
  4. *
  5. * ex: Check if a companie with company_name exist in country_id
  6. *   class:            sfCustomUniqueValidator
  7. *   param:
  8. *     class:          Companies    //the class on which the search is performed
  9. *     nb_fields:      2            //the number of fields on which the comparison is done
  10. *     field_1:        company_name //First field of the comparison
  11. *     field_2:        country_id   //Other country for the comparison
  12. *
  13. * @package    lib
  14. * @author     Joachim Martin
  15. * @date       15/06/2007
  16. */
  17.  
  18. class sfCustomUniqueValidator extends sfValidator {
  19.  
  20.    /**
  21.    * Executes this validator.
  22.    *
  23.    * @param mixed A file or parameter value/array
  24.    * @param error An error message reference
  25.    *
  26.    * @return bool true, if this validator executes successfully, otherwise false
  27.    */
  28.  
  29.     public function execute(&$value, &$error) {
  30.  
  31.         $className  = $this->getParameter('class').'Peer';
  32.  
  33.         //Get fields number
  34.         $nb_fields = $this->getParameter('nb_fields');
  35.  
  36.         //Define new criteria      
  37.         $c = new Criteria();
  38.  
  39.         //Loop on the fields
  40.         for($i = 1; $i <= $nb_fields ; $i++) {
  41.             //Retrieve field_$i
  42.             $check_param = $this->getParameterHolder()->get("field_$i");
  43.             $check_value = $this->getContext()->getRequest()->getParameter($check_param);
  44.  
  45.             //If check value defined       
  46.             if ($check_value != '') {  
  47.                 //Adding field to the criteria
  48.                 $columnName = call_user_func(array($className, 'translateFieldName'), $check_param, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_COLNAME);
  49.                 $c->add($columnName, $check_value);
  50.             }
  51.         }
  52.  
  53.         $object = call_user_func(array($className, 'doSelectOne'), $c);
  54.  
  55.         if ($object)
  56.         {
  57.           $tableMap = call_user_func(array($className, 'getTableMap'));
  58.           foreach ($tableMap->getColumns() as $column)
  59.           {
  60.             if (!$column->isPrimaryKey())
  61.             {
  62.               continue;
  63.             }
  64.  
  65.             $method = 'get'.$column->getPhpName();
  66.             $primaryKey = call_user_func(array($className, 'translateFieldName'), $column->getPhpName(), BasePeer::TYPE_PHPNAME, BasePeer::TYPE_FIELDNAME);
  67.             if ($object->$method() != $this->getContext()->getRequest()->getParameter($primaryKey))
  68.             {
  69.               $error = $this->getParameter('custom_unique_error');
  70.  
  71.               return false;
  72.             }
  73.           }
  74.         }
  75.  
  76.         return true;
  77.     } 
  78.  
  79.     public function initialize ($context, $parameters = null) {
  80.         // initialize parent
  81.         parent::initialize($context);
  82.  
  83.         //Set default parameters value
  84.         $this->setParameter('custom_unique_error','The value is not unique');
  85.  
  86.         $this->getParameterHolder()->add($parameters);
  87.  
  88.         // check parameters
  89.         if (!$this->getParameter('class'))
  90.         {
  91.           throw new sfValidatorException('The "class" parameter is mandatory for the sfCustomUniqueValidator validator.');
  92.         }
  93.  
  94.         if (!$this->getParameter('nb_fields'))
  95.         {
  96.           throw new sfValidatorException('The "nb_fields" parameter is mandatory for the sfCustomUniqueValidator validator.');
  97.         }
  98.  
  99.         return true;
  100.     }
  101. }

调用方法:
下面的代码检测companie表中是否已经有相同的记录

sfCustomUniqueValidator:
class: Companies
nb_fields: 3
field_1: company_name
field_2: activity_id
field_3: country_id
custom_unique_error: This company already exist for this country

class: 用来测试的model
nb_fields: 有几个字段要检测
field_x: 要检测的字段
custom_unique_error: 错误信息

symfony 1.0.4 released symfony 1.0.5 released
  • 标签 : 
  • 原文链接 : http://www.liumin.name/20070625/symfony-custom-unique-validator/
  • 转载原创文章请注明 : 刘敏的Blog
  • 收藏到 : Google书签 新浪ViVi 365Key网摘 天极网摘 我摘 POCO网摘 博采网摘 YouNote网摘 和讯网摘 博拉网 igooi网摘 I2Key网摘 天下图摘 百特门网摘 Del.icio.us Yahoo书签 奇贴 QQ娱乐摘 添加到Digg! 添加到Facebook!
  • 发表留言