Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@Wanna1 Wanna1 commented Dec 21, 2015

@feliphebueno

This comment has been minimized.

Copy link

@feliphebueno feliphebueno commented Sep 12, 2016

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

@ricardosaracino

This comment has been minimized.

Copy link

@ricardosaracino ricardosaracino commented Sep 30, 2016

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

This comment has been minimized.

Copy link

@mroderick-thr mroderick-thr commented Apr 24, 2017

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

@dalugoda

This comment has been minimized.

Copy link

@dalugoda dalugoda commented Nov 19, 2017

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

@tawfekov

This comment has been minimized.

Copy link
Owner Author

@tawfekov tawfekov commented Dec 29, 2017

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

@werwolf666

This comment has been minimized.

Copy link

@werwolf666 werwolf666 commented Jan 11, 2018

Thank you, very helpfull.

@fpilee

This comment has been minimized.

Copy link

@fpilee fpilee commented Feb 6, 2018

Thanks all tutorial I've found were for Symfony

@AlexanderWyss

This comment has been minimized.

Copy link

@AlexanderWyss AlexanderWyss commented Feb 26, 2018

This is awesome, thanks man.

@dwgebler

This comment has been minimized.

Copy link

@dwgebler dwgebler commented Apr 5, 2018

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

This comment has been minimized.

Copy link

@MaximeC64 MaximeC64 commented Apr 13, 2018

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

This comment has been minimized.

Copy link

@MP92 MP92 commented Apr 27, 2018

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);
@arupons

This comment has been minimized.

Copy link

@arupons arupons commented May 7, 2018

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

@yourchoice

This comment has been minimized.

Copy link

@yourchoice yourchoice commented Jul 11, 2018

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

This comment has been minimized.

Copy link

@nitheeshunnikrishnan nitheeshunnikrishnan commented Nov 1, 2018

awesome,
you save the day
👍

@catsAND

This comment has been minimized.

Copy link

@catsAND catsAND commented Nov 26, 2018

Thanks. 👍

@HazemNoor

This comment has been minimized.

Copy link

@HazemNoor HazemNoor commented Feb 2, 2020

Thank you

@VickaB

This comment has been minimized.

Copy link

@VickaB VickaB commented Apr 13, 2020

Thank you!

@janbarasek

This comment has been minimized.

Copy link

@janbarasek janbarasek commented Dec 2, 2020

I implement it to Symfony command and Doctrine Composer package here: https://github.com/baraja-core/doctrine/blob/master/src/Orm/DatabaseToDoctrineEntityExtractorCommand.php

@tawfekov

This comment has been minimized.

Copy link
Owner Author

@tawfekov tawfekov commented Dec 4, 2020

@janbarasek Thank you for your improvements

@michealzh

This comment has been minimized.

Copy link

@michealzh michealzh commented Apr 2, 2021

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

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