douhuang1852 2015-06-12 08:29
浏览 92
已采纳

Symfony2 Doctrine:ContextErrorException:Catchable Fatal Error:类DateTime的对象无法转换为字符串

In my Symfony2 project I have the following query in my ProductRepository.php:

$dateNow = new \DateTime();
$query = $this->createQueryBuilder('p')
            ->update('MyBundle\Products', 'p')
            ->set('p.published', $dateNow)
            ->getQuery();

$query->execute();

ProductEntity.php:

 /**
     * Set published
     *
     * @param \DateTime $published
     * @return Product
     */
    public function setPublished($published)
    {
        $this->published = $published;

        return $this;
    }

ERROR:

ContextErrorException: Catchable Fatal Error: Object of class DateTime could not be converted to string

In other posts I already read something about toString methods that need to be integrated, but I don't know where to implement it...

  • 写回答

1条回答 默认 最新

  • doulu4976 2015-06-12 08:58
    关注

    Try using parameters in your QueryBuilder:

    $query = $this->createQueryBuilder('p')
            ->update('MyBundle\Products', 'p')
            ->set('p.published', ':dateNow')
            ->setParameter('dateNow', $dateNow)
            ->getQuery();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料