Development

/branches/1.1/lib/plugins/sfPropelPlugin/lib/propel/validator/sfValidatorPropelChoice.class.php

You must first sign up to be able to contribute.

root/branches/1.1/lib/plugins/sfPropelPlugin/lib/propel/validator/sfValidatorPropelChoice.class.php

Revision 15627, 2.4 kB (checked in by Kris.Wallsmith, 6 years ago)

[1.1, 1.2, 1.3] fixed sfValidatorPropelChoice(Many) overrides some Criteria (closes #5924)

  • 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    *
32    * @see sfValidatorBase
33    */
34   protected function configure($options = array(), $messages = array())
35   {
36     $this->addRequiredOption('model');
37     $this->addOption('criteria', null);
38     $this->addOption('column', null);
39     $this->addOption('connection', null);
40   }
41
42   /**
43    * @see sfValidatorBase
44    */
45   protected function doClean($value)
46   {
47     $criteria = is_null($this->getOption('criteria')) ? new Criteria() : clone $this->getOption('criteria');
48     $criteria->addAnd($this->getColumn(), $value);
49
50     $object = call_user_func(array($this->getOption('model').'Peer', 'doSelectOne'), $criteria, $this->getOption('connection'));
51
52     if (is_null($object))
53     {
54       throw new sfValidatorError($this, 'invalid', array('value' => $value));
55     }
56
57     return $value;
58   }
59
60   /**
61    * Returns the column to use for comparison.
62    *
63    * The primary key is used by default.
64    *
65    * @return string The column name
66    */
67   protected function getColumn()
68   {
69     if ($this->getOption('column'))
70     {
71       $columnName = $this->getOption('column');
72     }
73     else
74     {
75       $map = call_user_func(array($this->getOption('model').'Peer', 'getTableMap'));
76       foreach ($map->getColumns() as $column)
77       {
78         if ($column->isPrimaryKey())
79         {
80           $columnName = strtolower($column->getColumnName());
81           break;
82         }
83       }
84     }
85
86     return call_user_func(array($this->getOption('model').'Peer', 'translateFieldName'), $columnName, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_COLNAME);
87   }
88 }
89
Note: See TracBrowser for help on using the browser.