Skip to content
You have unread notifications

Account menu

najmi9 Imad Najmi
najmi9  /   PassportScan Developer  /  

Reverse engineering: Generate entities from database

Setup the connection to an existing database through the environment variable:

DATABASE_URL="mysql://root:password@mysql-identity-api-v2/identity_api?serverVersion=8.2.1&charset=utf8mb4"

Currently doctrine import databases to:

  • xml
  • annotation
  • php
  • yaml

But not to php 8.2 attributes.

So for this I will use rector to move the annotation to valid php attributes automatically.

Generate entities from database

php bin/console doctrine:mapping:import App\\Entity annotation --path=src/Entity

Convert annotations to attributes

  1. Install Rector: composer require --dev rector/rector.
  2. Create a file called rector.php at the root of the project with the following contents:
    declare(strict_types=1);

    use Rector\Config\RectorConfig;
    use Rector\Doctrine\Set\DoctrineSetList;

    return function (RectorConfig $rectorConfig): void {
        $rectorConfig->paths([
            __DIR__ . '/tests',
        ]);
        $rectorConfig->sets([
            DoctrineSetList::ANNOTATIONS_TO_ATTRIBUTES,
        ]);
    };
  1. Run vendor/bin/rector process src/Entity, which obeys the above configuration.
  2. Uninstall Rector: composer remove rector/rector && rm rector.php

Generate getters and setters for the properties

php bin/console make:entity --regenerate --api-resource
Create new page · najmi9/PassportScan Developer Wiki