在Symfony2上扩展Crud Generator

时间:2014-06-09 22:48:40

标签: php symfony bundle

我知道如何扩展或覆盖crud生成器视图,创建像documendation这样的文件:

http://symfony.com/doc/current/bundles/SensioGeneratorBundle/index.html

但是我想重用那些视图,我不想把视图放在同一个Bundle中,我有实体和crud控制器,表单和视图将被创建,所以我试图生成我自己的命令并扩展GenerateDoctrineCrudCommand:< / p>

use Sensio\Bundle\GeneratorBundle\Command\GenerateDoctrineCrudCommand;
use Sensio\Bundle\GeneratorBundle\Generator\DoctrineCrudGenerator;

class BackendCrudCommand extends GenerateDoctrineCrudCommand {

  protected $generator;

  protected function configure()
  {
    parent::configure();

    $this->setName('d2armory:generate:crud');
    $this->setDescription('Automatic crud generator based on templates!');
  }

  protected function getGenerator()
  {
    if (null === $this->generator) {
        $this->generator = new DoctrineCrudGenerator($this->getContainer()->get('filesystem'), __DIR__.'/../Resources/skeleton/crud');
    }

    return $this->generator;
    //$generator = new DoctrineCrudGenerator($this->getContainer()->get('filesystem'), __DIR__.'/../Resources/skeleton/crud');
    //$this->setGenerator($generator);
    //return parent::getGenerator();
  }

}

我尝试了在getGenerator函数上找到的2段代码,我得到了:

 [Symfony\Component\Debug\Exception\ContextErrorException]                                                                                                                                                                
 Runtime Notice: Declaration of XXX\BackendBundle\Command\BackendCrudCommand::getGenerator() should be compatible with Sensio\Bundle\GeneratorBundle\Command\GeneratorCommand::getGenerator(Symfony\Component\HttpKernel\Bundle\BundleInterface $bundle = NULL) in /home.../XXX/BackendBundle/Command/BackendCrudCommand.php line 38

当然我有像SensioGenerator Bundle一样的文件夹树作为骨架,但只有一些文件要覆盖,我希望它从我的包中只需要我声明的文件,它必须采取defualt文件,如果不存在我捆绑

我不知道这是否是正确的方法,我想念的是什么。

谢谢你的建议!

1 个答案:

答案 0 :(得分:2)

您错过了使用参数声明函数: protected function getGenerator(BundleInterface $ bundle = null) 并使用Symfony \ Component \ HttpKernel \ Bundle \ BundleInterface;