Skip to content

Instantly share code, notes, and snippets.

@agustincl
Forked from tawfekov/generator.php
Last active August 29, 2015 14:21
Show Gist options
  • Save agustincl/3ce93f29d0503235e8f8 to your computer and use it in GitHub Desktop.
Save agustincl/3ce93f29d0503235e8f8 to your computer and use it in GitHub Desktop.
<?php
/**
*
* config/autoload/entitiesGenerator.local.php
array(
'entitiesGenerator' => array(
'connection' => array(
'cnx' => array(
'driver' => 'pdo_mysql',
'host' => 'localhost',
'port' => '3306',
'charset' => 'utf8',
//'user' => 'root',
//'password' => '1234',
//'dbname' => 'database',
),
),
'entities' => array(
'namespace' => 'Package\\Entity\\',
'path' => __DIR__.'/../../vendor/module/package/src/'
)
)
*/
$config = include __DIR__.'/../config/autoload/entitiesGenerator.local.php';
$generatorConfig = $config['entitiesGenerator'];
$connectionParams = $generatorConfig['connection']['cnx'];
include __DIR__.'/../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->setMetadataDriverImpl($config->newDefaultAnnotationDriver($generatorConfig['entities']['path']));
$config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
$config->setProxyDir(__DIR__ . '/Proxies');
$config->setProxyNamespace('Proxies');
$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()
);
$driver->setNamespace($generatorConfig['entities']['namespace']);
$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, $generatorConfig['entities']['path']);
print 'Done!';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment