UniqueValidator.php 3.03 KB
Newer Older
w  
Qiang Xue committed
1 2
<?php
/**
Alexander Makarov committed
3
 * UniqueValidator class file.
w  
Qiang Xue committed
4 5
 *
 * @link http://www.yiiframework.com/
Qiang Xue committed
6
 * @copyright Copyright &copy; 2008 Yii Software LLC
w  
Qiang Xue committed
7 8 9
 * @license http://www.yiiframework.com/license/
 */

w  
Qiang Xue committed
10
namespace yii\validators;
Qiang Xue committed
11
use yii\base\InvalidConfigException;
w  
Qiang Xue committed
12

w  
Qiang Xue committed
13 14 15 16
/**
 * CUniqueValidator validates that the attribute value is unique in the corresponding database table.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
Alexander Makarov committed
17
 * @since 2.0
w  
Qiang Xue committed
18
 */
Alexander Makarov committed
19
class UniqueValidator extends Validator
w  
Qiang Xue committed
20 21 22 23 24 25 26
{
	/**
	 * @var boolean whether the attribute value can be null or empty. Defaults to true,
	 * meaning that if the attribute is empty, it is considered valid.
	 */
	public $allowEmpty = true;
	/**
Qiang Xue committed
27
	 * @var string the ActiveRecord class name or alias of the class
Alexander Makarov committed
28
	 * that should be used to look for the attribute value being validated.
Qiang Xue committed
29
	 * Defaults to null, meaning using the ActiveRecord class of the attribute being validated.
w  
Qiang Xue committed
30 31 32 33 34 35 36 37 38 39 40 41 42
	 * @see attributeName
	 */
	public $className;
	/**
	 * @var string the ActiveRecord class attribute name that should be
	 * used to look for the attribute value being validated. Defaults to null,
	 * meaning using the name of the attribute being validated.
	 */
	public $attributeName;

	/**
	 * Validates the attribute of the object.
	 * If there is any error, the error message is added to the object.
Qiang Xue committed
43
	 * @param \yii\db\ActiveRecord $object the object being validated
w  
Qiang Xue committed
44
	 * @param string $attribute the attribute being validated
Qiang Xue committed
45
	 * @throws InvalidConfigException if table doesn't have column specified
w  
Qiang Xue committed
46
	 */
w  
Qiang Xue committed
47
	public function validateAttribute($object, $attribute)
w  
Qiang Xue committed
48 49
	{
		$value = $object->$attribute;
Alexander Makarov committed
50
		if ($this->allowEmpty && $this->isEmpty($value)) {
w  
Qiang Xue committed
51
			return;
Alexander Makarov committed
52 53
		}

Qiang Xue committed
54
		/** @var $className \yii\db\ActiveRecord */
Qiang Xue committed
55 56
		$className = $this->className === null ? get_class($object) : \Yii::import($this->className);
		$attributeName = $this->attributeName === null ? $attribute : $this->attributeName;
w  
Qiang Xue committed
57

Qiang Xue committed
58
		$table = $className::getTableSchema();
Alexander Makarov committed
59
		if (($column = $table->getColumn($attributeName)) === null) {
Qiang Xue committed
60
			throw new InvalidConfigException('Table "' . $table->name . '" does not have a column named "' . $attributeName . '"');
Alexander Makarov committed
61
		}
w  
Qiang Xue committed
62

Qiang Xue committed
63
		$query = $className::find();
Qiang Xue committed
64
		$query->where(array($column->name => $value));
Alexander Makarov committed
65 66

		if ($object->getIsNewRecord()) {
Qiang Xue committed
67
			// if current $object isn't in the database yet then it's OK just to call exists()
Qiang Xue committed
68
			$exists = $query->exists();
Alexander Makarov committed
69 70
		} else {
			// if current $object is in the database already we can't use exists()
Qiang Xue committed
71 72
			$query->limit(2);
			$objects = $query->all();
w  
Qiang Xue committed
73 74

			$n = count($objects);
Alexander Makarov committed
75 76 77
			if ($n === 1) {
				if ($column->isPrimaryKey) {
					// primary key is modified and not unique
w  
Qiang Xue committed
78
					$exists = $object->getOldPrimaryKey() != $object->getPrimaryKey();
Alexander Makarov committed
79
				} else {
w  
Qiang Xue committed
80 81 82
					// non-primary key, need to exclude the current record based on PK
					$exists = array_shift($objects)->getPrimaryKey() != $object->getOldPrimaryKey();
				}
Alexander Makarov committed
83
			} else {
w  
Qiang Xue committed
84
				$exists = $n > 1;
Alexander Makarov committed
85
			}
w  
Qiang Xue committed
86 87
		}

Alexander Makarov committed
88
		if ($exists) {
89
			$message = $this->message !== null ? $this->message : \Yii::t('yii|{attribute} "{value}" has already been taken.');
Qiang Xue committed
90
			$this->addError($object, $attribute, $message);
w  
Qiang Xue committed
91 92
		}
	}
Alexander Makarov committed
93
}