我尝试将更新的对象刷新到数据库但是为了一些保留,我得到了这个错误:
尝试调用类名为“Doctrine \ Bundle \ DoctrineBundle \ Registry”的名为“flush”的未定义方法。
这是我的命令看起来
<?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();
}
}
提前致谢。
答案 0 :(得分:2)
您忘了拨打getManager()
$em = $this->getContainer()->get('doctrine')->getManager();