Development

/branches/1.2/lib/plugins/sfPropelPlugin/lib/validator/sfValidatorPropelChoice.class.php

You must first sign up to be able to contribute.

root/branches/1.2/lib/plugins/sfPropelPlugin/lib/validator/sfValidatorPropelChoice.class.php

Revision 16976, 3.1 kB (checked in by fabien, 5 years ago)

fixed Propel forms, filters, and admin generator when column names contain uppercase letters (closes #6151, #6187, #5030, #5610, #2855)

  • 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) 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  * sfValidatorPropelChoice validates that the value is one of the rows of a table.
13  *
14  * @package    symfony
15  * @subpackage validator
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
18  */
19 class sfValidatorPropelChoice extends sfValidatorBase
20 {
21   /**
22    * Configures the current validator.
23    *
24    * Available options:
25    *
26    *  * model:      The model class (required)
27    *  * criteria:   A criteria to use when retrieving objects
28    *  * column:     The column name (null by default which means we use the primary key)
29    *                must be in field name format
30    *  * connection: The Propel connection to use (null by default)
31    *  * multiple:   true if the select tag must allow multiple selections
32    *
33    * @see sfValidatorBase
34    */
35   protected function configure($options = array(), $messages = array())
36   {
37     $this->addRequiredOption('model');
38     $this->addOption('criteria', null);
39     $this->addOption('column', null);
40     $this->addOption('connection', null);
41     $this->addOption('multiple', false);
42   }
43
44   /**
45    * @see sfValidatorBase
46    */
47   protected function doClean($value)
48   {
49     $criteria = is_null($this->getOption('criteria')) ? new Criteria() : clone $this->getOption('criteria');
50
51     if ($this->getOption('multiple'))
52     {
53       if (!is_array($value))
54       {
55         $value = array($value);
56       }
57
58       $criteria->addAnd($this->getColumn(), $value, Criteria::IN);
59
60       $objects = call_user_func(array(constant($this->getOption('model').'::PEER'), 'doSelect'), $criteria, $this->getOption('connection'));
61
62       if (count($objects) != count($value))
63       {
64         throw new sfValidatorError($this, 'invalid', array('value' => $value));
65       }
66     }
67     else
68     {
69       $criteria->addAnd($this->getColumn(), $value);
70
71       $object = call_user_func(array(constant($this->getOption('model').'::PEER'), 'doSelectOne'), $criteria, $this->getOption('connection'));
72
73       if (is_null($object))
74       {
75         throw new sfValidatorError($this, 'invalid', array('value' => $value));
76       }
77     }
78
79     return $value;
80   }
81
82   /**
83    * Returns the column to use for comparison.
84    *
85    * The primary key is used by default.
86    *
87    * @return string The column name
88    */
89   protected function getColumn()
90   {
91     if ($this->getOption('column'))
92     {
93       $columnName = $this->getOption('column');
94       $from = BasePeer::TYPE_FIELDNAME;
95     }
96     else
97     {
98       $map = call_user_func(array(constant($this->getOption('model').'::PEER'), 'getTableMap'));
99       foreach ($map->getColumns() as $column)
100       {
101         if ($column->isPrimaryKey())
102         {
103           $columnName = $column->getPhpName();
104           break;
105         }
106       }
107       $from = BasePeer::TYPE_PHPNAME;
108     }
109
110     return call_user_func(array(constant($this->getOption('model').'::PEER'), 'translateFieldName'), $columnName, $from, BasePeer::TYPE_COLNAME);
111   }
112 }
113
Note: See TracBrowser for help on using the browser.