From 6915f0249e8fa195b73e0189fbcad4ece0dcea42 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 27 Apr 2016 12:42:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E6=A8=A1=E5=9E=8B=E7=B1=BB?= =?UTF-8?q?=E5=AF=B9=E4=B8=BB=E9=94=AE=E7=9A=84=E8=87=AA=E5=8A=A8=E8=AF=BB?= =?UTF-8?q?=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 13 +++++++------ library/think/model/Merge.php | 4 ++-- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 8ab7d1cb..53ad11f6 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -30,7 +30,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess protected static $event = []; // 数据表主键 复合主键使用数组定义 - protected $pk = 'id'; + protected $pk; // 错误信息 protected $error; // 当前模型名称 @@ -177,7 +177,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess */ public function getPk($table = '') { - if (!$this->pk) { + if (empty($this->pk)) { $this->pk = self::db()->getTableInfo($table, 'pk'); } return $this->pk; @@ -272,8 +272,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess // 获取自动增长主键 if ($result && $getInsertId) { $insertId = self::db()->getLastInsID(); - if (is_string($this->pk) && $insertId) { - $this->data[$this->pk] = $insertId; + $pk = $this->getPk(); + if (is_string($pk) && $insertId) { + $this->data[$pk] = $insertId; } } // 新增回调 @@ -731,7 +732,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess { // 记录当前关联信息 $model = $this->parseModel($model); - $localKey = $localKey ?: $this->pk; + $localKey = $localKey ?: $this->getPk(); $foreignKey = $foreignKey ?: Loader::parseName($this->name) . '_id'; return $this->relation->hasOne($model, $foreignKey, $localKey); } @@ -765,7 +766,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess { // 记录当前关联信息 $model = $this->parseModel($model); - $localKey = $localKey ?: $this->pk; + $localKey = $localKey ?: $this->getPk(); $foreignKey = $foreignKey ?: Loader::parseName($this->name) . '_id'; return $this->relation->hasMany($model, $foreignKey, $localKey); } diff --git a/library/think/model/Merge.php b/library/think/model/Merge.php index 4279f152..541334d4 100644 --- a/library/think/model/Merge.php +++ b/library/think/model/Merge.php @@ -184,7 +184,7 @@ class Merge extends Model $table = is_int($key) ? self::db()->name($model)->getTable() : $model; // 处理关联模型数据 $data = $this->parseData($name, $this->data); - self::db()->table($table)->strict(false)->where($this->fk, $this->data[$this->pk])->update($data); + self::db()->table($table)->strict(false)->where($this->fk, $this->data[$this->getPk()])->update($data); } // 新增回调 $this->trigger('after_update', $this); @@ -238,7 +238,7 @@ class Merge extends Model $result = self::db()->delete($this->data); if ($result) { // 获取主键数据 - $pk = $this->data[$this->pk]; + $pk = $this->data[$this->getPk()]; // 删除关联数据 foreach (static::$relationModel as $key => $model) {