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);
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;
}
+10 -9
View File
@@ -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 . '\';
';