既存のデータベース スキーマから、関連する docblock 注釈を使用して Doctrine 2 エンティティを生成することは可能ですか?
9264 次
4 に答える
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',
);
于 2011-03-18T04:11:00.707 に答える
2
はい、可能ですが、RDBMS データ型は完全にはサポートされていないため、プロジェクトで使用する前にコードを少しいじる必要があるかもしれません。Doctrine 1.x のように簡単ではありませんが、それでもかなり簡単です。ここで私が自分で使用したいくつかのサンプルコード(使用する前に適切にフォルダを作成してください)
Doctrine\ORM\Tools\EntityGenerator を使用します。 ini_set("display_errors", "オン"); $libPath = __DIR__ . '/../lib/doctrine2'; // オートローダー 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('プロキシ', __DIR__); $classLoader->register(); // 設定 $config = new \Doctrine\ORM\Configuration(); $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver(__DIR__ . '/Entities')); $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache); $config->setProxyDir(__DIR__ . '/プロキシ'); $config->setProxyNamespace('プロキシ'); $connectionParams = 配列( 'データベース名' => 'xx', 'ユーザー' => 'ルート', 'パスワード' => '', 'ホスト' => 'ローカルホスト', 'ドライバー' => 'pdo_mysql', ); $em = \Doctrine\ORM\EntityManager::create($connectionParams, $config); // カスタム データ型 (リバース エンジニアリング用にマッピングされていません) $em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('set', 'string'); $em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string'); // メタデータを取得 $driver = new \Doctrine\ORM\Mapping\Driver\DatabaseDriver( $em->getConnection()->getSchemaManager() ); $classes = $driver->getAllClassNames(); foreach ($classes を $class として) { //サポートされていないテーブル/スキーマをここで処理して、一部のクラスを除外できます 真であれば) { $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__ . '/エンティティ'); print '完了!';
于 2010-11-02T10:04:50.617 に答える
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();
于 2010-12-03T10:47:39.667 に答える
0
https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/Tools/Console/ConsoleRunner.phpの時点で、エンティティの生成は Doctrine のデフォルト CLI ですでにサポートされています
于 2012-01-19T11:13:08.480 に答える