0
votes

I create a symfony command that executes a function in a controller but when I run the command I get this error message:

[Symfony \ Component \ Debug \ Exception \ FatalThrowableError] Fatal error: Call to a member function has () on null

My Services.yml

services:
app.site_batch:
    class: App\SiteBundle\Controller\BatchController
    arguments: 
        entityManager: "@doctrine.orm.entity_manager"
        container: "@service_container"

My Controller

        <?php

    namespace App\SiteBundle\Controller;

    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
    use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\HttpFoundation\Response;


    use ChrisCom\BDDBundle\Entity\Paiement;
    use ChrisCom\BDDBundle\Entity\BBooking;


    /**
     * @Route("/batch")
     */
    class BatchController extends Controller
    {
          private $log_file;
            private $output;

            private function openLog($filename) { $this->log_file = fopen($this->get('kernel')->getLogDir() . '/' . $filename, 'a'); }
          private function log($line, $message)
          {
              if ($this->log_file)
              {
                  $msg = sprintf("[%s %s:%03d] %s\n", date('d/m/Y H:i:s'), basename(__FILE__), $line, $message);
                  fputs($this->log_file, $msg);
                    $this->output .= $msg;
              }
          }
          private function closeLog() { if ($this->log_file) fclose($this->log_file); }




            /**
             * @Route("/check_bookings", name="batch_check_bookings")
            */
            public function checkBookingsAction($simul)
            {
                $em = $this->getDoctrine()->getManager();

                $rep_book   = $em->getRepository('ChrisComBDDBundle:BBooking');
                $cur_date   = new \DateTime();
                $format_cmp = 'YmdHis';
                $flush      = false;
....

My Command

    <?php
namespace App\SiteBundle\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 CheckBookingsCommand extends ContainerAwareCommand
{
    protected function configure()
    {
        $this
            ->setName('cron:checkBookings')
            ->setDescription('Vérification des réservation')
            ->addOption(
                'simul',
                null,
                InputOption::VALUE_NONE,
                'Si défini, le programme ne fera pas de COMMIT en base de donnée'
            );
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $output->writeln("Starting the cron");

        // Call the method here
              $version = $this->getContainer()->get('app.bdd')->parameterGet('VERSION');

                $simul = ($input->getOption('simul'));
                if ($simul) $output->writeln("Mode simulation");

                $output->writeln($this->getContainer()->get('app.site_batch')->checkBookingsAction($simul));

        $output->writeln("Cron completed");
    }
}

I searched but without result ...

Thank you for help.

1

1 Answers

0
votes

In your BatchController :

public function checkBookingsAction($simul)
{
    $em = $this->container->get('doctrine')->getManager();
    // ...
}

When you use a controller as a service, you lose the possibility to use $this->getDoctrine().