Browse Source

Added ActiveRecordInterface::getOldPrimaryKey().

tags/2.0.0-beta
Qiang Xue 11 years ago
parent
commit
d5f40b42cf
  1. 19
      framework/yii/db/ActiveRecordInterface.php
  2. 4
      framework/yii/validators/ExistValidator.php
  3. 8
      framework/yii/validators/UniqueValidator.php

19
framework/yii/db/ActiveRecordInterface.php

@ -70,6 +70,23 @@ interface ActiveRecordInterface
public function getPrimaryKey($asArray = false);
/**
* Returns the old primary key value(s).
* This refers to the primary key value that is populated into the record
* after executing a find method (e.g. find(), findAll()).
* The value remains unchanged even if the primary key attribute is manually assigned with a different value.
* @param boolean $asArray whether to return the primary key value as an array. If true,
* the return value will be an array with column name as key and column value as value.
* If this is false (default), a scalar value will be returned for non-composite primary key.
* @property mixed The old primary key value. An array (column name => column value) is
* returned if the primary key is composite. A string is returned otherwise (null will be
* returned if the key value is null).
* @return mixed the old primary key value. An array (column name => column value) is returned if the primary key
* is composite or `$asArray` is true. A string is returned otherwise (null will be returned if
* the key value is null).
*/
public function getOldPrimaryKey($asArray = false);
/**
* Creates an [[ActiveQueryInterface|ActiveQuery]] instance for query purpose.
*
* This method is usually ment to be used like this:
@ -290,4 +307,4 @@ interface ActiveRecordInterface
* If true, the model containing the foreign key will be deleted.
*/
public function unlink($name, $model, $delete = false);
}
}

4
framework/yii/validators/ExistValidator.php

@ -61,7 +61,7 @@ class ExistValidator extends Validator
return;
}
/** @var \yii\db\ActiveRecord $className */
/** @var \yii\db\ActiveRecordInterface $className */
$className = $this->className === null ? get_class($object) : $this->className;
$attributeName = $this->attributeName === null ? $attribute : $this->attributeName;
$query = $className::find();
@ -85,7 +85,7 @@ class ExistValidator extends Validator
if ($this->attributeName === null) {
throw new InvalidConfigException('The "attributeName" property must be set.');
}
/** @var \yii\db\ActiveRecord $className */
/** @var \yii\db\ActiveRecordInterface $className */
$className = $this->className;
$query = $className::find();
$query->where([$this->attributeName => $value]);

8
framework/yii/validators/UniqueValidator.php

@ -8,8 +8,6 @@
namespace yii\validators;
use Yii;
use yii\base\InvalidConfigException;
use yii\db\ActiveRecord;
use yii\db\ActiveRecordInterface;
/**
@ -57,7 +55,7 @@ class UniqueValidator extends Validator
return;
}
/** @var \yii\db\ActiveRecord $className */
/** @var \yii\db\ActiveRecordInterface $className */
$className = $this->className === null ? get_class($object) : $this->className;
$attributeName = $this->attributeName === null ? $attribute : $this->attributeName;
@ -69,9 +67,7 @@ class UniqueValidator extends Validator
$exists = $query->exists();
} else {
// if current $object is in the database already we can't use exists()
$query->limit(2);
$objects = $query->all();
$objects = $query->limit(2)->all();
$n = count($objects);
if ($n === 1) {
if (in_array($attributeName, $className::primaryKey())) {

Loading…
Cancel
Save