2010-10-31 66 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();