Commit 7cf358af by Qiang Xue

Fixes #4607: AR model will throw an exception if it does not have a primary key…

Fixes #4607: AR model will throw an exception if it does not have a primary key to avoid updating/deleting data massively
parent 25c2f9bf
......@@ -178,6 +178,7 @@ Yii Framework 2 Change Log
- Enh #4581: Added ability to disable url encoding in `UrlRule` (tadaszelvys)
- Enh #4597: `yii\composer\Installer::setPermission()` supports setting permission for both directories and files now (qiangxue)
- Enh #4602: Added $key param in ActionColumn buttons Closure call (disem)
- Enh #4607: AR model will throw an exception if it does not have a primary key to avoid updating/deleting data massively (qiangxue)
- Enh #4630: Added automatic generating of unique slug value to `yii\behaviors\Sluggable` (klimov-paul)
- Enh #4644: Added `\yii\db\Schema::createColumnSchema()` to be able to customize column schema used (mcd-php)
- Enh #4656: HtmlPurifier helper config can now be a closure to change the purifier config object after it was created (Alex-Code)
......
......@@ -1008,10 +1008,14 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
* @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).
* @throws Exception if the AR model does not have a primary key
*/
public function getOldPrimaryKey($asArray = false)
{
$keys = $this->primaryKey();
if (empty($keys)) {
throw new Exception(get_class($this) . ' does not have a primary key. You should either define a primary key for the corresponding table or override the primaryKey() method.');
}
if (count($keys) === 1 && !$asArray) {
return isset($this->_oldAttributes[$keys[0]]) ? $this->_oldAttributes[$keys[0]] : null;
} else {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment