2013-01-09 20 views
1

我想在Doctrine Create Schema Event中插入一些记录到数据库(用于外键约束的一些默认数据)。Doctrine 2 Create Schema是否有Event侦听器?

该函数由通过焙烧(在Symfony2中)的命令行调用:

php app/console doctrine:schema:update --force 

当该命令运行,Doctrine2生成用于数据库中的模式。

有什么办法可以对此做出反应,并将数据插入到数据库中?

谢谢!

+0

不,但您可以创建自己的命令:完成模式更新并一次性添加记录。 – phpisuber01

+0

看看[DoctrineFixtureBundle](http://symfony.com/doc/current/bundles/DoctrineFixturesBundle/index.html)!它会帮助你加载初始数据。 – Pierrickouw

+0

@GreenLeaf,你可以添加一个答案?谢谢! –

回答

2

创建一个新的Console Command将完成您的需求。

命令代码

MyCool /包/命令/ GenerateSchemaUpdateCommand.php

<?php 
namespace MyCool\Bundle\Command; 

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; 
use Symfony\Component\Console\Input\InputArgument; 
use Symfony\Component\Console\Input\InputInterface; 
use Symfony\Component\Console\Output\OutputInterface; 

class GenerateSchemaUpdateCommand extends ContainerAwareCommand 
{ 
    /** 
    * Configure command, set parameters definition and help. 
    */ 
    protected function configure() 
    { 
     $this 
      ->setName('mycoolbundle:schema:update') 
      ->setDescription('Perform my custom schema update.') 
      ->setHelp(sprintf(
      'Performs the doctrine:schema:update plus adds our custom records. \n' . 
       PHP_EOL 
     )); 
    } 

    /** 
    * Execution Code 
    * @param \Symfony\Component\Console\Input\InputInterface $input 
    * @param \Symfony\Component\Console\Output\OutputInterface $output 
    */ 
    protected function execute(InputInterface $input, OutputInterface $output) 
    { 
     $output = system('php -f app/console doctrine:schema:update --force'); 
     // do your custom things here. 
     echo $output; 
    } 
} 

执行你的命令

php -f app/console mycoolbundle:schema:update 

增加参数

你可以用下面的方法添加参数的命令:

addArgument('var_name', InputArgument::OPTIONAL, 'Help message...') 
// InputArgument::REQUIRED is also available here 

注意

这样做,这样可以让你添加的功能很多你的控制台命令,再加上你将有如果您选择需要,可直接访问您的模型和实体。

相关问题