| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 94 |
parent::initialize($context); |
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
$this->setParameter('unique_error', 'Uniqueness error'); |
|---|
| 98 |
|
|---|
| 99 |
$this->getParameterHolder()->add($parameters); |
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 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 |
} |
|---|