Doctrine 1.2.4
Doctrine_Validator_Unique Class Reference

Inherits Doctrine_Validator_Driver.

Public Member Functions

 __get ($arg)
 
 __isset ($arg)
 
 __set ($arg, $value)
 
 getArg ($arg)
 
 getArgs ()
 
 setArg ($arg, $value)
 
 validate ($value)
 

Detailed Description

Definition at line 33 of file Unique.php.

Member Function Documentation

Doctrine_Validator_Driver::__get (   $arg)
inherited

__get an alias for getOption

Parameters
string$arg

Definition at line 48 of file Driver.php.

{
if (isset($this->args[$arg])) {
return $this->args[$arg];
}
return null;
}
Doctrine_Validator_Driver::__isset (   $arg)
inherited

__isset

Parameters
string$arg

Definition at line 61 of file Driver.php.

{
return isset($this->args[$arg]);
}
Doctrine_Validator_Driver::__set (   $arg,
  $value 
)
inherited

sets given value to an argument

Parameters
$argthe name of the option to be changed
$valuethe value of the option
Returns
Doctrine_Validator_Driver this object

Definition at line 73 of file Driver.php.

{
$this->args[$arg] = $value;
return $this;
}
Doctrine_Validator_Driver::getArg (   $arg)
inherited

returns the value of an argument

Parameters
$argthe name of the option to retrieve
Returns
mixed the value of the option

Definition at line 86 of file Driver.php.

{
if ( ! isset($this->args[$arg])) {
throw new Doctrine_Validator_Exception('Unknown option ' . $arg);
}
return $this->args[$arg];
}
Doctrine_Validator_Driver::getArgs ( )
inherited

returns all args and their associated values

Returns
array all args as an associative array

Definition at line 114 of file Driver.php.

{
return $this->args;
}
Doctrine_Validator_Driver::setArg (   $arg,
  $value 
)
inherited

sets given value to an argument

Parameters
$argthe name of the option to be changed
$valuethe value of the option
Returns
Doctrine_Validator_Driver this object

Definition at line 102 of file Driver.php.

{
$this->args[$arg] = $value;
return $this;
}
Doctrine_Validator_Unique::validate (   $value)

checks if given value is unique

Parameters
mixed$value
Returns
boolean

Definition at line 41 of file Unique.php.

{
if (is_null($value)) {
return true;
}
$table = $this->invoker->getTable();
$conn = $table->getConnection();
$pks = $table->getIdentifierColumnNames();
if (is_array($pks)) {
for ($i = 0, $l = count($pks); $i < $l; $i++) {
$pks[$i] = $conn->quoteIdentifier($pks[$i]);
}
$pks = implode(', ', $pks);
}
$sql = 'SELECT ' . $pks . ' FROM ' . $conn->quoteIdentifier($table->getTableName()) . ' WHERE ';
if (is_array($this->field)) {
foreach ($this->field as $k => $v) {
$this->field[$k] = $conn->quoteIdentifier($table->getColumnName($v));
}
$sql .= implode(' = ? AND ', $this->field) . ' = ?';
$values = $value;
} else {
$sql .= $conn->quoteIdentifier($table->getColumnName($this->field)) . ' = ?';
$values = array();
$values[] = $value;
}
// If the record is not new we need to add primary key checks because its ok if the
// unique value already exists in the database IF the record in the database is the same
// as the one that is validated here.
$state = $this->invoker->state();
foreach ((array) $table->getIdentifierColumnNames() as $pk) {
$sql .= ' AND ' . $conn->quoteIdentifier($pk) . ' != ?';
$pkFieldName = $table->getFieldName($pk);
$values[] = $this->invoker->$pkFieldName;
}
}
if (isset($this->args) && is_array($this->args) && isset($this->args['where'])) {
$sql .= ' AND ' . $this->args['where'];
}
$stmt = $table->getConnection()->getDbh()->prepare($sql);
$stmt->execute($values);
return ( ! is_array($stmt->fetch()));
}

The documentation for this class was generated from the following file: