使用Symfony 2创建命令

时间:2016-08-09 13:02:57

标签: symfony

我正在尝试寻找解决方案来创建一个动态插入数据库以获取数据的命令 我还没有找到任何解决方案 你可以通过网站帮助我吗?
我已经通过查阅Symfony的官方文档创建了一个命令 但我的prblème是如何创建一个命令来提供使用DoctrineFixturesBundle的数据库

<?php
namespace  Test\FrontBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Helper\DialogHelper;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Form\FormBuilderInterface;
use Doctrine\Common\Annotations\AnnotationException;
class HelloCommand extends ContainerAwareCommand
{
protected function configure()
{
    $this
        ->setName('demo:alimentation')
        ->setDescription('Alimentation base de données ')
        ->setDefinition(array(
           new InputOption('table', '', InputOption::VALUE_REQUIRED, 'Le nom de la table '),
           new InputOption('entity', '', InputOption::VALUE_REQUIRED, 'entity concerned')
        ))

      ;
}

protected function interact(InputInterface $input, OutputInterface $output)
{
    parent::interact($input, $output);
    //$dialog = $this->getDialogHelper ();
    $helper = $this->getHelper('question');
    $question = new ConfirmationQuestion('Continue with this action?', false);

    if (!$helper->ask($input, $output, $question)) {
        return;
    }
    $output->writeln(array(
        '',
        '      Bienvenue    ',
        '',
        'Cet outil va vous permettre de insérer dans la base de donnée dynamiquement ',
        '',
    ));

    $dialog = $this->getHelperSet()->get('dialog');
    $entity = $dialog->ask($output, 'What is the name of the entity?');
    $input->getOption('entity');
    $table = $dialog->ask($output, 'What is the name of the table?');
    $input->getOption('table');
    $input->setOption('entity', $entity);
    $input->setOption('table', $table);

}

protected function getDialogHelper()
{
    $dialog = $this->getHelperSet()->get('dialog');
    if (!$dialog || get_class($dialog) !== 'Sensio\Bundle\GeneratorBundle\Command\Helper\DialogHelper') {
        $this->getHelperSet()->set($dialog = new DialogHelper());
    }

    return $dialog;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
    $dialog = $this->getDialogHelper();
    if ($input->isInteractive()) {
        $question = new ConfirmationQuestion('Do you confirm generation?', true);
        if (!$dialog->askConfirmation($output, $question->getQuestion(), true)) {
            $output->writeln('<error>Command aborted</error>');

            return 1;
        }
    }
    // On recupere les options
    $entity = $input->getOption('entity').'()';
    $table = $input->getOption('table');
    $name = $input->getArgument('name');
    $prenom = $input->getArgument('prenom');





public function createAction(Request $request)
{
$form = $this->createFormBuilder(new $entity)//un passage d'identité
        ->add('name', null, array('label' => 'Nom de l\'album '))
        ->add('type', null, array('label' => 'Type de l\'album'))
        ->add('artist', null, array('label' => 'Artist'))
        ->add('duration', null, array('label' => 'Duration de l\'album'))
        ->add('released', 'date', array('label' => 'Date de l\'album'))
        ->add('submit', 'submit')// les add pour la personalisation
        ->getForm();//pour récupérer le formulaire
    //c'est la logique :il faut le déplacer

    $form->handleRequest($request);
    if ($request->isMethod('post') && $form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($form->getData());
        $em->flush();
        return $output->$this->render('TestFrontBundle:Sheet:create.html.twig', array('form' => $form->getForm()->createView()));

    }
}


}

1 个答案:

答案 0 :(得分:0)

该命令只是替换你的控制器,你仍然可以使用其中的服务

class YourCommand extends ContainerAwareCommand
{

    protected function configure()
    {
        $this->setName( 'your:command' );
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $container = $this->getContainer();
        $service = $container->get( 'your.service' );
        $service->insertYourDataDynamicaly($paramsYouNeed)