Componette

Componette

dodo-it

dodo-it / entity-generator 2.1.1

Highly customizable (typed) entity generator from database. It can generate entities for whole database, table/view and from raw SQL query

download-cloud-line composer require dodo-it/entity-generator

Entity generator

Highly customizable (typed) entity generator from database. It can generate entities for whole database, table/view and from raw SQL query


Latest Stable Version build Coverage Status PHPStan Total Downloads License

Installation

$ composer require dodo-it/entity-generator

How to run:

   $config = new \DodoIt\EntityGenerator\Generator\Config([
       'path' =>  __DIR__ . '/Entities',
       'extends' => \Examples\Pdo\Entities\Entity::class,
       'namespace' => 'Examples\Pdo\Entities'
   ]);

   $pdo = new \PDO('mysql:dbname=example;host=127.0.0.1', 'root', '');

   $generatorFactory = new \DodoIt\EntityGenerator\Factory\GeneratorPdoFactory($pdo);
   $generator = $generatorFactory->create($config);
   $generator->generate();

What kind of entities can I get?

Tool is highly customizable and can generate various different entity types of which most interesting are:

  • PHP 7.4 typed properties
class ArticleEntity extends YourBaseEntity
{
   public int $id;

   public ?string $title;

   public bool $published;

   public ?\DateTimeInterface $created_at;
}
  • properties with phpdoc
class ArticleEntity extends YourBaseEntity
{

	/** @var int */
	protected $id;

	/** @var string */
	protected $title;

	/** @var bool */
	protected $published;

	/** @var \DateTimeInterface */
	protected $created_at;
}
  • properties with getters and setters (methods body is customizable)
class ArticleEntity extends YourBaseEntity
{

	public function getId(): int
	{
		return $this->id;
	}


	public function setId(int $value): self
	{
		$this['id'] = $value;
		return $this;
	}


	public function getTitle(): ?string
	{
		return $this->title;
	}


	public function setTitle(?string $value): self
	{
		$this['title'] = $value;
		return $this;
	}


	public function getPublished(): bool
	{
		return $this->published;
	}


	public function setPublished(bool $value): self
	{
		$this['published'] = $value;
		return $this;
	}


	public function getCreatedAt(): ?\DateTimeInterface
	{
		return $this->created_at;
	}


	public function setCreatedAt(?\DateTimeInterface $value): self
	{
		$this['created_at'] = $value;
		return $this;
	}
  • phpdoc properties
/**
 * @property int $id
 * @property string $title
 * @property int $published
 * @property \DateTimeInterface $created_at
 */
class ArticleEntity extends YourBaseEntity
{
}
  • properties with phpdoc
class ArticleEntity extends YourBaseEntity
{
	/** @var int */
	public $id;

	/** @var string */
	public $title;

	/** @var bool */
	public $published;

	/** @var \DateTimeInterface */
	public $created_at;
  • it can generate column constants (use generateColumnConstants option)
class ArticleEntity extends YourBaseEntity
{
    	public const TABLE_NAME = 'articles';
    	public const ID = 'id';
    	public const TITLE = 'title';
    	public const PUBLISHED = 'published';
    	public const CREATED_AT = 'created_at';

.
.
.
  • almost any combination you can imagine, check src/Generator/Config.php for list of all options see example folder

You can add your own methods to entities and change getter/setter functions, they won't be overriden when regenerated if rewrite flag is set to false

Configuration

see src/Generator/Config.php

Docker integration

Entity Generator image

Docker Pulls https://hub.docker.com/r/pifou25/entity-generator

Generation from existing database

The database should be accessible from the docker network, the hostname is either the name of the Mysql running container, or localhost if db is running on host. The below command will generate entities into ./entities directory.

docker run --rm -v $PWD/entities:/app/entities --network some-network \
   -e MYSQL_HOSTNAME=some-mariadb \
   -e MYSQL_DATABASE=exmple-database \
   -e MYSQL_USERNAME=example-user \
   -e MYSQL_PASSWORD=my_cool_secret \
    pifou25/entity-generator

namespace and extends example

The base class must exist into /entities to be declared

docker run --rm -v $PWD/include/entities:/app/entities --network scripts_default \
   -e MYSQL_HOSTNAME=myhost \
   -e MYSQL_DATABASE=mydb \
   -e MYSQL_USERNAME=myuser \
   -e MYSQL_PASSWORD=mypwd \
   -e ENTITY_NAMESPACE=Example\\Pdo\\Entities \
   -e BASECLASS=Example\\Pdo\\Entities\Entity \
    entity-generator

Generation from flat plain SQL file

The docker compose file create a new database and initialize it with SQL data, you have to put SQL init file into ./examples directory. When db is ready, the entity-generator start on it to generate PHP entities.

It is as simple as running this command from the docker directory : docker compose up

  • 2.1.1 2.1.1

    What's Changed

    New Contributors

    Full Changelog: 2.1.0...2.1.1

  • 2.1.0 2.1.0

    support for php 8.3
    dropped support for 8.0
    update dependencies

  • 2.0.0

    • dropped support for all php versions < 8
    • support for php 8.1
    • phpstan 1.8
    • contributte qa
    • switch to github actions
  • 1.2.1 1.2.1

    Allow to set extends to null

  • 1.2.0 1.2.0

    • min php 8.0
    • update min dependencies version
    • small code enahncements
  • 1.1.2 1.1.2

    • remove symfony/console dependency
  • 1.1.1 1.1.1

    • PHP 8 support
    • allow adding trait to generated entity
  • 1.1.0 1.1.0

    • php 7.4 support
    • ability to generate typed properties
    • ability to add declare_strict_types
  • 1.0.10 1.0.10

    • move ninjify/qa to dev dependencies
    • update ninjify/qa
    • support for phpunit9
  • 1.0.9 1.0.9

    • fix regenerating mapping
    • fix regenerating constants
    • add more unit tests
  • 1.0.8 1.0.8

    • constants are allways regenerated becase phpgenerator <=2.6.* does not have info on constants
    • mapping is now being merged so it doesn't skip mappings from previous version of entity
  • 1.0.7 1.0.7

    • support for !rewrite with phpdoc properties
  • 1.0.6 1.0.6

    • by default use \DateTimeInterface for datetime instead of \DateTime
    • option to generate column constant
    • better replacements per word
  • 1.0.5

    • option to rewrite config (generate new entity and overwrite all existing files)
    • add mapping (array where key is table column name and value is entity name)
    • allow setting getters/setters body
    • make sure column constant name is not class (add _ before if it is)
  • 1.0.4 1.0.4

    • bugfix: use table constant name from table
  • 1.0.3 1.0.3

    • singularize all words not just last one
    • allow setting visibility on getters and setters
  • 1.0.2 1.0.2

    • feature: option to generate phpdoc properties
    • bugfix: skip generating properties if generateProperties = false
  • 1.0.1 1.0.1

    • support for generating column constant
    • setup travis build and add badges to readme
  • 1.0.0 1.0.0

Componette Componette felix@nette.org