model类增加db属性 可以用于模型内部进行查询操作调用 $this->db

This commit is contained in:
thinkphp
2016-04-24 23:02:29 +08:00
parent ec61c9717a
commit 56f102c893

View File

@@ -28,6 +28,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
protected static $table; protected static $table;
// 回调事件 // 回调事件
protected static $event = []; protected static $event = [];
// 数据库操作对象
protected $db;
// 数据表主键 复合主键使用数组定义 // 数据表主键 复合主键使用数组定义
protected $pk = 'id'; protected $pk = 'id';
@@ -87,6 +89,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$this->data = $data; $this->data = $data;
} }
$this->name = basename(str_replace('\\', '/', get_class($this))); $this->name = basename(str_replace('\\', '/', get_class($this)));
$this->db = self::db();
$this->initialize(); $this->initialize();
$this->relation = new Relation($this); $this->relation = new Relation($this);
@@ -675,6 +678,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
if (is_string($relations)) { if (is_string($relations)) {
$relations = explode(',', $relations); $relations = explode(',', $relations);
} }
foreach ($relations as $relation) { foreach ($relations as $relation) {
$this->data[$relation] = $this->relation->getRelation($relation); $this->data[$relation] = $this->relation->getRelation($relation);
} }
@@ -714,7 +718,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
public function eagerlyResult($result, $relation) public function eagerlyResult($result, $relation)
{ {
return $this->relation->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(); $table = $table ?: Db::name(Loader::parseName($this->name) . '_' . $name)->getTable();
$localKey = $localKey ?: $name . '_id'; $localKey = $localKey ?: $name . '_id';
$foreignKey = $foreignKey ?: Loader::parseName($this->name) . '_id'; $foreignKey = $foreignKey ?: Loader::parseName($this->name) . '_id';
return $this->relation->belongsToMany($model, $table, $localKey, $foreignKey); return $this->relation->belongsToMany($model, $table, $localKey, $foreignKey);
} }
@@ -797,10 +799,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
public static function db() public static function db()
{ {
$model = get_called_class(); $model = get_called_class();
if (!isset(self::$links[$model])) { if (!isset(self::$links[$model])) {
self::$links[$model] = Db::connect(static::$connection); self::$links[$model] = Db::connect(static::$connection);
} }
// 设置当前数据表和模型名
self::$links[$model]->setTable(static::$table); self::$links[$model]->setTable(static::$table);
$name = basename(str_replace('\\', '/', $model)); $name = basename(str_replace('\\', '/', $model));
self::$links[$model]->name($name); self::$links[$model]->name($name);