dodo-it/dibi-entity-generator

为dibi从数据库生成实体

2.1.1 2024-09-16 12:21 UTC

This package is auto-updated.

Last update: 2024-09-16 12:22:39 UTC


README

高度可配置的数据库类型实体生成器。它可以生成整个数据库、表/视图以及查询的实体。

这是dibi/nette桥接https://github.com/dodo-it/entity-generator/

Latest Stable Version Total Downloads License

安装

$ composer require dodo-it/dibi-entity-generator

注册

  extensions:
    entityGenerator: DodoIt\DibiEntityGenerator\DI\DibiEntityGeneratorExtension

配置

示例

entityGenerator:
    path: %appDir%/Model/Entity
    namespace: App\Model\Entity
    extends: App\Model\Entities\BaseEntity
    generateGetters: false
    generateSetters: false
    extends: DodoIt\EntityGenerator\Entity
    propertyVisibility: 'public'

您可以在以下位置查看所有选项及其默认值: https://github.com/dodo-it/entity-generator/blob/master/src/Generator/Config.php

使用方法

抽象实体类

首先创建您的BaseEntity类,所有实体都将从该类扩展,并在配置中将选项extends设置到该类。作为一个起点,您可以使用Dibi\Row并设置为仅生成phpdoc注释,这样不会改变任何内容,但您将在查询中拥有完整的自动完成功能。更好的场景是生成getter和setter,然后它们可以具有返回类型提示...

存储库中的示例代码

public function getById(int $id): ArticleEntity
{
		return $this->db->select('*')->from('articles')->where('id = %i', $id)
				->execute()
				->setRowClass(ArticleEntity::class)
				->fetch();
}

生成所有

要生成所有实体,请在数据库表和视图中运行

console entity:generate 

仅生成一个表/视图

console entity:generate table_name

从查询生成

将您的查询写入.sql文件后,运行命令

 console entity:generate --query-file=path/to/QueryFile.sql EntityName

从包含查询.sql文件的目录生成

将您的查询写入一个文件夹中的.sql文件,然后使用以下命令(重新)生成所有查询的实体

 console entity:generate --query-dir=/path/to/dir