Created
August 2, 2012 23:27
-
-
Save missinglink/3241917 to your computer and use it in GitHub Desktop.
Doctrine2 Reverse Engineer Sqlite Database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use \Doctrine\ORM\Tools\EntityGenerator, | |
\Doctrine\Common\ClassLoader, | |
\Doctrine\ORM\Configuration, | |
\Doctrine\Common\Cache\ArrayCache, | |
\Doctrine\ORM\EntityManager, | |
\Doctrine\ORM\Mapping\Driver\DatabaseDriver, | |
\Doctrine\ORM\Tools\DisconnectedClassMetadataFactory; | |
// autoload | |
$classLoader = new ClassLoader( 'Entities', __DIR__ ); | |
$classLoader->register(); | |
$classLoader = new ClassLoader( 'Proxies', __DIR__ ); | |
$classLoader->register(); | |
// config | |
$config = new Configuration; | |
$config->setMetadataDriverImpl( $config->newDefaultAnnotationDriver( sprintf( '%s/Entities', __DIR__ ) ) ); | |
$config->setMetadataCacheImpl( new ArrayCache ); | |
$config->setProxyDir( sprintf( '%s/Proxies', __DIR__ ) ); | |
$config->setProxyNamespace( 'Proxies' ); | |
// entity manager | |
$em = EntityManager::create( array( | |
'driver' => 'pdo_sqlite', | |
'path' => __DIR__ . '/example.config.sqlite', | |
), | |
$config | |
); | |
// custom datatypes (not mapped for reverse engineering) | |
$em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping( 'set', 'string' ); | |
$em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping( 'enum', 'string' ); | |
// fetch metadata | |
$em->getConfiguration()->setMetadataDriverImpl( | |
new DatabaseDriver( | |
$em->getConnection()->getSchemaManager() | |
) | |
); | |
$cmf = new DisconnectedClassMetadataFactory; | |
$cmf->setEntityManager( $em ); | |
$generator = new EntityGenerator; | |
$generator->setUpdateEntityIfExists( true ); | |
$generator->setGenerateStubMethods( true ); | |
$generator->setGenerateAnnotations( true ); | |
$generator->generate( $cmf->getAllMetadata(), sprintf( '%s/Entities', __DIR__ ) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment