【问题标题】:generate annotated doctrine2 entites from db schema从数据库模式生成带注释的教义2实体
【发布时间】:2010-10-31 22:59:36
【问题描述】:

是否可以从现有的数据库模式生成带有相关 docblock 注释的 Doctrine 2 实体?

【问题讨论】:

  • 考虑不这样做。如果您专门为您的应用程序从头开始创建实体,您将创建更好的实体。稍后用注释映射它们。

标签: annotations doctrine-orm schema docblocks


【解决方案1】:

我必须进行这些更改才能使上述代码正常工作..

<?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',
);

【讨论】:

  • 谢谢!这实际上奏效了。 (不像学说中包含的程序)。 - 虽然我只使用了$connectionParams 下的代码,因为我对你的环境设置没有信心。例如函数register(); 显然是未定义的。无论如何都要大 +1!
  • 这对我来说很好用......对你来说是一个大 +1。对于有同样问题的其他人,请使用此答案,所有其他人都不会工作......
  • @dminer:你的脚本运行成功,节省了我很多时间,因为我是 Doctrine 的新手。此脚本使用不包括 CRUD 操作的 setter 和 getter 方法从数据库创建实体。如果我想在该实体中添加所有基本 CRUD 操作,下一步该怎么做。请注意,我的项目不是交响乐项目,而是一个简单的核心项目。
【解决方案2】:

是的,尽管 RDBMS 数据类型不完全受支持,但您可能需要先尝试一下您的代码,然后才能在项目中使用它。它不像 Doctrine 1.x 过去那样简单,但仍然相当容易。这是我自己使用的一些示例代码(在使用之前正确创建文件夹)

使用 Doctrine\ORM\Tools\EntityGenerator; ini_set("display_errors", "On"); $libPath = __DIR__ 。 '/../lib/doctrine2'; // 自动加载器 需要一次 $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 = 新的 \Doctrine\ORM\Configuration(); $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver(__DIR__ . '/Entities')); $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache); $config->setProxyDir(__DIR__ . '/Proxies'); $config->setProxyNamespace('代理'); $connectionParams = 数组( 'dbname' => 'xx', '用户' => 'root', '密码' => '', '主机' => '本地主机', '驱动程序' => 'pdo_mysql', ); $em = \Doctrine\ORM\EntityManager::create($connectionParams, $config); // 自定义数据类型(未映射用于逆向工程) $em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('set', 'string'); $em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string'); // 获取元数据 $driver = 新的 \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__ . '/Entities'); 打印“完成!”;

【讨论】:

  • 在线失败 $metadata[] = $cmf->getMetadataFor($class);为什么它会尝试获取尚不存在的类的父类?我的意思是,这些是要进行逆向工程的实体的名称。
【解决方案3】:

我已经实现了新命令来实现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();

【讨论】:

    【解决方案4】:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-22
      • 2016-04-26
      • 1970-01-01
      • 1970-01-01
      • 2012-04-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多