dseomy1964 2015-09-28 00:41
浏览 100

如何在继承自Command.php的类中实现我的构造函数

Working with symfony2.3.4 and PHP5.6.3

I´m implementing a command of my own, well it actually does basically the same as doctrine:generate:entity only with some customization of mine.

I'm stuck trying to implement the constructor method in my class OwnEntityCommand.php which extends from GenerateDoctrineEntityCommand class which follows a line of upward inheritance until it reaches Command.php.

The constructor in that last class looks like this:

public function __contruct($name = null) {  
    $this->definition = new InputDefinition();  
    $this->ignoreValidationErrors = false;  
    $this->applicationDefinitionMerged = false;  
    $this->applicationDefinitionMergedWithArgs = false;  
    $this->aliases = array();  

    if(null !== $name){  
        $this->setName($name);  
    }  
    $this->configure();  

    if($this->name){  
        throw new \LogicException('The command name cannot be empty.');
    }
}

and the one I need in mine is supposed to look like this:

public function __construct(Filesystem $filesystem, RegistryInterface $registry){  
    $this->filesystem = $filesystem;  
    $this->registry = $registry;  
}

and the only attrs in my class being:

private $filesystem;  
private $registry;

when I try to run the command it throws:

[InvalidArgumentException] Command "own:entity" is not defined.

do not know how to implement my own constructor without conflicting with the parent's(which I think is what's happening)

if need more data or code, just tell me

UPDATE:

protected function configure() {
$this
        ->setName('own:entity')
        ->setAliases(array('generate:own:entity'))
        ->setDescription('Generates a new Doctrine entity inside a bundle')
        ->addOption('entity', null, InputOption::VALUE_OPTIONAL, 'The entity class name to initialize (shortcut notation)', 'UserBundle:TestEntity')
        ->addOption('fields', null, InputOption::VALUE_REQUIRED, 'The fields to create with the new entity')
        ->addOption('format', null, InputOption::VALUE_REQUIRED, 'Use the format for configuration files (php, xml, yml, or annotation)', 'annotation')
        ->addOption('with-repository', null, InputOption::VALUE_NONE, 'Whether to generate the entity repository or not');

}

  • 写回答

1条回答 默认 最新

  • douna2014 2015-09-28 05:57
    关注

    I think you have to override the configure function in order to change the name command like this:

    class OwnEntityCommand extends GenerateDoctrineEntityCommand
    {
        protected function configure()
        {
            $this
                ->setName('own:entity')
                ->setAliases(array('entity:own'))
                ->setDescription('Generates a new Doctrine entity inside a bundle')
                ->addOption('entity', null, InputOption::VALUE_REQUIRED, 'The entity class name to initialize (shortcut notation)')
                ->addOption('fields', null, InputOption::VALUE_REQUIRED, 'The fields to create with the new entity')
                ->addOption('format', null, InputOption::VALUE_REQUIRED, 'Use the format for configuration files (php, xml, yml, or annotation)', 'annotation')
                ->setHelp(<<<EOT
    The <info>%command.name%</info> task generates a new Doctrine
    entity inside a bundle:
    <info>php %command.full_name% --entity=AcmeBlogBundle:Blog/Post</info>
    The above command would initialize a new entity in the following entity
    namespace <info>Acme\BlogBundle\Entity\Blog\Post</info>.
    You can also optionally specify the fields you want to generate in the new
    entity:
    <info>php %command.full_name% entity:own --entity=AcmeBlogBundle:Blog/Post --fields="title:string(255) body:text"</info>
    By default, the command uses annotations for the mapping information; change it
    with <comment>--format</comment>:
    <info>php %command.full_name% --entity=AcmeBlogBundle:Blog/Post --format=yml</info>
    To deactivate the interaction mode, simply use the <comment>--no-interaction</comment> option
    without forgetting to pass all needed options:
    <info>php %command.full_name% --entity=AcmeBlogBundle:Blog/Post --format=annotation --fields="title:string(255) body:text" --no-interaction</info>
    This also has support for passing field specific attributes:
    <info>php %command.full_name% --entity=AcmeBlogBundle:Blog/Post --format=annotation --fields="title:string(length=255 nullable=true unique=true) body:text ranking:decimal(precision:10 scale:0)" --no-interaction</info>
    EOT
            );
        }
    

    Source: https://github.com/sensiolabs/SensioGeneratorBundle/blob/master/Command/GenerateDoctrineEntityCommand.php

    评论

报告相同问题?

悬赏问题

  • ¥15 fesafe材料库问题
  • ¥20 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统