美文网首页
tp6从mysql字段生成注解

tp6从mysql字段生成注解

作者: 耍帅oldboy | 来源:发表于2023-04-07 03:08 被阅读0次
    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);
        }
    }
    
    

    相关文章

      网友评论

          本文标题:tp6从mysql字段生成注解

          本文链接:https://www.haomeiwen.com/subject/snccddtx.html