Skip to content

Commit 1c8b9cd

Browse files
author
Ethan
committed
table name
1 parent b24af22 commit 1c8b9cd

File tree

2 files changed

+59
-21
lines changed

2 files changed

+59
-21
lines changed

src/Builder.php

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class Builder
2424
private $db;
2525

2626
protected $table;
27+
protected $tableAs;
2728
protected $field;
2829
protected $orderBy;
2930
protected $limit;
@@ -88,14 +89,23 @@ public function getConnection()
8889
* @param string $tableName 支持 as 例如 user as u
8990
* @return static
9091
*/
91-
public function table($tableName = '')
92+
public function table($tableName = '', $asName = null)
9293
{
93-
if (!empty($tableName)) {
94-
$tableName = self::addPrefix($tableName);
94+
if ($asName === null) {
95+
// 兼容as部份写在tableName中
96+
// user as u
97+
// {{user}} as u
98+
// {{%user}} as u
99+
// user u
100+
if (preg_match('/^(.+?)\s+(as\s+)?(\w+)$/i', $tableName, $res)) {
101+
$tableName = $res[1];
102+
$asName = $res[3];
103+
}
95104
}
96105

97106
$builder = clone $this;
98-
$builder->table = $tableName;
107+
$builder->table = self::addPrefix($tableName);
108+
$builder->tableAs = $this->addTableQuote($asName);
99109
return $builder;
100110
}
101111

@@ -108,24 +118,31 @@ public function table($tableName = '')
108118
*/
109119
public function addPrefix($tableName)
110120
{
111-
// user as u
112-
// {{user}} as u
113-
// {{%user}} as u
114-
// user u
115-
if (preg_match('/^(.+?)\s+(as\s+)?(\w+)$/i', $tableName, $res)) {
116-
$tableName = $res[1];
117-
$asName = ' AS ' . $res[3];
118-
} else {
119-
$asName = '';
121+
if (empty($tableName)) {
122+
return $tableName;
123+
}
124+
if (strpos($tableName, '{{') === false) {
125+
return $this->addTableQuote('%' . $tableName);
126+
}
127+
128+
return $tableName;
129+
}
130+
131+
private function addTableQuote($tableName)
132+
{
133+
if (empty($tableName)) {
134+
return $tableName;
120135
}
121136

122137
if (strpos($tableName, '{{') === false) {
123-
$tableName = '{{%' . $tableName . '}}';
138+
return '{{' . $tableName . '}}';
124139
}
140+
125141
if (!preg_match('/^\{\{%?[\w\-\.\$]+%?\}\}$/', $tableName)) {
126-
throw new Exception('表名含有不被允许的字符');
142+
throw new Exception('表名含有不被允许的字符: ' . $tableName);
127143
}
128-
return $tableName . $asName;
144+
145+
return $tableName;
129146
}
130147

131148
/**
@@ -206,7 +223,7 @@ public function findAll($condition = '', $params = array())
206223
{
207224
$this->where($condition, $params);
208225

209-
$sql = 'SELECT ' . static::getFieldString() . ' FROM ' . $this->table
226+
$sql = 'SELECT ' . static::getFieldString() . ' FROM ' . $this->table . $this->getTableAs()
210227
. $this->getJoinString()
211228
. $this->getWhereString()
212229
. $this->getGroupByString()
@@ -218,6 +235,15 @@ public function findAll($condition = '', $params = array())
218235
return static::findAllBySql($sql, $this->params);
219236
}
220237

238+
private function getTableAs()
239+
{
240+
if (empty($this->tableAs)) {
241+
return '';
242+
}
243+
244+
return ' AS ' . $this->tableAs;
245+
}
246+
221247
/**
222248
* 拆分查询,用于处理非常多的查询结果,而不会消耗大量内存,建议加上排序字段
223249
*
@@ -564,7 +590,7 @@ public function __call($method, $arguments)
564590

565591
$method = strtoupper($method);
566592

567-
$sql = 'SELECT ' . $method . '(' . $field . ') FROM ' . $this->table
593+
$sql = 'SELECT ' . $method . '(' . $field . ') FROM ' . $this->table . $this->getTableAs()
568594
. $this->getJoinString()
569595
. $this->getWhereString();
570596

@@ -894,7 +920,7 @@ public function transaction(Closure $callback)
894920
*/
895921
public function toSql()
896922
{
897-
$sql = 'SELECT ' . static::getFieldString() . ' FROM ' . $this->table
923+
$sql = 'SELECT ' . static::getFieldString() . ' FROM ' . $this->table . $this->getTableAs()
898924
. $this->getJoinString()
899925
. $this->getWhereString()
900926
. $this->getGroupByString()
@@ -1211,6 +1237,7 @@ public function afterFind($callback)
12111237
protected function reset()
12121238
{
12131239
$this->table = null;
1240+
$this->tableAs = null;
12141241
$this->fetchClass = null;
12151242
$this->orderBy = null;
12161243
$this->field = null;

src/ModelTrait.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,17 @@
2323
* @method static DataProvider paginate($pageSize = null)
2424
* @method static int count($field = '*')
2525
* @method static Builder useWritePdo()
26+
* @method static Builder limit($limit)
27+
* @method static Builder offset($offset)
2628
* @method static Builder orderBy($columns)
29+
* @method static Builder having($having, array $params = array())
30+
* @method static Builder field($field)
31+
* @method static Builder lockForUpdate()
32+
* @method static Builder lockInShareMode()
33+
* @method static Builder join($table, $on)
34+
* @method static Builder leftJoin($table, $on)
2735
* @method static boolean chunk($num, $callback)
36+
* @method static boolean chunkById($num, callable $callback, $column = 'id')
2837
*
2938
* @method static bool insert(array $data);
3039
* @method static int insertGetId(array $data);
@@ -43,7 +52,9 @@ public static function tableName()
4352

4453
public static function __callStatic($name, $arguments)
4554
{
46-
return call_user_func_array([DB::table(static::tableName())->asEntity(static::className()), $name], $arguments);
55+
return call_user_func_array(
56+
[DB::table(static::tableName(), self::getTableName(get_called_class()))->asEntity(static::className()), $name],
57+
$arguments);
4758
}
4859

4960
public function loadDefaultValues()
@@ -156,7 +167,7 @@ public static function with($relations)
156167
{
157168
$relations = is_string($relations) ? func_get_args() : $relations;
158169

159-
return DB::table(static::tableName())->asEntity(static::className())->afterFind(function ($models) use ($relations) {
170+
return DB::table(static::tableName(), self::getTableName(get_called_class()))->asEntity(static::className())->afterFind(function ($models) use ($relations) {
160171
RelationBase::appendRelationData($models, $relations);
161172
});
162173
}

0 commit comments

Comments
 (0)