如何在继承自Command.php的类中实现构造函数
使用symfony2.3.4和PHP5.6.3 我正在实现我自己的一个命令,实际上它与原则:generate:entity基本相同,只是有一些我的定制 我一直在尝试在我的类OwnEntityCommand.php中实现构造函数方法,它从GeneratedActineEntityCommand类扩展而来,该类遵循一行向上继承,直到到达Command.php 最后一个类中的构造函数如下所示:如何在继承自Command.php的类中实现构造函数,php,symfony,inheritance,command,Php,Symfony,Inheritance,Command,使用symfony2.3.4和PHP5.6.3 我正在实现我自己的一个命令,实际上它与原则:generate:entity基本相同,只是有一些我的定制 我一直在尝试在我的类OwnEntityCommand.php中实现构造函数方法,它从GeneratedActineEntityCommand类扩展而来,该类遵循一行向上继承,直到到达Command.php 最后一个类中的构造函数如下所示: public function __contruct($name = null) { $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.');
}
}
public function __construct(Filesystem $filesystem, RegistryInterface $registry){
$this->filesystem = $filesystem;
$this->registry = $registry;
}
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
);
}
我需要的那个应该是这样的:
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.');
}
}
public function __construct(Filesystem $filesystem, RegistryInterface $registry){
$this->filesystem = $filesystem;
$this->registry = $registry;
}
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
);
}
我班上唯一的属性是:
private $filesystem;
private $registry;
当我尝试运行它抛出的命令时:
[无效辩论例外]
未定义命令“own:entity”
不知道如何实现自己的构造函数而不与父构造函数发生冲突(我认为这就是正在发生的事情)
如果需要更多的数据或代码,请告诉我
更新:
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');
}我认为您必须覆盖configure函数,才能像这样更改name命令:
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.');
}
}
public function __construct(Filesystem $filesystem, RegistryInterface $registry){
$this->filesystem = $filesystem;
$this->registry = $registry;
}
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
);
}
class OwnEntityCommand扩展了GeneratedEntityCommand
{
受保护的函数配置()
{
美元这个
->setName('own:entity')
->setAlias(数组('entity:own'))
->setDescription('在包中生成新的条令实体')
->addOption('entity',null,InputOption::VALUE_REQUIRED,'要初始化的实体类名(快捷方式表示法)')
->addOption('fields',null,InputOption::VALUE\u REQUIRED,'The fields to create with The new entity')
->addOption('format',null,InputOption::VALUE_REQUIRED,'使用配置文件(php、xml、yml或注释)的格式','annotation')
->setHelp(我认为您必须重写configure函数才能更改name命令,如下所示:
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.');
}
}
public function __construct(Filesystem $filesystem, RegistryInterface $registry){
$this->filesystem = $filesystem;
$this->registry = $registry;
}
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
);
}
class OwnEntityCommand扩展了GeneratedEntityCommand
{
受保护的函数配置()
{
美元这个
->setName('own:entity')
->setAlias(数组('entity:own'))
->setDescription('在包中生成新的条令实体')
->addOption('entity',null,InputOption::VALUE_REQUIRED,'要初始化的实体类名(快捷方式表示法)')
->addOption('fields',null,InputOption::VALUE\u REQUIRED,'The fields to create with The new entity')
->addOption('format',null,InputOption::VALUE_REQUIRED,'使用配置文件(php、xml、yml或注释)的格式','annotation')
->setHelp(您可以发布完整的命名空间和类名吗?@Matteo namespace CC\UserBundle\Command;类OwnEntityCommand扩展了GeneratedEntityCommand。是吗?您可以发布完整的命名空间和类名吗?@Matteo namespace CC\UserBundle\Command;类OwnEntityCommand扩展了GeneratedEntityCommand。是吗?