错误:在非对象上调用成员函数format()

时间:2015-11-20 17:35:30

标签: symfony orm doctrine-orm

我创建了一个symfony控制台命令,可以自动从参与者条目中选择一个获胜者,每天通过cron运行。

执行功能如下所示

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

        $this->today = new \DateTime();
        $this->today = $this->today->format('Y-m-d');

        $this->container = $this->getApplication()->getKernel()->getContainer();
        $participant = $this->container->get('smack_calendar.data_collection_repository')
            ->findOneBy(array('entryDate' => $this->today ));

}

执行时

 app/console foo:pickwinner --test daily

我收到此错误

  

[Symfony \ Component \ Debug \ Exception \ FatalErrorException]错误:调用   到非对象的成员函数format()

在这一行

->findOneBy(array('entryDate' => $this->today ));

我不是一个大的symfony用户,从来没有触及过教条,所以请原谅我,如果这是一个愚蠢的问题:D

1 个答案:

答案 0 :(得分:1)

使用doctrine处理日期时,始终使用DateTime对象,因此:

$this->container = $this->getApplication()->getKernel()->getContainer();
$participant = $this->container->get('smack_calendar.data_collection_repository')
        ->findOneBy(array('entryDate' => new \DateTime('now') ));

PS在苍蝇中创建对象,尤其是可变的。