教义DateTime不能转换为字符串 [英] doctrine DateTime could not be converted to string

查看:36
本文介绍了教义DateTime不能转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用symfony 2.3和教义2.2.我创建了一个控制台命令,以便在数据库中插入一些数据.当我尝试使用当前日期更新时间列时,我收到此错误

I am using symfony 2.3 and doctrine 2.2. I created a console command in order to insert some data in the database. When I try to update the time column with the current date, I recieve this error

    Catchable fatal error: Object of class DateTime could not be converted to string
 in D:\xampp\htdocs\biginfo\vendor\doctrine\orm\lib\Doctrine\ORM\Query\Expr\Comp
arison.php on line 98  

Command.php

protected function configure() {
        $this
                ->setName('biginfo:invoice')
                ->setDescription('Générer les factures de chaque commercial chaque début du mois')
        ;
    }

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

        $users = $this->findByRole('ROLE_COMMERCIAL');
        // update invoice
        $this->updateInvoice($users);
        $this->updateStatus();
    }

    public function updateStatus() {
        $em = $this->getContainer()->get('doctrine.orm.entity_manager');
        $queryBuilder = $em->createQueryBuilder();
        $queryBuilder
                ->update('Biginfo\UserBundle\Entity\User', 'u')
                ->set('u.nbrBusiness', 0)
                ->set('u.time', new \DateTime(date('Y-m-d')));
        return $queryBuilder->getQuery();
    }   

我该如何解决?

推荐答案

如注释中所述,您需要将 DateTime 转换为字符串.您需要使用 DateTime :: format('Ymd H:i:s')功能,类似

As mentioned in the comments, you need to convert the DateTimeto a string. You need to use DateTime::format('Y-m-d H:i:s') function, something like

新的\ DateTime-> format('Y-m-d H:i:s')应该可以.

这篇关于教义DateTime不能转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆