Skip to content

Commit

Permalink
feat(controller-generator): add dynamic primary key retrieval (#107)
Browse files Browse the repository at this point in the history
ControllerGenerator now dynamically fetches the primary key column name
from the provided table columns rather than relying on a static method.
This enhancement allows for more flexible code generation accommodate tables
with non-standard primary key names.
  • Loading branch information
People-Sea authored Jul 1, 2024
1 parent 9c7e102 commit a4e0ff8
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/ControllerGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

namespace Mine\Generator;

use Hyperf\Collection\Collection;
use Hyperf\Support\Filesystem\Filesystem;
use Mine\Exception\NormalStatusException;
use Mine\Generator\Contracts\GeneratorTablesContract;
Expand All @@ -47,6 +48,8 @@ class ControllerGenerator extends MineGenerator implements CodeGenerator

protected Filesystem $filesystem;

protected Collection $columns;

/**
* 设置生成信息.
* @throws ContainerExceptionInterface
Expand All @@ -59,6 +62,7 @@ public function setGenInfo(GeneratorTablesContract $tablesContract): ControllerG
if (empty($tablesContract->getModuleName()) || empty($tablesContract->getMenuName())) {
throw new NormalStatusException(t('setting.gen_code_edit'));
}
$this->columns = $tablesContract->getColumns();
$this->setNamespace($this->tablesContract->getNamespace());
return $this->placeholderReplace();
}
Expand Down Expand Up @@ -331,9 +335,17 @@ protected function getDtoClass(): string
);
}

/**
* 生成代码表主键.
*/
protected function getPk(): string
{
return $this->tablesContract->getPkName();
foreach ($this->columns as $column) {
if ($column->is_pk == self::YES) {
return $column->column_name;
}
}
return '';
}

protected function getStatusValue(): string
Expand Down

0 comments on commit a4e0ff8

Please sign in to comment.