您也可以在實體存儲庫中的以下內容:
return $this
->createQueryBuilder('f')
->update($this->getEntityName(), 'f')
->set('f.downloadCounter', $file->getDownloadCounter() + 1)
->where('f.id = :id')->setParameter('id', $file->getId())
->getQuery()
->execute();
或者使用DQL:
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery(
'UPDATE YourBundle:File f
SET f.downloadCounter = :downloadCounter'
)->setParameter('downloadCounter', $file->getDownloadCounter() + 1);
或者通過簡化的DQL:
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery(
'UPDATE YourBundle:File f
SET f.downloadCounter = f.downloadCounter + 1'
);
這些解決方案的缺點:如果您的實體已經加載,它將具有先前的計數,而不是遞增的計數。
你做的方式非常好,但更好的方法是將增量方法添加到實體。
謝謝你,你的DQL幫助了我。而不是使用':downloadCounter'我沒有使用'f.downloadCounter + 1'並添加了一個文件的位置 –
我已經添加它作爲示例 - 很高興我的回答有幫助 –
這不是一個答案,問題是如何使增量查詢,而不是如何用+1代替summ值 –