2014-10-27 153 views
1

我將自定義選項添加到doctrine:fixtures:load命令。現在我想知道如何在定製燈具類中得到這個命令選項:如何在命令類之外獲得命令參數?

class LoadUserData implements FixtureInterface, ContainerAwareInterface { 

    private $container; 
    /** 
    * {@inheritDoc} 
    */ 
    public function load(ObjectManager $manager) { 

    } 

    public function setContainer(ContainerInterface $container = null) { 
    $this->container = $container; 
    } 
} 

有什麼建議嗎?

回答

0

所以,如果你擴展了Doctrine\Bundle\FixturesBundle\Command\LoadDataFixturesDoctrineCommand並設法添加一個額外的參數,那麼你可以設置該值作爲容器參數傳遞。

<?php 

namespace Your\Bundle\Command; 

use Symfony\Component\Console\Input\InputInterface; 
use Symfony\Component\Console\Output\OutputInterface; 
use Doctrine\Bundle\FixturesBundle\Command\LoadDataFixturesDoctrineCommand; 

class LoadDataFixturesCommand extends LoadDataFixturesDoctrineCommand 
{ 
    /** 
    * {@inheritDoc} 
    */ 
    protected function configure() 
    { 
     parent::configure(); 

     $this->addOption('custom-option', null, InputOption::VALUE_OPTIONAL, 'Your Custom Option'); 
    } 

    /** 
    * {@inheritDoc} 
    */ 
    protected function execute(InputInterface $input, OutputInterface $output) 
    { 
     $this->getContainer()->setParameter('custom-option', $input->getOption('custom-option')); 

     parent::execute($input, $output); 
    } 
} 

然後在您的燈具類中獲取該容器參數。

class LoadUserData implements FixtureInterface, ContainerAwareInterface 
{ 
    /** 
    * If you have PHP 5.4 or greater, you can use this trait to implement ContainerAwareInterface 
    * 
    * @link https://github.com/symfony/symfony/blob/master/src/Symfony/Component/DependencyInjection/ContainerAwareTrait.php 
    */ 
    use \Symfony\Component\DependencyInjection\ContainerAwareTrait; 

    /** 
    * {@inheritDoc} 
    */ 
    public function load(ObjectManager $manager) 
    { 
     $customOption = $this->container->getParameter('custom-option'); 

     ///.... 
    } 
} 
+1

這將是實際夾具中的' - > getParameter('fixture-option')'。 – qooplmao 2014-10-28 13:53:30

+0

@Qoop哎呀,謝謝你指出。編輯。 :) – 2014-10-28 15:44:57

0

執行功能不是叫我,我不得不重新命名命令調用:

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

    $this->setName('app:fixtures:load') 
     ->addOption('custom-option', null, InputOption::VALUE_OPTIONAL, 'Your Custom Option'); 
} 
在這條路上

命令

php app/console app:fixtures:load 

會打電話給您的個人執行功能。