通过Doctrine Entity类生成sql并插入到指定的数据库中

php依赖:
composer create-project symfony/skeleton:"6.4.*" make-sql
doctrine/doctrine-bundle
doctrine/orm
命令:php src/scm.php
scm.php脚本:
code
<?php

require_once 'vendor/autoload.php';

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\Tools\SchemaTool;

$paths = array(DIR . '/Entity');

isDevMode = true;config = Setup::createAttributeMetadataConfiguration(paths,isDevMode);

$entityManager = EntityManager::create(array(
'driver'   => 'pdo_mysql',
'host'     => 'localhost',
'dbname'   => 'admin',
'user'     => 'qingu',
'password' => 'yyJ1234',
), $config);

$tool = new \Doctrine\ORM\Tools\SchemaTool($entityManager);

classes = array(entityManager->getClassMetadata('Entity\Tickets'),
);
tool->createSchema(classes);

在一个symfony项目中:
src\Entity\Tickets.php:
code
<?php

namespace App\Entity;

use DateTime;
use Doctrine\ORM\Mapping as ORM;

/**

  • Tickets
    */

[ORM\Entity]

[ORM\Table(name: 'tickets')]

class Tickets
{
/**
* @var int
*/
#[ORM\Column(name: 'id', type: 'integer', nullable: false, options: ['comment' => 'ID'])]
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'IDENTITY')]
private $id;

/**
 * @var int|null
 */
#[ORM\Column(name: 'uid', type: 'smallint', nullable: true)]
private $uid = '0';

/**
 * @var DateTime|null
 */
#[ORM\Column(name: 'optdt', type: 'datetime', nullable: true, options: ['comment' => '操作时间'])]
private $optdt;

/**
 * @var int|null
 */
#[ORM\Column(name: 'optid', type: 'smallint', nullable: true)]
private $optid = '0';

/**
 * @var string|null
 */
#[ORM\Column(name: 'optname', type: 'string', length: 20, nullable: true, options: ['comment' => '操作人'])]
private $optname;

/**
 * @var DateTime|null
 */
#[ORM\Column(name: 'applydt', type: 'date', nullable: true, options: ['comment' => '申请日期'])]
private $applydt;

/**
 * @var string|null
 */
#[ORM\Column(name: 'explain', type: 'string', length: 500, nullable: true, options: ['comment' => '说明'])]
private $explain;

/**
 * @var bool|null
 */
#[ORM\Column(name: 'status', type: 'boolean', nullable: true, options: ['default' => '1', 'comment' => '审核状态'])]
private $status = true;

/**
 * @var bool|null
 */
#[ORM\Column(name: 'isturn', type: 'boolean', nullable: true, options: ['default' => '1', 'comment' => '是否提交'])]
private $isturn = true;

/**
 * @var int|null
 */
#[ORM\Column(name: 'property_id', type: 'integer', nullable: true, options: ['comment' => '资产id'])]
private $propertyId;

/**
 * @var string|null
 */
#[ORM\Column(name: 'property_name', type: 'string', length: 255, nullable: true, options: ['comment' => '资产名称'])]
private $propertyName;

/**
 * @var string|null
 */
#[ORM\Column(name: 'tickets_name', type: 'string', length: 255, nullable: true, options: ['comment' => '工单名称'])]
private $ticketsName;

/**
 * @var DateTime|null
 */
#[ORM\Column(name: 'failure_detection', type: 'date', nullable: true, options: ['comment' => '失效发现时间'])]
private $failureDetection;

/**
 * @var DateTime|null
 */
#[ORM\Column(name: 'schedule_time', type: 'date', nullable: true, options: ['comment' => '安排时间'])]
private $scheduleTime;

/**
 * @var DateTime|null
 */
#[ORM\Column(name: 'object_time', type: 'datetime', nullable: true, options: ['comment' => '目标时间'])]
private $objectTime;

/**
 * @var DateTime|null
 */
#[ORM\Column(name: 'completion_time', type: 'datetime', nullable: true, options: ['comment' => '完成时间'])]
private $completionTime;

/**
 * @var DateTime|null
 */
#[ORM\Column(name: 'down_time', type: 'datetime', nullable: true, options: ['comment' => '停机时间'])]
private $downTime;

/**
 * @var string|null
 */
#[ORM\Column(name: 'production_loss', type: 'string', length: 255, nullable: true, options: ['comment' => '生产损失'])]
private $productionLoss;

/**
 * @var string|null
 */
#[ORM\Column(name: 'principal', type: 'string', length: 255, nullable: true, options: ['comment' => '负责人'])]
private $principal;

/**
 * @var string|null
 */
#[ORM\Column(name: 'principal_id', type: 'string', length: 255, nullable: true, options: ['comment' => '负责人id'])]
private $principalId;

/**
 * @var string|null
 */
#[ORM\Column(name: 'category_id', type: 'string', length: 255, nullable: true, options: ['comment' => '类别id'])]
private $categoryId;

/**
 * @var string|null
 */
#[ORM\Column(name: 'category', type: 'string', length: 255, nullable: true, options: ['comment' => '类别'])]
private $category;

/**
 * @var string|null
 */
#[ORM\Column(name: 'category_type_name', type: 'string', length: 255, nullable: true, options: ['comment' => '类别总类名称'])]
private $categoryTypeName;

/**
 * @var int|null
 */
#[ORM\Column(name: 'category_type_id', type: 'integer', nullable: true, options: ['comment' => '类别总览id'])]
private $categoryTypeId;

/**
 * @var int|null
 */
#[ORM\Column(name: 'degree_priority_id', type: 'integer', nullable: true, options: ['comment' => '优先程度id'])]
private $degreePriorityId;

/**
 * @var string|null
 */
#[ORM\Column(name: 'degree_priority', type: 'string', length: 255, nullable: true, options: ['comment' => '优先程度'])]
private $degreePriority;

/**
 * @var int|null
 */
#[ORM\Column(name: 'specialty_id', type: 'integer', nullable: true, options: ['comment' => '专业id'])]
private $specialtyId;

/**
 * @var string|null
 */
#[ORM\Column(name: 'specialty', type: 'string', length: 255, nullable: true, options: ['comment' => '专业'])]
private $specialty;

/**
 * @var string|null
 */
#[ORM\Column(name: 'describe', type: 'string', length: 255, nullable: true, options: ['comment' => '描述'])]
private $describe;

/**
 * @var DateTime|null
 */
#[ORM\Column(name: 'warranty_time', type: 'datetime', nullable: true, options: ['comment' => '报修日期'])]
private $warrantyTime;

/**
 * @var int|null
 */
#[ORM\Column(name: 'warranty_person_id', type: 'integer', nullable: true, options: ['comment' => '报修人id'])]
private $warrantyPersonId;

/**
 * @var string|null
 */
#[ORM\Column(name: 'warranty_person', type: 'string', length: 255, nullable: true, options: ['comment' => '报修人'])]
private $warrantyPerson;

/**
 * @var DateTime|null
 */
#[ORM\Column(name: 'creation_time', type: 'datetime', nullable: true, options: ['comment' => '创建时间'])]
private $creationTime;

/**
 * @var string|null
 */
#[ORM\Column(name: 'creation_person_id', type: 'string', length: 255, nullable: true, options: ['comment' => '创建人id'])]
private $creationPersonId;

/**
 * @var string|null
 */
#[ORM\Column(name: 'creation_person', type: 'string', length: 255, nullable: true, options: ['comment' => '创建人'])]
private $creationPerson;

/**
 * @var string|null
 */
#[ORM\Column(name: 'maintenance_plan', type: 'string', length: 255, nullable: true, options: ['comment' => '维护计划名称'])]
private $maintenancePlan;

/**
 * @var string|null
 */
#[ORM\Column(name: 'before_tickets', type: 'string', length: 255, nullable: true, options: ['comment' => '之前工单'])]
private $beforeTickets;

/**
 * @var string|null
 */
#[ORM\Column(name: 'maintenance_plan_id', type: 'string', length: 255, nullable: true, options: ['comment' => '维护计划id'])]
private $maintenancePlanId;

/**
 * @var int|null
 */
#[ORM\Column(name: 'state', type: 'integer', nullable: true, options: ['comment' => '状态'])]
private $state;

}

生成的sql:
CREATE TABLE tickets (
id int NOT NULL AUTO_INCREMENT COMMENT 'ID',
uid smallint DEFAULT NULL,
optdt datetime DEFAULT NULL COMMENT '操作时间',
optid smallint DEFAULT NULL,
optname varchar(20) COLLATE utf8mb3_unicode_ci DEFAULT NULL COMMENT '操作人',
applydt date DEFAULT NULL COMMENT '申请日期',
explain varchar(500) COLLATE utf8mb3_unicode_ci DEFAULT NULL COMMENT '说明',
status tinyint(1) DEFAULT '1' COMMENT '审核状态',
isturn tinyint(1) DEFAULT '1' COMMENT '是否提交',
property_id int DEFAULT NULL COMMENT '资产id',
property_name varchar(255) COLLATE utf8mb3_unicode_ci DEFAULT NULL COMMENT '资产名称',
tickets_name varchar(255) COLLATE utf8mb3_unicode_ci DEFAULT NULL COMMENT '工单名称',
failure_detection date DEFAULT NULL COMMENT '失效发现时间',
schedule_time date DEFAULT NULL COMMENT '安排时间',
object_time datetime DEFAULT NULL COMMENT '目标时间',
completion_time datetime DEFAULT NULL COMMENT '完成时间',
down_time datetime DEFAULT NULL COMMENT '停机时间',
production_loss varchar(255) COLLATE utf8mb3_unicode_ci DEFAULT NULL COMMENT '生产损失',
principal varchar(255) COLLATE utf8mb3_unicode_ci DEFAULT NULL COMMENT '负责人',
principal_id varchar(255) COLLATE utf8mb3_unicode_ci DEFAULT NULL COMMENT '负责人id',
category_id varchar(255) COLLATE utf8mb3_unicode_ci DEFAULT NULL COMMENT '类别id',
category varchar(255) COLLATE utf8mb3_unicode_ci DEFAULT NULL COMMENT '类别',
category_type_name varchar(255) COLLATE utf8mb3_unicode_ci DEFAULT NULL COMMENT '类别总类名称',
category_type_id int DEFAULT NULL COMMENT '类别总览id',
degree_priority_id int DEFAULT NULL COMMENT '优先程度id',
degree_priority varchar(255) COLLATE utf8mb3_unicode_ci DEFAULT NULL COMMENT '优先程度',
specialty_id int DEFAULT NULL COMMENT '专业id',
specialty varchar(255) COLLATE utf8mb3_unicode_ci DEFAULT NULL COMMENT '专业',
describe varchar(255) COLLATE utf8mb3_unicode_ci DEFAULT NULL COMMENT '描述',
warranty_time datetime DEFAULT NULL COMMENT '报修日期',
warranty_person_id int DEFAULT NULL COMMENT '报修人id',
warranty_person varchar(255) COLLATE utf8mb3_unicode_ci DEFAULT NULL COMMENT '报修人',
creation_time datetime DEFAULT NULL COMMENT '创建时间',
creation_person_id varchar(255) COLLATE utf8mb3_unicode_ci DEFAULT NULL COMMENT '创建人id',
creation_person varchar(255) COLLATE utf8mb3_unicode_ci DEFAULT NULL COMMENT '创建人',
maintenance_plan varchar(255) COLLATE utf8mb3_unicode_ci DEFAULT NULL COMMENT '维护计划名称',
before_tickets varchar(255) COLLATE utf8mb3_unicode_ci DEFAULT NULL COMMENT '之前工单',
maintenance_plan_id varchar(255) COLLATE utf8mb3_unicode_ci DEFAULT NULL COMMENT '维护计划id',
state int DEFAULT NULL COMMENT '状态',
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci

导出src/Entity下所有表的sql:
php bin/console doctrine:schema:create --dump-sql

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容