generate annotated doctrine2 entites from db schema

前端 未结 4 1270
名媛妹妹
名媛妹妹 2021-01-31 12:43

Is it possible to generate Doctrine 2 entities, with the relevant docblock annotations, from an existing database schema?

4条回答
  •  再見小時候
    2021-01-31 13:40

    I had to made these changes for the above code to work..

    register();
    
    $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(
        'path' => 'test.sqlite3',
        'driver' => 'pdo_sqlite',
    );
    
    $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 EntityGenerator();
    $generator->setUpdateEntityIfExists(true);
    $generator->setGenerateStubMethods(true);
    $generator->setGenerateAnnotations(true);
    $generator->generate($metadata, __DIR__ . '/Entities');
    print 'Done!';
    ?>
    

    and mysql connection configuration like :

    $connectionParams = array(
        'driver' => 'pdo_mysql',
        'host' => 'localhost',
        'port' => '3306',
        'user' => 'root',
        'password' => 'root',
        'dbname' => 'database',
        'charset' => 'utf8',
    );
    

提交回复
热议问题