Development

/branches/1.3/lib/plugins/sfDoctrinePlugin/lib/validator/sfDoctrineUniqueValidator.class.php

You must first sign up to be able to contribute.

root/branches/1.3/lib/plugins/sfDoctrinePlugin/lib/validator/sfDoctrineUniqueValidator.class.php

Revision 24575, 3.1 kB (checked in by fabien, 4 years ago)

[1.3] fixed typo (closes #7742)

  • 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) Fabien Potencier <fabien.potencier@symfony-project.com>
6  * (c) Jonathan H. Wage <jonwage@gmail.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 /**
13  * sfDoctrineUniqueValidator validates that the value does not already exists
14  *
15  * <b>Required parameters:</b>
16  *
17  * # <b>class</b>         - [none]               - Doctrine class name.
18  * # <b>column</b>        - [none]               - Doctrine 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 doctrine
28  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
29  * @author     Jonathan H. Wage <jonwage@gmail.com>
30  * @version    SVN: $Id$
31  */
32 class sfDoctrineUniqueValidator extends sfValidator
33 {
34   /**
35    * execute
36    *
37    * @param string $value
38    * @param string $error
39    * @return void
40    */
41   public function execute(&$value, &$error)
42   {
43     $className  = $this->getParameter('class');
44     $columnName = $className.'.'.$this->getParameter('column');
45
46     $primaryKeys = Doctrine_Core::getTable($className)->getIdentifier();
47     if (!is_array($primaryKeys))
48     {
49       $primaryKeys = array($primaryKeys);
50     }
51
52     // implied assumption: the is at least one primary key
53     foreach ($primaryKeys as $primaryKey)
54     {
55       if (null === $primaryKeyValue = $this->getContext()->getRequest()->getParameter($primaryKey))
56       {
57         break;
58       }
59     }
60
61     $query = Doctrine_Query::create()
62               ->from($className);
63
64     if ($primaryKeyValue == null)
65     {
66       $query->where($columnName.' = ?');
67       $res = $query->execute(array($value));
68     } else {
69       $query->where($columnName.' = ? AND '.$primaryKey.' != ?');
70       $res = $query->execute(array($value, $primaryKeyValue));
71     }
72
73     if ($res->count())
74     {
75       $error = $this->getParameterHolder()->get('unique_error');
76
77       return false;
78     }
79
80     return true;
81   }
82
83   /**
84    * Initialize this validator.
85    *
86    * @param sfContext The current application context.
87    * @param array   An associative array of initialization parameters.
88    *
89    * @return bool true, if initialization completes successfully, otherwise false.
90    */
91   public function initialize($context, $parameters = null)
92   {
93     // initialize parent
94     parent::initialize($context);
95
96     // set defaults
97     $this->setParameter('unique_error', 'Uniqueness error');
98
99     $this->getParameterHolder()->add($parameters);
100
101     // check parameters
102     if (!$this->getParameter('class'))
103     {
104       throw new sfValidatorException('The "class" parameter is mandatory for the sfDoctrineUniqueValidator validator.');
105     }
106
107     if (!$this->getParameter('column'))
108     {
109       throw new sfValidatorException('The "column" parameter is mandatory for the sfDoctrineUniqueValidator validator.');
110     }
111
112     return true;
113   }
114 }
Note: See TracBrowser for help on using the browser.