2016-08-09 13 views
-1

ich versuche nach Lösungen zu suchen, einen Befehl zu erstellen, um Daten in die Datenbank einzufügen dynamisch Ich habe gefunden, keine Lösungen können Sie mir helfen, indem sie Websites
ich einen Befehl bereits erstellen, indem Beratung die offizielle Dokumentation von Symfony aber mein prblème ist, wie ein Befehl zum erstellen einer Datenbank, die DoctrineFixturesBundleerstellen Befehl mit Symfony 2

<?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())); 

    } 
} 


} 
+0

Sie müssen es nach der Symfony-Dokumentation entwickeln, und wenn Sie ein Problem finden, geben Sie hier den verwendeten Code ein. –

+0

Ich bereits einen Befehl erstellen, indem Sie die offizielle Dokumentation von Symfony konsultieren, aber mein prblème ist, wie man einen Befehl erstellt, um eine Datenbank ohne DotrineFixturesBundle – Khouloud

+0

zu liefern Vielleicht aktualisieren Sie Ihre Frage mit etwas Code. Zum Einfügen ziehen Sie einfach den Entity Manager aus dem Container, neu Ihre Entität und dann persist/flush, – Cerad

Antwort

0

der Befehl nur ersetzt Ihren Controller verwenden zu liefern, können Sie immer noch in sie verwenden, um Dienste

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) 
+0

@ Tomáš Votruba Ich möchte in die Datenbank mit einem Befehl einfügen – Khouloud

+0

@Khouloud - Erwarten Sie den Befehl, um irgendwie ein Formular zu öffnen? Denn das wird nicht passieren. – Cerad