是否可以从现有的数据库模式生成Doctrine 2实体和相关的docblock注释?从数据库模式生成带注释的doctrine2 entite
回答
我不得不做出这些变化对上面的代码工作..
<?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! – vbence 2011-07-08 10:50:38
这对我来说工作得很好......给你一个大+1。对于其他人谁具有同样的问题,请使用这个答案,所有其他人都不行... – Tareq 2012-01-28 07:16:19
@dminer:你的脚本运行succeffully并保存大量的矿山时间,我是新来BIE学说。该脚本使用setter和getter方法从数据库创建实体,但不包括CRUD操作。如果我想在该实体中添加所有基本的CRUD操作,下一步该做什么。请注意,我的项目不是一个交响乐项目,而是一个简单的核心项目。 – neeraj 2013-01-03 09:45:37
是的,尽管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!';
我已经实现了新的命令来实现这一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();
- 1. 从JPA注释实体类自动生成数据模式
- 2. 如何使用注释配置从实体生成数据库模式?
- 3. 从c#生成数据库模式类
- 4. 如何从emf模型生成带注释的'java代码'
- 5. 从生成的类生成数据注释
- 6. 带OneToOne注释的数据库结构
- 7. 从JPA注释生成DDL
- 8. 从Java JAXB注释类生成的模式中生成XSD限制
- 9. 如何从JAXB注释类生成JSON模式?
- 10. 使用带Hibernate注释POJO的节俭生成模型
- 11. 亚音速数据库模式生成
- 12. 生成数据库模式在IntelliJ15
- 13. Erlang数据库模式生成器
- 14. ASP.NET MVC 2动态生成模型中的数据注释
- 15. 在doctrine2中生成模型
- 16. 从Yii模型生成数据库模式
- 17. 从MySQL数据库中为Doctrine生成YAML模式或模型
- 18. 从模型类生成数据库
- 19. Symfony从模型生成数据库
- 20. Amchart注释。从注释模式返回正常模式
- 21. 从数据库模式生成基于REST的服务
- 22. 从数据库模式中生成类的类
- 23. 实体框架核心数据注释数据库生成的值
- 24. 使用MySQL作为数据库从模型生成数据库
- 25. 数据注释如果从控制列表视图模型生成不不行
- 26. HIbernate - HBM.xml从数据库模式自动生成
- 27. 从NHibernate映射生成数据库模式
- 28. 从数据库模式生成自定义表格
- 29. 从现有模式生成初始数据库迁移脚本
- 30. SQL Server数据库模式是否可以从.NET类生成
考虑不要这样做。如果您专门为您的应用程序创建它们,您将创建更好的实体。稍后将它们与注释进行映射。 – rojoca 2010-11-02 18:24:50