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?
As mentioned in the comments, you need to convert the
DateTime
to a string. You need to use DateTime::format('Y-m-d H:i:s') function, something likenew \DateTime->format('Y-m-d H:i:s')
should work.