mirror of
				https://github.com/yiisoft/yii2.git
				synced 2025-11-04 06:37:55 +08:00 
			
		
		
		
	refactored BaseActiveRecord::isPrimaryKey()
This commit is contained in:
		@ -1234,11 +1234,10 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
 | 
			
		||||
	public static function isPrimaryKey($keys)
 | 
			
		||||
	{
 | 
			
		||||
		$pks = static::primaryKey();
 | 
			
		||||
		foreach ($keys as $key) {
 | 
			
		||||
			if (!in_array($key, $pks, true)) {
 | 
			
		||||
		if (count($keys) === count($pks)) {
 | 
			
		||||
			return count(array_intersect($keys, $pks)) === count($pks);
 | 
			
		||||
		} else {
 | 
			
		||||
			return false;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
		return count($keys) === count($pks);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -55,7 +55,7 @@ class UniqueValidator extends Validator
 | 
			
		||||
			return;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		/** @var \yii\db\ActiveRecordInterface $className */
 | 
			
		||||
		/** @var ActiveRecordInterface $className */
 | 
			
		||||
		$className = $this->className === null ? get_class($object) : $this->className;
 | 
			
		||||
		$attributeName = $this->attributeName === null ? $attribute : $this->attributeName;
 | 
			
		||||
 | 
			
		||||
@ -67,6 +67,7 @@ class UniqueValidator extends Validator
 | 
			
		||||
			$exists = $query->exists();
 | 
			
		||||
		} else {
 | 
			
		||||
			// if current $object is in the database already we can't use exists()
 | 
			
		||||
			/** @var ActiveRecordInterface[] $objects */
 | 
			
		||||
			$objects = $query->limit(2)->all();
 | 
			
		||||
			$n = count($objects);
 | 
			
		||||
			if ($n === 1) {
 | 
			
		||||
@ -75,7 +76,7 @@ class UniqueValidator extends Validator
 | 
			
		||||
					$exists = $object->getOldPrimaryKey() != $object->getPrimaryKey();
 | 
			
		||||
				} else {
 | 
			
		||||
					// non-primary key, need to exclude the current record based on PK
 | 
			
		||||
					$exists = array_shift($objects)->getPrimaryKey() != $object->getOldPrimaryKey();
 | 
			
		||||
					$exists = $objects[0]->getPrimaryKey() != $object->getOldPrimaryKey();
 | 
			
		||||
				}
 | 
			
		||||
			} else {
 | 
			
		||||
				$exists = $n > 1;
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user