JohnM2 JohnM2 - 4 months ago 33
MySQL Question

How to truncate a table using Doctrine?

I want to empty a table in my MySQL database. How can I do that with Doctrine?

Answer

Truncating a table with Doctrine is as "simple" as:

$connection = $entityManager->getConnection();
$platform   = $connection->getDatabasePlatform();

$connection->executeUpdate($platform->getTruncateTableSQL('my_table', true /* whether to cascade */));

But you have to know that MySQL will not be able to truncate any table once it has a foreign key constraint.