Skip to content

Instantly share code, notes, and snippets.

@tawfekov
Last active January 7, 2026 13:22
Show Gist options
  • Select an option

  • Save tawfekov/4079388 to your computer and use it in GitHub Desktop.

Select an option

Save tawfekov/4079388 to your computer and use it in GitHub Desktop.
Doctrine2 Generate Entities form Existing Database
<?php
include '../vendor/autoload.php';
$classLoader = new \Doctrine\Common\ClassLoader('Entities', __DIR__);
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader('Proxies', __DIR__);
$classLoader->register();
// config
$config = new \Doctrine\ORM\Configuration();
$config->setMetadataDriverImpl($config->newDefaultAnnotationDriver(__DIR__ . '/Entities'));
$config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
$config->setProxyDir(__DIR__ . '/Proxies');
$config->setProxyNamespace('Proxies');
$connectionParams = array(
'driver' => 'pdo_mysql',
'host' => 'localhost',
'port' => '3306',
'user' => 'root',
'password' => 'root',
'dbname' => 'dbname',
'charset' => 'utf8',
);
$em = \Doctrine\ORM\EntityManager::create($connectionParams, $config);
// custom datatypes (not mapped for reverse engineering)
$em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('set', 'string');
$em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
// fetch metadata
$driver = new \Doctrine\ORM\Mapping\Driver\DatabaseDriver(
$em->getConnection()->getSchemaManager()
);
$em->getConfiguration()->setMetadataDriverImpl($driver);
$cmf = new \Doctrine\ORM\Tools\DisconnectedClassMetadataFactory($em);
$cmf->setEntityManager($em);
$classes = $driver->getAllClassNames();
$metadata = $cmf->getAllMetadata();
$generator = new Doctrine\ORM\Tools\EntityGenerator();
$generator->setUpdateEntityIfExists(true);
$generator->setGenerateStubMethods(true);
$generator->setGenerateAnnotations(true);
$generator->generate($metadata, __DIR__ . '/Entities');
print 'Done!';
@Wanna1

Wanna1 commented Dec 21, 2015

Copy link
Copy Markdown

@feliphebueno

Copy link
Copy Markdown

Thank you so much. This simple script saved my day and a lot of time. 😄 👍

@ricardosaracino

ricardosaracino commented Sep 30, 2016

Copy link
Copy Markdown

Beautiful, saved me a ton of effort

namespaced using

// fetch metadata
$driver = new \Doctrine\ORM\Mapping\Driver\DatabaseDriver(
$em->getConnection()->getSchemaManager()
);

$driver->setNamespace('App\Model\Entity');

$em->getConfiguration()->setMetadataDriverImpl($driver);

@mroderick-thr

Copy link
Copy Markdown

Saved me a ton of effort as well! Why is this feature buried so far inside the workings of Doctrine?

@dalugoda

Copy link
Copy Markdown

this is awesome, i have wasted hours to do this ! Thank you very much !!!

@tawfekov

Copy link
Copy Markdown
Author

I'm happy to see this gist being used till today , it's ~ 5 years old :)

@werwolf666

Copy link
Copy Markdown

Thank you, very helpfull.

@fpilee

fpilee commented Feb 6, 2018

Copy link
Copy Markdown

Thanks all tutorial I've found were for Symfony

@AlexanderWyss

Copy link
Copy Markdown

This is awesome, thanks man.

@dwgebler

dwgebler commented Apr 5, 2018

Copy link
Copy Markdown

For Symfony 4 generate entities + repository classes from existing database, same as you'd get using the make:entity command, I modified your gist slightly:

https://gist.github.com/dwgebler/4be59b84a5a4dd5bd6b90ad0efb22a35

Thanks btw, just saved me a ton of work manually mapping my existing database from a Silex project using DBAL to Symfony 4 w/ Doctrine ORM :)

@MaximeC64

Copy link
Copy Markdown

Hello dwgebler,
I search a system to import a existing database for a while so thanks for your job.
But i'm a beginner and i don't know where i have to put this .php file ?
Somebody can help me please ?

Thanks.
Sorry for my english, i'm french.

@MP92

MP92 commented Apr 27, 2018

Copy link
Copy Markdown

Thanks man, very cool tool!

Specific tables entity generation (not all tables, only specified):

// fetch metadata
$driver = new \Doctrine\ORM\Mapping\Driver\DatabaseDriver(
    $em->getConnection()->getSchemaManager()
);

$specificTables = [
    $em->getConnection()->getSchemaManager()->listTableDetails('table_one'),
    $em->getConnection()->getSchemaManager()->listTableDetails('table_two')
];

$driver->setTables($specificTables, []);

$em->getConfiguration()->setMetadataDriverImpl($driver);

@aruponse

aruponse commented May 7, 2018

Copy link
Copy Markdown

Thanks, this help a lot, and really really thanks to @MP92 you actually save my day.

@yourchoice

Copy link
Copy Markdown

Fatal error: Uncaught Doctrine\ORM\Mapping\MappingException: Table _article_recipe has no primary key. Doctrine does not support reverse engineering from tables that don't have a primary key. in E:_test\doctrine\vendor\doctrine\orm\lib\Doctrine\ORM\Mapping\Driver\DatabaseDriver.php:288

@nitheeshunnikrishnan

Copy link
Copy Markdown

awesome,
you save the day
👍

@catsAND

catsAND commented Nov 26, 2018

Copy link
Copy Markdown

Thanks. 👍

@HazemNoor

Copy link
Copy Markdown

Thank you

@VickaB

VickaB commented Apr 13, 2020

Copy link
Copy Markdown

Thank you!

@janbarasek

Copy link
Copy Markdown

@tawfekov

tawfekov commented Dec 4, 2020

Copy link
Copy Markdown
Author

@janbarasek Thank you for your improvements

@michealzh

Copy link
Copy Markdown

@janbarasek Thanks for sharing.It's awesome for my project.

@linuxd3v

linuxd3v commented Oct 25, 2021

Copy link
Copy Markdown

Its interesting that code generation got deprecated and to be removed in version 3.
Via EntityGenerator as in example above or through cli.
I wonder how would one get started with preexisting databases then (after v3 is stable)? probably just writing from scratch which seems pita.

see notes here: https://github.com/doctrine/orm/blob/3.0.x/UPGRADE.md

Deprecated code generators and related console commands
These console commands have been deprecated:
orm:convert-mapping
orm:generate:entities
orm:generate-repositories
These classes have been deprecated:
Doctrine\ORM\Tools\EntityGenerator
Doctrine\ORM\Tools\EntityRepositoryGenerator
Whole Doctrine\ORM\Tools\Export namespace with all its members have been deprecated as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment