Skip to content

Instantly share code, notes, and snippets.

@cirpo
Created July 14, 2010 22:08
Show Gist options
  • Select an option

  • Save cirpo/476173 to your computer and use it in GitHub Desktop.

Select an option

Save cirpo/476173 to your computer and use it in GitHub Desktop.
sfValidatorDoctrineUnique
<?php
protected function doClean($values)
{
$originalValues = $values;
$table = Doctrine_Core::getTable($this->getOption('model'));
if (!is_array($this->getOption('column')))
{
$this->setOption('column', array($this->getOption('column')));
}
//if $values isn't an array, make it one
if (!is_array($values))
{
//use first column for key
$columns = $this->getOption('column');
$values = array($columns[0] => $values);
}
$q = Doctrine_Core::getTable($this->getOption('model'))->createQuery('a');
foreach ($this->getOption('column') as $column)
{
$colName = $table->getColumnName($column);
if (!array_key_exists($column, $values))
{
// one of the column has be removed from the form
return $originalValues;
}
$q->addWhere('a.' . $colName . ' = ?', $values[$column]);
}
$object = $q->fetchOne();
// if no object or if we're updating the object, it's ok
if (!$object || $this->isUpdate($object, $values))
{
return $originalValues;
}
$error = new sfValidatorError($this, 'invalid', array('column' => implode(', ', $this->getOption('column'))));
if ($this->getOption('throw_global_error'))
{
throw $error;
}
$columns = $this->getOption('column');
throw new sfValidatorErrorSchema($this, array($columns[0] => $error));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment