用commande执行php文件

时间:2017-12-28 15:19:52

标签: php symfony

我将symfony 2.8用于我的项目。这是我将用commande执行的php文件的代码:

 use Video\IntelligenceBundle\Entity\Entry;
 use Video\IntelligenceBundle\Entity\Visits;
 use Doctrine\ORM\EntityManager;
 use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Input\InputOption;
 use Symfony\Component\Console\Output\OutputInterface;
 use Symfony\Component\Routing\Generator\UrlGeneratorInterface;


 class ConvertVisitsIncomeCommand extends ContainerAwareCommand
 {
public function configure()
{
    $this->setName('app:convert-visits')
        ->setDescription('Convertir les entrée de la table visits a la 
table entry')
    ;
}

protected function execute(InputInterface $input, OutputInterface 
$output)
{

    /** @var EntityManager $em */

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

   // $em = $this->getDoctrine()->getManager();

   $output->writeln("sss"); 
    /** @var Video\IntelligenceBundle\Entity\Visits $visit */
    $visitsRepository = $em->getRepository(Visits::class);

    $visits = $visitsRepository->findAll();
    $groupedVisitsByPerson = [];

    /** @var Video\IntelligenceBundle\Entity\Visits $visit */
    foreach ($visits as $visit)
        $groupedVisitsByPerson[$visit->getIdPerson()][] = $visit;

    $visitDate = null;
    $leaveDate = null;
    $tmp = [];
    foreach ($groupedVisitsByPerson as $personId => $visits) {
        try {

            $i      = 0;
            $length = count($visits);
            $visitDate = null;
            $leaveDate = null;
            /** @var \AppBundle\Entity\Visits $visit */
            foreach ($visits as $visit) {
                $i++;
                if($visit->getVisitDate()->getTimestamp() <= 0) {
                    $em->remove($visit);
                    continue;
                }
                if(is_null($visitDate)) {
                    $visitDate = $visit->getVisitDate();
                    $tmp[] = $visit;
                    if($length == $i) {
                        $entry = new Entry();
                        $entry
                            ->setVisitDate($visitDate)
                            ->setLeaveDate(null)
                            ->setIdPerson($personId)
                            ->setCamera($visit->getCamera());
                        $em->persist($entry);
                        $output->writeln("<success>".$entry->getIdPerson()." added</success>");
                    }
                } else {
                    $diff = $visit->getVisitDate()->diff($visitDate);
                    $seconds = $diff->s + $diff->i * 60 + $diff->h * 3600;
                    if($length == $i) {
                        $tmp[]     = $visit;
                        $leaveDate = $visit->getVisitDate();
                        if($leaveDate == $visitDate) {
                            $leaveDate = null;
                        }

                        $entry = new Entry();
                        $entry
                            ->setVisitDate($visitDate)
                            ->setLeaveDate($leaveDate)
                            ->setIdPerson($personId)
                            ->setCamera($visit->getCamera());
                        $em->persist($entry);
                        $output->writeln("<success>".$entry->getIdPerson()." added</success>");

                        $visitDate = null;
                        $leaveDate = null;
                    }
                    elseif($seconds > 0 && $seconds <= 60 ) {
                        $leaveDate = $visit->getVisitDate();
                        $tmp[]     = $visit;
                    }
                    elseif($seconds > 60) {
                        $entry = new Entry();
                        $entry
                            ->setVisitDate($visitDate)
                            ->setLeaveDate($leaveDate)
                            ->setIdPerson($personId)
                            ->setCamera($visit->getCamera());
                        $em->persist($entry);

                        $visitDate = $visit->getVisitDate();
                        $leaveDate = null;
                        $tmp[]     = $visit;
                    }
                }
            }

            foreach ($tmp as $visit)
                $em->remove($visit);

            $em->flush();
        } catch (\Exception $e) {
            $output->writeln("<error>".$e->getMessage()." </error>");


        }

    }

 }
   }

当我尝试使用此命令执行此代码时:

sudo php /opt/lampp/htdocs/vi-old/app/console app:convert-visits --env=prod

我有这个问题

  

[学说\ DBAL \异常\ ConnectionException]
  驱动程序中发生异常:SQLSTATE [HY000] [2002]没有这样的文件或   目录

谁能帮我解决这个问题呢。

1 个答案:

答案 0 :(得分:0)

看起来像MySQL本身的问题。确保已配置套接字并且文件存在。运行php -i | grep mysql.sock并查找*.default_socket

您也可以重新安装mysql扩展程序。

另外,如上所述,使用sudo

运行命令是个坏主意