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

Artamiel
  • 3,652
  • 2
  • 19
  • 24

2 Answers2

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    );
}
M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118
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;
}
Rooneyl
  • 7,802
  • 6
  • 52
  • 81