0

As per the documentation on the symfony, i have created the console file in /bin folder

!/usr/bin/env php

date_default_timezone_set('UTC');

set_time_limit(0);

(@include_once DIR . '/../vendor/autoload.php') || @include_once DIR . '/../../../autoload.php';

use AppBundle\Console\Command\FetchCompetitionCommand; use Symfony\Component\Console\Application;

$app = new Application(); $app->add(new FetchCompetitionCommand()); $app->run();

and then the Command file in the Bundle/Console/Command Folder

<?php 

namespace AppBundle\Console\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; 
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption; 
use Symfony\Component\Console\Output\OutputInterface;

class FetchCompetitionCommand extends ContainerAwareCommand {
     protected function configure()
     {
         $this
             ->setName('getCompetition')
             ->setDescription('Get the Competition Name (AFL,NRL)')
             ->addArgument(
                 'name',
                 InputArgument::OPTIONAL,
                 '(Which Competition Data are you looking to fetch?)'
             )
         ;
     }

     protected function execute(InputInterface $input, OutputInterface $output)
     {
         $name = $input->getArgument('name');

         $output->writeln($name );
     } 
} 
?>

What next i need to do for configuring the database and access the data from database

Thanks in Advance

0

2 Answers 2

2

In execute function you can get container and once you have container you can get your repositories through doctrine and do your database stuff

protected function execute(InputInterface $input, OutputInterface $output)
{
    $name = $input->getArgument('name');
    $container = $this->getContainer();
    $DM = $container->get('Doctrine')->getManager();
    $result = $DM->getRepository('NamspaceYourBundle:Entity')->findBy(array());
    /** do your stuff here */

    $output->writeln($name    );
}
Sign up to request clarification or add additional context in comments.

Comments

1

I tend to have a service class that does whatever the functionality required is, then the console command calls this. The advantage to this is that the same functionality can then be called from any place in your system.

protected function execute(InputInterface $input, OutputInterface $output)
{
    $name = $input->getArgument('name');
    $my_service = $this->getContainer()->get('my.service.class');
    $bar = $my_service->foo($name);
    $output->writeln($name    );
} 

Example function from service class;

public function foo($name)
{
    $DM = $container->get('Doctrine')->getManager();
    $result = $DM->getRepository('NamspaceYourBundle:Entity')->findBy(array($name));
    return $result;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.