From d5f40b42cfd8467bdcfbf127fee66c6f451aea99 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Sun, 22 Dec 2013 10:02:00 -0500 Subject: [PATCH] Added ActiveRecordInterface::getOldPrimaryKey(). --- framework/yii/db/ActiveRecordInterface.php | 19 ++++++++++++++++++- framework/yii/validators/ExistValidator.php | 4 ++-- framework/yii/validators/UniqueValidator.php | 8 ++------ 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/framework/yii/db/ActiveRecordInterface.php b/framework/yii/db/ActiveRecordInterface.php index 556384b..73db852 100644 --- a/framework/yii/db/ActiveRecordInterface.php +++ b/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); -} \ No newline at end of file +} diff --git a/framework/yii/validators/ExistValidator.php b/framework/yii/validators/ExistValidator.php index 585b82f..04d8af8 100644 --- a/framework/yii/validators/ExistValidator.php +++ b/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]); diff --git a/framework/yii/validators/UniqueValidator.php b/framework/yii/validators/UniqueValidator.php index d9cd587..b04f4a9 100644 --- a/framework/yii/validators/UniqueValidator.php +++ b/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())) {