UniqueValidator.php 2.96 KB
Newer Older
w  
Qiang Xue committed
1 2 3
<?php
/**
 * @link http://www.yiiframework.com/
Qiang Xue committed
4
 * @copyright Copyright (c) 2008 Yii Software LLC
w  
Qiang Xue committed
5 6 7
 * @license http://www.yiiframework.com/license/
 */

w  
Qiang Xue committed
8
namespace yii\validators;
Qiang Xue committed
9 10

use Yii;
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
{
	/**
Qiang Xue committed
22
	 * @var string the ActiveRecord class name or alias of the class
Alexander Makarov committed
23
	 * that should be used to look for the attribute value being validated.
Qiang Xue committed
24
	 * Defaults to null, meaning using the ActiveRecord class of the attribute being validated.
w  
Qiang Xue committed
25 26 27 28 29 30 31 32 33 34
	 * @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;

Qiang Xue committed
35 36 37 38 39 40 41
	/**
	 * Initializes the validator.
	 */
	public function init()
	{
		parent::init();
		if ($this->message === null) {
42
			$this->message = Yii::t('yii', '{attribute} "{value}" has already been taken.');
Qiang Xue committed
43 44 45
		}
	}

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

		if (is_array($value)) {
58
			$this->addError($object, $attribute, Yii::t('yii', '{attribute} is invalid.'));
59 60 61
			return;
		}

Qiang Xue committed
62
		/** @var $className \yii\db\ActiveRecord */
Qiang Xue committed
63
		$className = $this->className === null ? get_class($object) : Yii::import($this->className);
Qiang Xue committed
64
		$attributeName = $this->attributeName === null ? $attribute : $this->attributeName;
w  
Qiang Xue committed
65

Qiang Xue committed
66
		$table = $className::getTableSchema();
Alexander Makarov committed
67
		if (($column = $table->getColumn($attributeName)) === null) {
Qiang Xue committed
68
			throw new InvalidConfigException("Table '{$table->name}' does not have a column named '$attributeName'.");
Alexander Makarov committed
69
		}
w  
Qiang Xue committed
70

Qiang Xue committed
71
		$query = $className::find();
Qiang Xue committed
72
		$query->where(array($column->name => $value));
Alexander Makarov committed
73 74

		if ($object->getIsNewRecord()) {
Qiang Xue committed
75
			// if current $object isn't in the database yet then it's OK just to call exists()
Qiang Xue committed
76
			$exists = $query->exists();
Alexander Makarov committed
77 78
		} else {
			// if current $object is in the database already we can't use exists()
Qiang Xue committed
79 80
			$query->limit(2);
			$objects = $query->all();
w  
Qiang Xue committed
81 82

			$n = count($objects);
Alexander Makarov committed
83 84 85
			if ($n === 1) {
				if ($column->isPrimaryKey) {
					// primary key is modified and not unique
w  
Qiang Xue committed
86
					$exists = $object->getOldPrimaryKey() != $object->getPrimaryKey();
Alexander Makarov committed
87
				} else {
w  
Qiang Xue committed
88 89 90
					// non-primary key, need to exclude the current record based on PK
					$exists = array_shift($objects)->getPrimaryKey() != $object->getOldPrimaryKey();
				}
Alexander Makarov committed
91
			} else {
w  
Qiang Xue committed
92
				$exists = $n > 1;
Alexander Makarov committed
93
			}
w  
Qiang Xue committed
94 95
		}

Alexander Makarov committed
96
		if ($exists) {
Qiang Xue committed
97
			$this->addError($object, $attribute, $this->message);
w  
Qiang Xue committed
98 99
		}
	}
Zander Baldwin committed
100
}