我想在Doctrine Create Schema事件上插入一些记录到数据库中(一些用于外键约束的默认数据)。
这个函数通过命令行调用(在Symfony2中):
php app/console doctrine:schema:update --force
运行此命令时,Doctrine2将生成数据库的模式。
是否有办法对此作出反应,甚至将数据插入数据库?
谢谢!
创建一个新的控制台命令将完成您所寻找的。
<<p> 命令代码/strong>美亚国际/包/命令/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
指出这样做可以让你在控制台命令中添加很多功能,而且如果你选择需要它们,你可以直接访问你的模型和实体。
看看DoctrineFixtureBundle !