我正在使用Symfony2,我必须使用Doctrine QueryBuilder(http://docs.doctrine-project.org/en/latest/reference/query-builder.html)该文档没有删除或更新语句的示例。
我的实体是:
object(stdClass)[417]
public '__CLASS__' => string 'Les\DataBundle\Entity\News' (length=26)
public 'id' => int 1
public 'restoId' => int 1
public 'category' => string 'dessert' (length=7)
public 'text' => string 'jlkdjsalkdj sa' (length=14)
public 'dateCreated' => string 'DateTime' (length=8)
public 'dateModified' => null
我正在尝试删除数据库中的一行,但我一直收到错误:
[Semantical Error] line 0, col 7 near 'News WHERE n.id': Error: Class 'News' is not defined.
我的查询是:
$newsID =2 ;
$qd = $repository->createQueryBuilder('n');
$qd->delete('n')
->where('n.id = :id')
->setParameter('id',$newsID);
$query = $qd->getQuery();
$result = $query->getResult();
更新语句是否具有相同的结构?
答案 0 :(得分:0)
对于delete语句与select不同,您不必指定删除的列名而不是select。
更正代码
$qd = $repository->createQueryBuilder('n');
$qd->delete()
->where('n.id = :id')
->setParameter('id',$newsID);
$query = $qd->getQuery();
$result = $query->getResult();
另一种格式
$query = $this->getDoctrine()->getManager()->createQuery('delete FROM LesDataBundle:News n where n.id = '.$newsID);
$result = $query->getResult();
对于更新声明,它是:
$qd = $repository->createQueryBuilder('n');
$qd->delete()
$qd->update()
->set('n.text' , ':text')
->where('n.id = :id')
->setParameters( array('id'=> $newsID, 'text' => 'Hello World') );
$query = $qd->getQuery();
$result = $query->getResult();