[ Index ]
 

Code source de Symfony 1.0.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/addon/propel/validator/ -> sfPropelUniqueValidator.class.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the symfony package.
   5   * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
   6   * 
   7   * For the full copyright and license information, please view the LICENSE
   8   * file that was distributed with this source code.
   9   */
  10  
  11  /**
  12   * sfPropelUniqueValidator validates that the uniqueness of a column.
  13   * This validator only works for single column primary key.
  14   *
  15   * <b>Required parameters:</b>
  16   *
  17   * # <b>class</b>        - [none]               - Propel class name.
  18   * # <b>column</b>       - [none]               - Propel column name.
  19   *
  20   * <b>Optional parameters:</b>
  21   *
  22   * # <b>unique_error</b> - [Uniqueness error]   - An error message to use when
  23   *                                                the value for this column already
  24   *                                                exists in the database.
  25   *
  26   * @package    symfony
  27   * @subpackage validator
  28   * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
  29   * @author     Fédéric Coelho <frederic.coelho@symfony-project.com>
  30   * @version    SVN: $Id: sfPropelUniqueValidator.class.php 2995 2006-12-09 18:01:32Z fabien $
  31   */
  32  class sfPropelUniqueValidator extends sfValidator
  33  {
  34    public function execute(&$value, &$error)
  35    {
  36      $className  = $this->getParameter('class').'Peer';
  37      $columnName = call_user_func(array($className, 'translateFieldName'), $this->getParameter('column'), BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_COLNAME);
  38  
  39      $c = new Criteria();
  40      $c->add($columnName, $value);
  41      $object = call_user_func(array($className, 'doSelectOne'), $c);
  42  
  43      if ($object)
  44      {
  45        $tableMap = call_user_func(array($className, 'getTableMap'));
  46        foreach ($tableMap->getColumns() as $column)
  47        {
  48          if (!$column->isPrimaryKey())
  49          {
  50            continue;
  51          }
  52  
  53          $method = 'get'.$column->getPhpName();
  54          $primaryKey = call_user_func(array($className, 'translateFieldName'), $column->getPhpName(), BasePeer::TYPE_PHPNAME, BasePeer::TYPE_FIELDNAME);
  55          if ($object->$method() != $this->getContext()->getRequest()->getParameter($primaryKey))
  56          {
  57            $error = $this->getParameter('unique_error');
  58  
  59            return false;
  60          }
  61        }
  62      }
  63  
  64      return true;
  65    }
  66  
  67    /**
  68     * Initialize this validator.
  69     *
  70     * @param sfContext The current application context.
  71     * @param array   An associative array of initialization parameters.
  72     *
  73     * @return bool true, if initialization completes successfully, otherwise false.
  74     */
  75    public function initialize($context, $parameters = null)
  76    {
  77      // initialize parent
  78      parent::initialize($context);
  79  
  80      // set defaults
  81      $this->setParameter('unique_error', 'Uniqueness error');
  82  
  83      $this->getParameterHolder()->add($parameters);
  84  
  85      // check parameters
  86      if (!$this->getParameter('class'))
  87      {
  88        throw new sfValidatorException('The "class" parameter is mandatory for the sfPropelUniqueValidator validator.');
  89      }
  90  
  91      if (!$this->getParameter('column'))
  92      {
  93        throw new sfValidatorException('The "column" parameter is mandatory for the sfPropelUniqueValidator validator.');
  94      }
  95  
  96      return true;
  97    }
  98  }


Généré le : Fri Mar 16 22:42:14 2007 par Balluche grâce à PHPXref 0.7