This commit is contained in:
2021-09-29 10:44:14 +08:00
parent 2cd1739658
commit 6c396454da
2 changed files with 22 additions and 25 deletions
+12 -16
View File
@@ -92,7 +92,7 @@ class Gii
$task->setInput($this->input); $task->setInput($this->input);
return $task->generate(); return $task->generate();
default: default:
return $this->getModel($make, $input); return $this->getModel($make, $db, $input);
} }
} }
@@ -103,13 +103,9 @@ class Gii
* @return array * @return array
* @throws Exception * @throws Exception
*/ */
private function getModel($make, $input): array private function getModel($make, $db, $input): array
{ {
if ($this->db instanceof Connection) { return $this->makeByDatabases($make, $db, $input);
return $this->makeByDatabases($make, $input);
}
return $this->makeByDatabases($make, $input);
} }
@@ -119,7 +115,7 @@ class Gii
* @return array * @return array
* @throws Exception * @throws Exception
*/ */
private function makeByDatabases($make, InputInterface $input): array private function makeByDatabases($make, $db, InputInterface $input): array
{ {
$redis = Kiri::getDi()->get(Redis::class); $redis = Kiri::getDi()->get(Redis::class);
if ($input->hasArgument('name')) { if ($input->hasArgument('name')) {
@@ -127,8 +123,8 @@ class Gii
$redis->del('column:' . $this->tableName); $redis->del('column:' . $this->tableName);
} }
return match ($make) { return match ($make) {
'controller' => $this->getTable(1, 0), 'controller' => $this->getTable($db,1, 0),
'model' => $this->getTable(0, 1), 'model' => $this->getTable($db,0, 1),
default => [], default => [],
}; };
} }
@@ -141,9 +137,9 @@ class Gii
* *
* @throws Exception * @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)) { if (empty($tables)) {
return []; return [];
} }
@@ -201,10 +197,10 @@ class Gii
* @return array|string|null * @return array|string|null
* @throws Exception * @throws Exception
*/ */
private function getTables(): array|string|null private function getTables($db): array|string|null
{ {
if (empty($this->tableName)) { if (empty($this->tableName)) {
return $this->showAll(); return $this->showAll($db);
} }
$res = $this->tableName; $res = $this->tableName;
if (is_string($res)) { if (is_string($res)) {
@@ -220,10 +216,10 @@ class Gii
* @return array * @return array
* @throws Exception * @throws Exception
*/ */
private function showAll(): array private function showAll($db): array
{ {
$res = []; $res = [];
$_tables = Db::findAllBySql('show tables', [], $this->db); $_tables = Db::findAllBySql('show tables', [], $db);
if (empty($_tables)) { if (empty($_tables)) {
return $res; return $res;
} }
+10 -9
View File
@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Gii; namespace Gii;
use Database\Db; use Database\Db;
use Database\Model;
use Exception; use Exception;
use ReflectionException; use ReflectionException;
use Kiri\Kiri; use Kiri\Kiri;
@@ -68,10 +69,10 @@ namespace ' . $namespace . ';
$html .= $imports . PHP_EOL; $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; $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; $html .= 'use Annotation\Model\Get;' . PHP_EOL;
} }
} catch (\Throwable $e) { } catch (\Throwable $e) {
@@ -92,7 +93,7 @@ use Database\Connection;
use Annotation\Model\Get; use Annotation\Model\Get;
use Annotation\Model\Set; use Annotation\Model\Set;
use Annotation\Model\Relation; use Annotation\Model\Relation;
use Database\ActiveRecord; use Database\Model;
' . PHP_EOL; ' . PHP_EOL;
} }
@@ -111,7 +112,7 @@ use Database\ActiveRecord;
*' . implode('', $this->visible) . ' *' . implode('', $this->visible) . '
* @sql * @sql
*/ */
#[Target] class ' . $managerName . ' extends ActiveRecord #[Target] class ' . $managerName . ' extends Model
{ {
'; ';
@@ -220,8 +221,6 @@ use Database\ActiveRecord;
if (!empty($prefixed)) { if (!empty($prefixed)) {
if (str_starts_with($field, $prefixed)) { if (str_starts_with($field, $prefixed)) {
$field = str_replace($prefixed, '', $field); $field = str_replace($prefixed, '', $field);
$field = '{{%' . $field . '}}';
} }
} }
@@ -384,7 +383,10 @@ use Database\ActiveRecord;
return null; return null;
} }
return ' return '
public ?string $primary = \'' . $field . '\';'; /**
* @var string|null
*/
protected ?string $primary = \'' . $field . '\';';
} }
/** /**
@@ -394,8 +396,7 @@ use Database\ActiveRecord;
{ {
return ' return '
/** /**
* @return Connection * @var string
* @throws Exception
*/ */
protected string $connection = \'' . $this->db->id . '\'; protected string $connection = \'' . $this->db->id . '\';
'; ';