Skip to content

Instantly share code, notes, and snippets.

@tawfekov
Last active October 4, 2024 11:37
Show Gist options
  • Save tawfekov/4079388 to your computer and use it in GitHub Desktop.
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!';
@catsAND
Copy link

catsAND commented Nov 26, 2018

Thanks. 👍

@HazemNoor
Copy link

Thank you

@VickaB
Copy link

VickaB commented Apr 13, 2020

Thank you!

@janbarasek
Copy link

@tawfekov
Copy link
Author

tawfekov commented Dec 4, 2020

@janbarasek Thank you for your improvements

@michealzh
Copy link

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

@linuxd3v
Copy link

linuxd3v commented Oct 25, 2021

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