Skip to content

Commit 806b4e8

Browse files
author
Ethan
committed
ModelTrait
1 parent 04ebc86 commit 806b4e8

File tree

2 files changed

+33
-16
lines changed

2 files changed

+33
-16
lines changed

src/Builder.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818
*/
1919
class Builder
2020
{
21-
/**
22-
* @var Connection 数据库连接对象
23-
*/
21+
/** @var static */
22+
protected static $instance;
23+
24+
/** @var Connection 数据库连接对象 */
2425
private $db;
2526

2627
protected $table;
@@ -41,7 +42,6 @@ class Builder
4142
protected $groupBy;
4243
protected $having;
4344

44-
4545
/**
4646
* 自动生成查询绑定参数前缀
4747
*/
@@ -57,6 +57,23 @@ public function __construct(array $config = array())
5757
}
5858
}
5959

60+
/**
61+
* @return static
62+
*/
63+
public function setAsGlobal()
64+
{
65+
static::$instance = $this;
66+
return $this;
67+
}
68+
69+
/**
70+
* @return static
71+
*/
72+
public static function getInstance()
73+
{
74+
return static::$instance;
75+
}
76+
6077
/**
6178
* 设置数据库连接
6279
*

src/ModelTrait.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use PFinal\Database\Relations\BelongsTo;
66
use PFinal\Database\Relations\HasMany;
77
use PFinal\Database\Relations\HasOne;
8-
use Leaf\DB;
8+
use PFinal\Database\Builder as DB;
99
use PFinal\Database\Relations\RelationBase;
1010

1111
/**
@@ -47,22 +47,22 @@ trait ModelTrait
4747
*/
4848
public static function tableName()
4949
{
50-
return '{{%' . self::getTableName(get_called_class()) . '}}';
50+
return '{{%' . self::convertTableName(get_called_class()) . '}}';
5151
}
5252

5353
public static function __callStatic($name, $arguments)
5454
{
5555
return call_user_func_array(
56-
[DB::table(static::tableName(), self::getTableName(get_called_class()))->asEntity(static::className()), $name],
56+
[DB::getInstance()->table(static::tableName(), self::convertTableName(get_called_class()))->asEntity(get_called_class()), $name],
5757
$arguments);
5858
}
5959

6060
public function loadDefaultValues()
6161
{
62-
return DB::table(static::tableName())->loadDefaultValues($this);
62+
return DB::getInstance()->table(static::tableName())->loadDefaultValues($this);
6363
}
6464

65-
private static function getTableName($className)
65+
private static function convertTableName($className)
6666
{
6767
//去掉namespace
6868
$name = rtrim(str_replace('\\', '/', $className), '/\\');
@@ -87,11 +87,11 @@ private static function getTableName($className)
8787
public function hasOne($related, $foreignKey = null, $localKey = 'id')
8888
{
8989
if ($foreignKey === null) {
90-
$foreignKey = self::getTableName(get_called_class()) . '_id';
90+
$foreignKey = self::convertTableName(get_called_class()) . '_id';
9191
}
9292

9393
$hasOne = new HasOne();
94-
$hasOne->setConnection(DB::getConnection());
94+
$hasOne->setConnection(DB::getInstance()->getConnection());
9595

9696
$obj = $hasOne->table($related::tableName())->asEntity($related);
9797

@@ -113,11 +113,11 @@ public function hasOne($related, $foreignKey = null, $localKey = 'id')
113113
public function belongsTo($related, $foreignKey = null, $ownerKey = 'id')
114114
{
115115
if ($foreignKey === null) {
116-
$foreignKey = self::getTableName($related) . '_id';
116+
$foreignKey = self::convertTableName($related) . '_id';
117117
}
118118

119119
$hasOne = new BelongsTo();
120-
$hasOne->setConnection(DB::getConnection());
120+
$hasOne->setConnection(DB::getInstance()->getConnection());
121121

122122
$obj = $hasOne->table($related::tableName())->asEntity($related);
123123

@@ -139,11 +139,11 @@ public function belongsTo($related, $foreignKey = null, $ownerKey = 'id')
139139
public function hasMany($related, $foreignKey = null, $localKey = 'id')
140140
{
141141
if ($foreignKey === null) {
142-
$foreignKey = self::getTableName(get_called_class()) . '_id';
142+
$foreignKey = self::convertTableName(get_called_class()) . '_id';
143143
}
144144

145145
$hasOne = new HasMany();
146-
$hasOne->setConnection(DB::getConnection());
146+
$hasOne->setConnection(DB::getInstance()->getConnection());
147147

148148
$obj = $hasOne->table($related::tableName())->asEntity($related);
149149

@@ -167,7 +167,7 @@ public static function with($relations)
167167
{
168168
$relations = is_string($relations) ? func_get_args() : $relations;
169169

170-
return DB::table(static::tableName(), self::getTableName(get_called_class()))->asEntity(static::className())->afterFind(function ($models) use ($relations) {
170+
return DB::getInstance()->table(static::tableName(), self::convertTableName(get_called_class()))->asEntity(get_called_class())->afterFind(function ($models) use ($relations) {
171171
RelationBase::appendRelationData($models, $relations);
172172
});
173173
}

0 commit comments

Comments
 (0)