Bon*_*uar 5 sql schema entities symfony doctrine-orm
我想从我的实体生成 SQL。
我知道命令 php app/console doctrine:schema:create --dump-sql和doctrine:schema:update,但显然没有办法过滤 Entities,甚至不能过滤 Bundle,而只能过滤 EntityManager ?!
我错过了什么 ?我认为这是一个很常见的需求,而且很容易开发。
PS我需要它,因为我有一个与其他软件共享的旧的奇怪数据库,这并不完全是Doctrine想要的,所以如果我不过滤,我会遇到一些错误,或者在最好的情况下会出现很多无用/错误的错误修改。
由于这里唯一提出的答案不适合我,并且该主题似乎是我发现的唯一引用此问题的答案,因此我有一个解决方案(请注意,我在 SF2 ContainerAwareCommand 中使用它):
namespace AppBundle\Command;
use Doctrine\DBAL\Schema\Comparator;
use Doctrine\ORM\Tools\SchemaTool;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
/* ... */
class MyCommand extends ContainerAwareCommand
{
/* ... */
protected function execute(InputInterface $input, OutputInterface $output)
{
$em = $this->getContainer()->get('doctrine.orm.entity_manager');
/* Doctrine tool class for create/drop/update database schemas
based on metadata class descriptors */
$tool = new SchemaTool($em);
/* Doctrine tool class for comparing differences between database
schemas */
$comparator = new Comparator();
/* Create an empty schema */
$fromSchema = $tool->getSchemaFromMetadata(array());
/* Create the schema for our class */
$toSchema = $tool->getSchemaFromMetadata(
array($em->getClassMetadata('AppBundle:MyEntity'))
);
/* Compare schemas, and write result as SQL */
$schemaDiff = $comparator->compare($fromSchema, $toSchema);
$sql = $schemaDiff->toSql(
$em->getConnection()->getDatabasePlatform()
);
$output->writeln($sql);
}
}
Run Code Online (Sandbox Code Playgroud)
您可以过滤实体管理器,但您需要在配置中手动注册它们:
orm:
auto_generate_proxy_classes: %kernel.debug%
entity_managers:
default:
mappings:
OneBundle:
type: annotation
AnotherBundle:
type: annotation
another_entity_manager:
mappings:
SomeOtherBundle:
type: annotation
Run Code Online (Sandbox Code Playgroud)
您可以使用这种方式,例如:
php app/console doctrine:schema:update --dump-sql --em=another_entity_manager
Run Code Online (Sandbox Code Playgroud)
这应该只更新 SomeOtherBundle 中实体的架构。
| 归档时间: |
|
| 查看次数: |
7573 次 |
| 最近记录: |