Development

/branches/1.0/lib/addon/propel/validator/sfPropelUniqueValidator.class.php

You must first sign up to be able to contribute.

root/branches/1.0/lib/addon/propel/validator/sfPropelUniqueValidator.class.php

Revision 8045, 3.2 kB (checked in by Carl.Vondrick, 6 years ago)

1.0: added warning to sfPropelUniqueValidator (refs #2467)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
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  * Warning: sfPropelUniqueValidator is susceptible to race conditions.  Although
27  * unlikely, in multiuser environments, the result may change the instant it
28  * returns.  You should still be ready to handle a duplicate INSERT error. 
29  *
30  * @package    symfony
31  * @subpackage validator
32  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
33  * @author     Fédéric Coelho <frederic.coelho@symfony-project.com>
34  * @version    SVN: $Id$
35  */
36 class sfPropelUniqueValidator extends sfValidator
37 {
38   public function execute(&$value, &$error)
39   {
40     $className  = $this->getParameter('class').'Peer';
41     $columnName = call_user_func(array($className, 'translateFieldName'), $this->getParameter('column'), BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_COLNAME);
42
43     $c = new Criteria();
44     $c->add($columnName, $value);
45     $object = call_user_func(array($className, 'doSelectOne'), $c);
46
47     if ($object)
48     {
49       $tableMap = call_user_func(array($className, 'getTableMap'));
50       foreach ($tableMap->getColumns() as $column)
51       {
52         if (!$column->isPrimaryKey())
53         {
54           continue;
55         }
56
57         $method = 'get'.$column->getPhpName();
58         $primaryKey = call_user_func(array($className, 'translateFieldName'), $column->getPhpName(), BasePeer::TYPE_PHPNAME, BasePeer::TYPE_FIELDNAME);
59         if ($object->$method() != $this->getContext()->getRequest()->getParameter($primaryKey))
60         {
61           $error = $this->getParameter('unique_error');
62
63           return false;
64         }
65       }
66     }
67
68     return true;
69   }
70
71   /**
72    * Initialize this validator.
73    *
74    * @param sfContext The current application context.
75    * @param array   An associative array of initialization parameters.
76    *
77    * @return bool true, if initialization completes successfully, otherwise false.
78    */
79   public function initialize($context, $parameters = null)
80   {
81     // initialize parent
82     parent::initialize($context);
83
84     // set defaults
85     $this->setParameter('unique_error', 'Uniqueness error');
86
87     $this->getParameterHolder()->add($parameters);
88
89     // check parameters
90     if (!$this->getParameter('class'))
91     {
92       throw new sfValidatorException('The "class" parameter is mandatory for the sfPropelUniqueValidator validator.');
93     }
94
95     if (!$this->getParameter('column'))
96     {
97       throw new sfValidatorException('The "column" parameter is mandatory for the sfPropelUniqueValidator validator.');
98     }
99
100     return true;
101   }
102 }
103
Note: See TracBrowser for help on using the browser.