. */ /** * Doctrine_Validator_Unique * * @package Doctrine * @subpackage Validator * @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @link www.phpdoctrine.org * @since 1.0 * @version $Revision: 6398 $ * @author Konsta Vesterinen */ class Doctrine_Validator_Unique { /** * checks if given value is unique * * @param mixed $value * @return boolean */ public function validate($value) { 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(); if ( ! ($state == Doctrine_Record::STATE_TDIRTY || $state == Doctrine_Record::STATE_TCLEAN)) { foreach ((array) $table->getIdentifierColumnNames() as $pk) { $sql .= ' AND ' . $conn->quoteIdentifier($pk) . ' != ?'; $pkFieldName = $table->getFieldName($pk); $values[] = $this->invoker->$pkFieldName; } } $stmt = $table->getConnection()->getDbh()->prepare($sql); $stmt->execute($values); return ( ! is_array($stmt->fetch())); } }