命令文件中的空值

问题描述 投票:0回答:2

我实际上是在一个symfony项目中工作,在该项目中,我必须将所有与出租车有关的信息放入数据库,并将其写入CSV文件。

为此,我创建了一个CiblageCommand文件:

<?php

namespace AppBundle\Command;

use AppBundle\Entity\Taxi;
use AppBundle\Entity\StatutTaxi;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;

class CiblageCommand extends ContainerAwareCommand
{
private $em;

public function __construct(EntityManagerInterface $em)
{
    parent::__construct();
    $this->em = $em;
}

protected function configure()
{
    $this
        //the short description shown while running "php bin/console list"
        ->setDescription('cible les taxis')
        //commande
        ->setname('ciblage')//php bin/console ciblage
        //the full command descriptionn shown when running the command with
        //the "--help" option
        ->setHelp('Cette commande permet de cibler les taxis valides présents'
            .' dans la BDD et de leur générer un PDF conforme.')
        ;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
    $io = new SymfonyStyle($input, $output);
    $io->title("Lancement de la génération");

    $io->title("\nListage des taxis");
    $this->create();/*
    $io->title("\nCiblage terminé");

    $io->title("\nGénération des courriers");
    $this->generation();
    */
    $io->success("\nGénération terminée");
}

protected function create()
{
    $repository_taxi = $this->em->getRepository('AppBundle:Taxi');
    $listTaxi = $repository_taxi->findAll();

    $doctrine = $this->getContainer()->get('doctrine');
    $em = $doctrine->getEntityManager();

    $handle = fopen('CSVTaxi', 'w+');
    fputcsv($handle, ['IDENTIFIANT', 'RAISON SOCIALE', 'STATUT', 'ETAT'], ';');

    foreach ($listTaxi as $taxi)
    {   
        if($taxi == null){
            $listTaxi[$taxi] = "NULL" ;
        }
        fputcsv(
            $handle,
            [
                $taxi->getNumAm(),
                $taxi->getRaisonSociale(),
                $taxi->getStatutTaxi()->getLibelleStatut(),
                $taxi->getEtatTaxi()->getLibelleEtat()],
            ';'
        );
    }
    fclose($handle);
    echo("nbr taxi : ".count($listTaxi)."\n");
}
}

但是,当我尝试执行“ getStatutTaxi()”时,该值返回为null,尽管之前使用if,我也无法在该文件上写入。

我的终端机给我的错误

[apl4e04@l11750300app2dev 10:23:23] [~/web/html/scot] $ php bin/console ciblage

Lancement de la génération
==========================


Listage des taxis
==================

10:17:16 ERROR     [console] Error thrown while running command "ciblage". Message: "Call to a 
member function getLibelleStatut() on null" ["exception" => Error { …},"command" => 
"ciblage","message" => "Call to a member function getLibelleStatut() on null"]
PHP Fatal error:  Uncaught Symfony\Component\Debug\Exception\FatalThrowableError: Call to a member 
function getLibelleStatut() on null in 
/app/apl4e04/web/html/scot/src/AppBundle/Command/CiblageCommand.php:74
Stack trace:
#0 /app/apl4e04/web/html/scot/src/AppBundle/Command/CiblageCommand.php(44): 
AppBundle\Command\CiblageCommand->create()#1/app/apl4e04/web/html/scot/vendor/
symfony/symfony/src/Symfony/Component/Console/Command/Command.php(255): 
AppBundle\Command\CiblageCommand->execute(Object(Symfony\Component\Console\Input\ArgvInput), 
Object(Symfony\Component\Console\Output\ConsoleOutput))
#2 /app/apl4e04/web/html/scot/vendor/symfony/symfony/src/Symfony/
Component/Console/Application.php(987): Symfony\Component\Console\Command\Command- 
>run(Object(Symfony\Component\Console\Input\ArgvInput), 
Object(Symfony\Component\Console\Output\ConsoleOutput))
#3/app/apl4e04/web/html/scot/vendor/symfony/symfony/
src/Symfony/Bundle/FrameworkBundle/Console/Application 
 .php(86): Symfony\Component\Console\Application->doRunCommand(Object(AppBundl in 
/app/apl4e04/web/html/scot/src/AppBundle/Command/CiblageCommand.php on line 74

我该如何解决并使其正常工作?

谢谢您的帮助。诚挚地,

symfony null doctrine repository getter
2个回答
0
投票

我们可以通过在添加到csv文件之前检查值实例来解决此问题。

$statusLibelleStatut = $taxi->getStatutTaxi() instance of StatutTaxi ? $taxi->getStatutTaxi()->getLibelleStatut() : '';
$etatLibelleEtat = $taxi->getEtatTaxi() instance of LibelleEtat ? $taxi->getEtatTaxi()->getLibelleEtat() : '';

fputcsv(
            $handle,
            [
                $taxi->getNumAm(),
                $taxi->getRaisonSociale(),
                $statusLibelleStatut,
                $etatLibelleEtat],
            ';'
        );

0
投票

更新:我只是在解决问题的'getLibelleStatut()'处添加一个三元函数。

protected function create()
{
    $repository_taxi = $this->em->getRepository('AppBundle:Taxi');
    $listTaxi = $repository_taxi->findAll();

   //$doctrine = $this->getContainer()->get('doctrine');
    //$em = $doctrine->getEntityManager();

    $handle = fopen('CSVTaxi.csv', 'w+');
    fputcsv($handle, [' IDENTIFIANT', 'RAISON SOCIALE', 'STATUT', 'ETAT'], ';');

    foreach ($listTaxi as $taxi)
    {
        fputcsv(
            $handle,
            [
                $taxi->getNumAm(),
                $taxi->getRaisonSociale(),
                ($taxi->getStatutTaxi()==null)?"":$taxi->getStatutTaxi()->getLibelleStatut(),
                $taxi->getEtatTaxi()->getLibelleEtat()],
            ';'
        );
    }
    fclose($handle);
    echo("nbr taxi : ".count($listTaxi)."\n");
}
© www.soinside.com 2019 - 2024. All rights reserved.