Symfony 2.8:从命令调用 $em->flush()

Symfony 2.8: Calling $em->flush() from command

我尝试将更新后的对象刷新到数据库,但由于某种原因我收到此错误:

Attempted to call an undefined method named "flush" of class "Doctrine\Bundle\DoctrineBundle\Registry".

这是我的命令的样子

<?php
// src/AppBundle/Command/CheckHoseCondition.php
namespace AppBundle\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Console\Helper\ProgressBar;
use \DateTime;

class CheckHoseConditionCommand extends ContainerAwareCommand
{

    protected function configure()
    {
         // ...
    }


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

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

        // fetch all userHoses objects
        $userHoses = $em->getRepository('AppBundle:UserHose')->findAll();

        // check dates
        $today = new DateTime(); // vandaag
        $almostExpiredDate = new DateTime('+3 months');

        foreach($userHoses as $hose)
        {

            // change condition to 2
            if ( ($today > $hose->getExpirationDate()) && ($hose->getExpirationDate() != NULL) )
            {

                $hose->setHosecondition(2);

            }

            // change condition to 1
            if ( ($today <= $hose->getExpirationDate()) && ($almostExpiredDate >= $hose->getExpirationDate()) && ($hose->getExpirationDate() != NULL) )
            {

                $hose->setHoseCondition(1);

            }

        }

        // flush the updated objects
        $em->flush();

    }
}

提前致谢。

您忘记拨打getManager()

像这样:

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