Skip to content

Commit 0c951d1

Browse files
committed
实体模型增加table、suffix等参数设置
1 parent 31f534c commit 0c951d1

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

src/Entity.php

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ public function __construct(array | object $data = [], ?Model $model = null)
7474
'schema' => $options['schema'] ?? [],
7575
'update_time' => $options['update_time'] ?? 'update_time',
7676
'create_time' => $options['create_time'] ?? 'create_time',
77+
'connection' => $options['connection'] ?? null,
78+
'name' => $options['name'] ?? null,
79+
'table' => $options['table'] ?? null,
80+
'suffix' => $options['suffix'] ?? '',
7781
'pk' => $options['pk'] ?? 'id',
7882
'validate' => $options['validate'] ?? $this->parseValidate(),
7983
'type' => $options['type'] ?? [],
@@ -178,16 +182,6 @@ private function getWeakData($key, $name, $default = null)
178182
return self::$weakMap[$this][$key][$name] ?? $default;
179183
}
180184

181-
/**
182-
* 获取对应表名(仅限简单模型).
183-
*
184-
* @return string
185-
*/
186-
protected function getTableName(): string
187-
{
188-
return $this->getOption('table_name', Str::snake(class_basename(static::class)));
189-
}
190-
191185
/**
192186
* 获取主键名.
193187
*
@@ -202,13 +196,13 @@ public function getPk()
202196
}
203197

204198
/**
205-
* 获取模型名.
199+
* 获取表名(不含前后缀).
206200
*
207201
* @return string
208202
*/
209203
public function getName(): string
210204
{
211-
return Str::snake($this->getTableName());
205+
return $this->getOption('name', Str::snake(class_basename(static::class)));
212206
}
213207

214208
/**
@@ -248,10 +242,12 @@ private function initModel(?Model $model, string $class)
248242
*/
249243
private function getSimpleModel()
250244
{
251-
return Db::connect($this->getOption('connect'))
245+
$db = Db::connect($this->getOption('connection'))
252246
->newQuery()
253-
->name($this->getTableName())
254247
->pk($this->getOption('pk'));
248+
249+
return $this->getOption('table') ? $db->table($this->getOption('table'))
250+
: $db->name($this->getName())->suffix($this->getOption('suffix'));
255251
}
256252

257253
/**
@@ -1738,7 +1734,7 @@ public function db(): Query
17381734
$db = $db instanceof Query ? $db : $db->db();
17391735

17401736
// 执行扩展查询
1741-
$this->query($db->suffix($this->getOption('suffix', '')));
1737+
$this->query($db->suffix($this->getOption('suffix')));
17421738
return $db;
17431739
}
17441740

0 commit comments

Comments
 (0)