doctrine DateTime could not be converted to string

2019-08-08 19:32发布

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();
    }   

How can I fix it?

1条回答
萌系小妹纸
2楼-- · 2019-08-08 19:41

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

new \DateTime->format('Y-m-d H:i:s') should work.

查看更多
登录 后发表回答