我创建了一个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");
}
}
我搜索但没有结果......
感谢您的帮助。
答案 0 :(得分:0)
在BatchController
:
public function checkBookingsAction($simul)
{
$em = $this->container->get('doctrine')->getManager();
// ...
}
当您将控制器用作服务时,您将无法使用$this->getDoctrine()
。