2015-06-12 21 views
1

在我的Symfony2項目我在ProductRepository.php以下查詢:Symfony2的學說:ContextErrorException:開捕致命錯誤:類的DateTime的對象無法轉換爲字符串

$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; 
    } 

錯誤:

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

在其他職位,我已經讀到一些關於需要整合的toString方法,但我不知道在哪裏實現它...

+0

您能否在ProductEntity中顯示已發佈字段的映射? –

回答

0

嘗試使用參數在QueryBuilder

$query = $this->createQueryBuilder('p') 
     ->update('MyBundle\Products', 'p') 
     ->set('p.published', ':dateNow') 
     ->setParameter('dateNow', $dateNow) 
     ->getQuery(); 
相關問題