网站首页 文章专栏 lumen 自动生成model
引用组件:krlove/eloquent-model-generator
1、安装组件
composer require krlove/eloquent-model-generator --dev
2.添加注册项
$app->register(\Krlove\EloquentModelGenerator\Provider\GeneratorServiceProvider::class);
3.执行命令
php artisan krlove:generate:model IncomeCustomer --table-name=income_customer --output-path=./Models --namespace=App\\Models --no-timestamps
配置项可选参数
[
'namespace' => 'App', //命名空间
'base_class_name' => \Illuminate\Database\Eloquent\Model::class,//继承的基类
'output_path' => null, //文件生成地址
'no_timestamps' => null, //生成timestamps属性 --no_timestamps
'date_format' => null, //生成date_format属性
'connection' => null, //数据库链接地址
'no_backup' => null, //备份同名原文件
'db_types' => null, //自定义数据类型,出现特殊数据类型而原组件中没有配置导致报错的
];
eg:
测试一张数据表:
CREATE TABLE `income_customer` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `app_id` smallint(5) unsigned NOT NULL COMMENT '所属平台ID', `plat_member_id` int(10) unsigned NOT NULL COMMENT '所属公司ID', `plat_member_name` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '所属公司名称', `plat_operator_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '所属公司下的操作员ID', `customer_name` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '拥有的客户名称', `customer_organization_num` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '客户的统一社会信用代码、纳税人识别号', `customer_registe_region` varchar(6) COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '注册地区【gb2260 行政区划代码表,可从社会信用代码中同步】', `state` tinyint(1) NOT NULL DEFAULT '0', `create_time` datetime DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=29 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
生成效果如下:
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; /** * @property int $id * @property integer $app_id * @property int $plat_member_id * @property string $plat_member_name * @property int $plat_operator_id * @property string $customer_name * @property string $customer_organization_num * @property string $customer_registe_region * @property boolean $state * @property string $create_time */ class IncomeCustomer extends Model { /** * The table associated with the model. * * @var string */ protected $table = 'income_customer'; /** * @var array */ protected $fillable = ['app_id', 'plat_member_id', 'plat_member_name', 'plat_operator_id', 'customer_name', 'customer_organization_num', 'customer_registe_region', 'state', 'create_time']; /** * Indicates if the model should be timestamped. * * @var bool */ public $timestamps = false; }
看起来能满足基本的需求,美中不足的是没有把数据库的注释带过来,研究了一番,对这个插件做了点调整:
vendor/krlove/eloquent-model-generator/src/Processor/FieldProcessor.php
public function process(EloquentModel $model, Config $config) { $schemaManager = $this->databaseManager->connection($config->get('connection'))->getDoctrineSchemaManager(); $prefix = $this->databaseManager->connection($config->get('connection'))->getTablePrefix(); $tableDetails = $schemaManager->listTableDetails($prefix . $model->getTableName()); $primaryColumnNames = $tableDetails->getPrimaryKey() ? $tableDetails->getPrimaryKey()->getColumns() : []; $columnNames = []; foreach ($tableDetails->getColumns() as $column) { $model->addProperty(new VirtualPropertyModel( $column->getName().' '.$column->getComment(), $this->typeRegistry->resolveType($column->getType()->getName()) )); if (!in_array($column->getName(), $primaryColumnNames)) { $columnNames[] = $column->getName(); } } $fillableProperty = new PropertyModel('fillable'); $fillableProperty->setAccess('protected') ->setValue($columnNames) ->setDocBlock(new DocBlockModel('@var array')); $model->addProperty($fillableProperty); return $this; }
修改后,再次执行生成的命令,得到的结果如下:
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; /** * @property int $id * @property integer $app_id 所属平台ID * @property int $plat_member_id 所属公司ID * @property string $plat_member_name 所属公司名称 * @property int $plat_operator_id 所属公司下的操作员ID * @property string $customer_name 拥有的客户名称 * @property string $customer_organization_num 客户的统一社会信用代码、纳税人识别号 * @property string $customer_registe_region 注册地区【gb2260 行政区划代码表,可从社会信用代码中同步】 * @property boolean $state * @property string $create_time */ class IncomeCustomer extends Model { /** * The table associated with the model. * * @var string */ protected $table = 'income_customer'; /** * @var array */ protected $fillable = ['app_id', 'plat_member_id', 'plat_member_name', 'plat_operator_id', 'customer_name', 'customer_organization_num', 'customer_registe_region', 'state', 'create_time']; /** * Indicates if the model should be timestamped. * * @var bool */ public $timestamps = false; }
完美!
转载请注明出处