【问题标题】:Error Symfony when command execute命令执行时出现错误 Symfony
【发布时间】:2023-12-17 03:42:02
【问题描述】:

我创建了一个在控制器中执行功能的 symfony 命令,但是当我运行该命令时,我收到以下错误消息:

[Symfony\Component\Debug\Exception\FatalThrowableError] 致命错误:对成员函数的调用 has () on null

我的服务.yml

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

我的控制器

        <?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;
....

我的命令

    <?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");
    }
}

我搜索了但没有结果...

感谢您的帮助。

【问题讨论】:

    标签: symfony


    【解决方案1】:

    在你的BatchController

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

    当您将控制器用作服务时,您将失去使用 $this-&gt;getDoctrine() 的可能性。

    【讨论】:

      最近更新 更多