From 56f102c893833cef221e325c4867c9d4630c3994 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sun, 24 Apr 2016 23:02:29 +0800 Subject: [PATCH] =?UTF-8?q?model=E7=B1=BB=E5=A2=9E=E5=8A=A0db=E5=B1=9E?= =?UTF-8?q?=E6=80=A7=20=E5=8F=AF=E4=BB=A5=E7=94=A8=E4=BA=8E=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B=E5=86=85=E9=83=A8=E8=BF=9B=E8=A1=8C=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=E8=B0=83=E7=94=A8=20$this->db?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 3e96afff..c668fe87 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -28,6 +28,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess protected static $table; // 回调事件 protected static $event = []; + // 数据库操作对象 + protected $db; // 数据表主键 复合主键使用数组定义 protected $pk = 'id'; @@ -87,6 +89,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $this->data = $data; } $this->name = basename(str_replace('\\', '/', get_class($this))); + $this->db = self::db(); $this->initialize(); $this->relation = new Relation($this); @@ -675,6 +678,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess if (is_string($relations)) { $relations = explode(',', $relations); } + foreach ($relations as $relation) { $this->data[$relation] = $this->relation->getRelation($relation); } @@ -714,7 +718,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess public function eagerlyResult($result, $relation) { return $this->relation->eagerlyResult($result, $relation); - } /** @@ -785,7 +788,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $table = $table ?: Db::name(Loader::parseName($this->name) . '_' . $name)->getTable(); $localKey = $localKey ?: $name . '_id'; $foreignKey = $foreignKey ?: Loader::parseName($this->name) . '_id'; - return $this->relation->belongsToMany($model, $table, $localKey, $foreignKey); } @@ -797,10 +799,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess public static function db() { $model = get_called_class(); - if (!isset(self::$links[$model])) { self::$links[$model] = Db::connect(static::$connection); } + // 设置当前数据表和模型名 self::$links[$model]->setTable(static::$table); $name = basename(str_replace('\\', '/', $model)); self::$links[$model]->name($name);