如何使用queryBuilder从数据库中删除?

时间:2017-11-03 09:16:57

标签: php sql database symfony doctrine-orm

我想删除一些数据并返回删除的数量。 这是我的代码:

public function deleteMyData() : ?int
{
    $qb = $this->connection->createQueryBuilder()
        ->delete('myTable')
        ->where('pays ="us"')
    ;
    return $qb->execute()->rowCount();
}

我已经测试了其他类似的东西:

->delete()
->from('myTable')
->where('pays ="fr"')

当我运行我的代码时,我遇到了这个错误:

  

[Symfony的\元器件\调试\异常\ FatalThrowableError]

     

在整数

上调用成员函数rowCount()

我转储$qb->execute()->rowCount()并返回" 0"。

感谢您的帮助!

PS:我认为问题不在于查询,因为:

  

错误执行' DELETE FROM theQueryTest时发生异常WHERE pays =" us"':

SQL很棒

PS 2:我无法使用->getQuery()

enter image description here

2 个答案:

答案 0 :(得分:0)

此代码返回一个整数

df %>% 
   group_by(A) %>% 
   nest(B) %>% 
   mutate(newcol = map2(data, n_rows, ~ .x %>% slice(seq(.y)))) %>%
   select(-data) %>%
   unnest

如果您想计算受影响的行,请尝试以下操作:

return $qb->execute();

答案 1 :(得分:0)

我找到了解决方案

就我而言,我必须使用 document.addEventListener('deviceready', () => { console.log('DEVICE READY FIRED AFTER', (Date.now() - before), 'ms'); cordova.plugins.pdf.htmlToPDF({ data: "<html> <h1> Hello World </h1> </html>", documentSize: "A4", landscape: "portrait", type: "share" }, (sucess) => console.log('sucess: ', sucess), (error) => console.log('error:', error)); }); 但我不必使用$this->connection

我阅读了doc,我在Connection.php中找到了这个( - &gt; delete(...)):

  
      
  • @return integer受影响的行数。
  •   

所以我必须直接使用$qb->execute()

$this->connection->delete(...)