How to truncate a table using Doctrine 2?

后端 未结 5 1179
走了就别回头了
走了就别回头了 2020-12-13 18:00

I assume that I need to build a native query to truncate a table using Doctine2.

$emptyRsm = new \\Doctrine\\ORM\\Query\\ResultSetMapping();
$sql = \'TRUNCAT         


        
5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-13 18:59

    This is how you can delete all entites from a entity reposiotry in symfony using doctrine (not ignoring foreign key checks). The functions returns the count of the deleted entites.

    /**
     * @return int
     */
    public function deleteAll(): int
    {
        $qb = $this->createQueryBuilder('t');
    
        $qb->delete();
    
        return $qb->getQuery()->getSingleScalarResult() ?? 0;
    }
    

提交回复
热议问题