From 6c396454da6fdf668ad1ab4018d3b2d5d3bb1dc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Wed, 29 Sep 2021 10:44:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kiri-gii/Gii.php | 28 ++++++++++++---------------- kiri-gii/GiiModel.php | 19 ++++++++++--------- 2 files changed, 22 insertions(+), 25 deletions(-) diff --git a/kiri-gii/Gii.php b/kiri-gii/Gii.php index be438a31..8ff97b4b 100644 --- a/kiri-gii/Gii.php +++ b/kiri-gii/Gii.php @@ -92,7 +92,7 @@ class Gii $task->setInput($this->input); return $task->generate(); default: - return $this->getModel($make, $input); + return $this->getModel($make, $db, $input); } } @@ -103,13 +103,9 @@ class Gii * @return array * @throws Exception */ - private function getModel($make, $input): array + private function getModel($make, $db, $input): array { - if ($this->db instanceof Connection) { - return $this->makeByDatabases($make, $input); - } - - return $this->makeByDatabases($make, $input); + return $this->makeByDatabases($make, $db, $input); } @@ -119,7 +115,7 @@ class Gii * @return array * @throws Exception */ - private function makeByDatabases($make, InputInterface $input): array + private function makeByDatabases($make, $db, InputInterface $input): array { $redis = Kiri::getDi()->get(Redis::class); if ($input->hasArgument('name')) { @@ -127,8 +123,8 @@ class Gii $redis->del('column:' . $this->tableName); } return match ($make) { - 'controller' => $this->getTable(1, 0), - 'model' => $this->getTable(0, 1), + 'controller' => $this->getTable($db,1, 0), + 'model' => $this->getTable($db,0, 1), default => [], }; } @@ -141,9 +137,9 @@ class Gii * * @throws Exception */ - private function getTable($controller, $model): array + private function getTable($db, $controller, $model): array { - $tables = $this->getFields($this->getTables()); + $tables = $this->getFields($this->getTables($db)); if (empty($tables)) { return []; } @@ -201,10 +197,10 @@ class Gii * @return array|string|null * @throws Exception */ - private function getTables(): array|string|null + private function getTables($db): array|string|null { if (empty($this->tableName)) { - return $this->showAll(); + return $this->showAll($db); } $res = $this->tableName; if (is_string($res)) { @@ -220,10 +216,10 @@ class Gii * @return array * @throws Exception */ - private function showAll(): array + private function showAll($db): array { $res = []; - $_tables = Db::findAllBySql('show tables', [], $this->db); + $_tables = Db::findAllBySql('show tables', [], $db); if (empty($_tables)) { return $res; } diff --git a/kiri-gii/GiiModel.php b/kiri-gii/GiiModel.php index d31c2a94..621637ea 100644 --- a/kiri-gii/GiiModel.php +++ b/kiri-gii/GiiModel.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Gii; use Database\Db; +use Database\Model; use Exception; use ReflectionException; use Kiri\Kiri; @@ -68,10 +69,10 @@ namespace ' . $namespace . '; $html .= $imports . PHP_EOL; } - if (!str_contains($imports, 'Annotation\Model\Set')) { + if (!str_contains($imports, 'Database\Annotation\Set')) { $html .= 'use Annotation\Model\Set;' . PHP_EOL; } - if (!str_contains($imports, 'Annotation\Model\Get')) { + if (!str_contains($imports, 'Database\Annotation\Get')) { $html .= 'use Annotation\Model\Get;' . PHP_EOL; } } catch (\Throwable $e) { @@ -92,7 +93,7 @@ use Database\Connection; use Annotation\Model\Get; use Annotation\Model\Set; use Annotation\Model\Relation; -use Database\ActiveRecord; +use Database\Model; ' . PHP_EOL; } @@ -111,7 +112,7 @@ use Database\ActiveRecord; *' . implode('', $this->visible) . ' * @sql */ -#[Target] class ' . $managerName . ' extends ActiveRecord +#[Target] class ' . $managerName . ' extends Model { '; @@ -220,8 +221,6 @@ use Database\ActiveRecord; if (!empty($prefixed)) { if (str_starts_with($field, $prefixed)) { $field = str_replace($prefixed, '', $field); - - $field = '{{%' . $field . '}}'; } } @@ -384,7 +383,10 @@ use Database\ActiveRecord; return null; } return ' - public ?string $primary = \'' . $field . '\';'; + /** + * @var string|null + */ + protected ?string $primary = \'' . $field . '\';'; } /** @@ -394,8 +396,7 @@ use Database\ActiveRecord; { return ' /** - * @return Connection - * @throws Exception + * @var string */ protected string $connection = \'' . $this->db->id . '\'; ';