Generate Entity, XML, and YML from an existing database using doctrine2
July 4, 2017 | by biplob.ice
Sometimes we may need to generate Entity XML, and YML files from the existing database which saves time. I’m explaining it in a few easy steps. This may also be considered as getting started with doctrine2.
Step-1: If you’re on Mac, open your terminal and go to your project root directory.
mkdir MyProject cd MyProject
Step 2: Assuming you’ve composer installed. If not, check Composer documentation. Run the following commands.
composer require doctrine/orm // If you want to generate yml files composer require symfony/yaml composer install
Step 3: Autoloading is taken care of by Composer. You just have to include the composer autoload file in your project. Once you have prepared the class loading, you acquire an EntityManager instance. Create the following file in your project root directory.
<?php
// bootstrap.php
require_once "vendor/autoload.php";
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
// Create a simple "default" Doctrine ORM configuration
// For Annotations
$paths = [__DIR__."/src"];
// Or if you prefer XML or YML
// $paths = [__DIR__."/config/xml"];
// $paths = [__DIR__."/config/yaml"];
$isDevMode = true;
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
// database configuration parameters
$dbParams = array(
'driver' => 'pdo_mysql',
'user' => 'root',
'password' => 'root',
'dbname' => 'DB_NAME',
);
$entityManager = EntityManager::create($dbParams, $config);
For more details, check the [documentation](http://docs.doctrine-project.org/en/latest/tutorials/getting-started.html)
Step 4: Create the following to use the doctrine console.
<?php // cli-config.php require_once "bootstrap.php"; return \Doctrine\ORM\Tools\Console\ConsoleRunner::createHelperSet($entityManager);
For more details check the documentation.
Step 5: Generate the doctrine2 model from database with the following command. You can use ‘annotation’ or ‘yml’ instead of ‘XML’. And I’m using config/xml as the output folder.
php vendor/bin/doctrine orm:convert-mapping --from-database xml /path/to/output/folder
Step-6 Generate entities from the XML files with the following command.
php vendor/bin/doctrine orm:generate:entities dest/path/ --no-backup
The source code of this project is available here.
Tips
You may get the following error while mapping from the database.
[Doctrine\ORM\Mapping\MappingException] Table FileImageThumbnailPaths has no primary key. Doctrine does not support reverse engineering from tables that don't have a primary key.
Do the following trick to skip this error. Navigate to your vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/DatabaseDriver.php file. Add continue; on line 288. The code will look like as follows-
if ( ! $table->hasPrimaryKey()) {
continue;
throw new MappingException(
"Table " . $table->getName() . " has no primary key. Doctrine does not ".
"support reverse engineering from tables that don't have a primary key."
);
}
RELATED POSTS
View all