所以,如果你擴展了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');
///....
}
}
這將是實際夾具中的' - > getParameter('fixture-option')'。 – qooplmao 2014-10-28 13:53:30
@Qoop哎呀,謝謝你指出。編輯。 :) – 2014-10-28 15:44:57