tp6从mysql字段生成注解

2023-04-07  本文已影响0人  耍帅oldboy
class dbinfo extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('dbinfo')
            ->addOption('table',null,Option::VALUE_REQUIRED)
            ->setDescription('the dbinfo command');
    }

    protected function execute(Input $input, Output $output)
    {
        // 指令输出
        $output->writeln('dbinfo');

        $columns = \think\facade\Db::query("SELECT COLUMN_NAME, DATA_TYPE , COLUMN_COMMENT
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = '{$input->getOption('table')}'");
        $annotation = "";
        foreach ($columns as $column) {
            $type = 'string';
            if (in_array($column['DATA_TYPE'], ['int', 'tinyint', 'smallint', 'mediumint', 'bigint'])) {
                $type = 'int';
            } elseif (in_array($column['DATA_TYPE'], ['float', 'double', 'decimal'])) {
                $type = 'float';
            }
            $columnName = $column['COLUMN_NAME'];
            if (in_array($columnName, ['created_at', 'updated_at', 'deleted_at'])) {
                $type = '\\Carbon\\Carbon';
            }
            $columnComment = $column['COLUMN_COMMENT'];
            $annotation    .= sprintf("\n * @property   %s  \$%s  %s", $type, $columnName, $columnComment);
        }
        $annotation .= "\n";
        // 指令输出
        $output->writeln($annotation);
    }
}

上一篇下一篇

猜你喜欢

热点阅读