Development

/branches/1.2/lib/plugins/sfDoctrinePlugin/lib/validator/sfValidatorDoctrineChoice.class.php

You must first sign up to be able to contribute.

root/branches/1.2/lib/plugins/sfDoctrinePlugin/lib/validator/sfValidatorDoctrineChoice.class.php

Revision 20026, 2.5 kB (checked in by Jonathan.Wage, 5 years ago)

[1.2, 1.3] Fixing sfValidatorDoctrineChoice to get alias from query instead of hardcoding a (closes #6740)

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  * sfValidatorDoctrineChoice validates that the value is one of the rows of a table.
14  *
15  * @package    symfony
16  * @subpackage doctrine
17  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
18  * @author     Jonathan H. Wage <jonwage@gmail.com>
19  * @version    SVN: $Id: sfValidatorDoctrineChoice.class.php 8804 2008-05-06 12:11:10Z fabien $
20  */
21 class sfValidatorDoctrineChoice extends sfValidatorBase
22 {
23   /**
24    * Configures the current validator.
25    *
26    * Available options:
27    *
28    *  * model:      The model class (required)
29    *  * alias:      The alias of the root component used in the query
30    *  * query:      A query to use when retrieving objects
31    *  * column:     The column name (null by default which means we use the primary key)
32    *                must be in field name format
33    *  * connection: The Doctrine connection to use (null by default)
34    *
35    * @see sfValidatorBase
36    */
37   protected function configure($options = array(), $messages = array())
38   {
39     $this->addRequiredOption('model');
40     $this->addOption('alias', 'a');
41     $this->addOption('query', null);
42     $this->addOption('column', null);
43     $this->addOption('connection', null);
44   }
45
46   /**
47    * @see sfValidatorBase
48    */
49   protected function doClean($value)
50   {
51     $a = ($q = $this->getOption('query')) ? $q->getRootAlias():$this->getOption('alias');
52     $q = is_null($this->getOption('query')) ? Doctrine_Query::create()->from($this->getOption('model') . ' ' . $a) : $this->getOption('query');
53     $q->addWhere($a . '.' . $this->getColumn() . ' = ?', $value);
54
55     $object = $q->fetchOne();
56     
57     if (!$object)
58     {
59       throw new sfValidatorError($this, 'invalid', array('value' => $value));
60     }
61
62     return $value;
63   }
64
65   /**
66    * Returns the column to use for comparison.
67    *
68    * The primary key is used by default.
69    *
70    * @return string The column name
71    */
72   protected function getColumn()
73   {
74     $table = Doctrine::getTable($this->getOption('model'));
75     if ($this->getOption('column'))
76     {
77       $columnName = $this->getOption('column');
78     }
79     else
80     {
81       $identifier = (array) $table->getIdentifier();
82       $columnName = current($identifier);
83     }
84
85     return $table->getColumnName($columnName);
86   }
87 }
88
Note: See TracBrowser for help on using the browser.