| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 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 |
|
|---|