2010-10-31 120 views

回答

8

我不得不做出这些变化对上面的代码工作..

<?php 
use Doctrine\ORM\Tools\EntityGenerator; 
ini_set("display_errors", "On"); 
$libPath = __DIR__; // Set this to where you have doctrine2 installed 
// autoloaders 
require_once $libPath . '/Doctrine/Common/ClassLoader.php'; 

$classLoader = new \Doctrine\Common\ClassLoader('Doctrine', $libPath); 
$classLoader->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!'; 
?> 

和MySQL连接配置,如:

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

谢谢!这实际上工作。 (不像包含在教条中的程序)。 - 虽然我只使用'$ connectionParams'下的代码,因为我对自己的环境设置没有信心。例如,函数register();显然是未定义的。反正大+1! – vbence 2011-07-08 10:50:38

+0

这对我来说工作得很好......给你一个大+1。对于其他人谁具有同样的问题,请使用这个答案,所有其他人都不行... – Tareq 2012-01-28 07:16:19

+0

@dminer:你的脚本运行succeffully并保存大量的矿山时间,我是新来BIE学说。该脚本使用setter和getter方法从数据库创建实体,但不包括CRUD操作。如果我想在该实体中添加所有基本的CRUD操作,下一步该做什么。请注意,我的项目不是一个交响乐项目,而是一个简单的核心项目。 – neeraj 2013-01-03 09:45:37

2

是的,尽管RDBMS数据类型没有完全支持,所以在项目中使用它之前,您可能需要稍微玩一下代码。这并不是直接的,因为Doctrine 1.x曾经是,但仍然相当容易。这里是一些示例代码,我用我自己(在使用它之前正确地创建文件夹)

 
use Doctrine\ORM\Tools\EntityGenerator; 

ini_set("display_errors", "On"); 

$libPath = __DIR__ . '/../lib/doctrine2'; 

// autoloaders 
require_once $libPath . '/Doctrine/Common/ClassLoader.php'; 

$classLoader = new \Doctrine\Common\ClassLoader('Doctrine', $libPath); 
$classLoader->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(
    'dbname' => 'xx', 
    'user' => 'root', 
    'password' => '', 
    'host' => 'localhost', 
    'driver' => 'pdo_mysql', 
); 

$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() 
); 
$classes = $driver->getAllClassNames(); 
foreach ($classes as $class) { 
    //any unsupported table/schema could be handled here to exclude some classes 
    if (true) { 
     $metadata[] = $cmf->getMetadataFor($class); 
    } 
} 

$em->getConfiguration()->setMetadataDriverImpl($driver); 
$cmf = new \Doctrine\ORM\Tools\DisconnectedClassMetadataFactory($em); 

$generator = new EntityGenerator(); 
$generator->setUpdateEntityIfExists(true); 
$generator->setGenerateStubMethods(true); 
$generator->setGenerateAnnotations(true); 
$generator->generate($metadata, __DIR__ . '/Entities'); 

print 'Done!'; 
+0

失败,行$ metadata [] = $ cmf-> getMetadataFor($ class); 为什么它会尝试获取尚不存在的类的父级?我的意思是,这些是将要进行逆向工程的实体的名称。 – DaTroop 2010-12-03 14:44:37

+0

这不工作... – Tareq 2012-01-28 07:17:39

0

我已经实现了新的命令来实现这一https://github.com/umpirsky/doctrine2/blob/master/lib/Doctrine/ORM/Tools/Console/Command/GenerateEntitiesDbCommand.php

只需添加这样的:

$cli->addCommands(array(
// DBAL Commands 
new \Doctrine\DBAL\Tools\Console\Command\RunSqlCommand(), 
new \Doctrine\DBAL\Tools\Console\Command\ImportCommand(), 

// ORM Commands 
new \Doctrine\ORM\Tools\Console\Command\ClearCache\MetadataCommand(), 
new \Doctrine\ORM\Tools\Console\Command\ClearCache\ResultCommand(), 
new \Doctrine\ORM\Tools\Console\Command\ClearCache\QueryCommand(), 
new \Doctrine\ORM\Tools\Console\Command\SchemaTool\CreateCommand(), 
new \Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand(), 
new \Doctrine\ORM\Tools\Console\Command\SchemaTool\DropCommand(), 
new \Doctrine\ORM\Tools\Console\Command\EnsureProductionSettingsCommand(), 
new \Doctrine\ORM\Tools\Console\Command\ConvertDoctrine1SchemaCommand(), 
new \Doctrine\ORM\Tools\Console\Command\GenerateRepositoriesCommand(), 
new \Doctrine\ORM\Tools\Console\Command\GenerateEntitiesCommand(), 
new \Doctrine\ORM\Tools\Console\Command\GenerateEntitiesDbCommand(), 
new \Doctrine\ORM\Tools\Console\Command\GenerateProxiesCommand(), 
new \Doctrine\ORM\Tools\Console\Command\ConvertMappingCommand(), 
new \Doctrine\ORM\Tools\Console\Command\RunDqlCommand(), 
new \Doctrine\ORM\Tools\Console\Command\ValidateSchemaCommand(), 

)) ; $ cli-> run();