Beware of Truncating Tables
Beware of truncating tables in any RDBMS, especially if you want to use explicit transactions for commit/rollback functionality. Please read the 'My recommendation' of this answer.
DDL statements perform an implicit-commit
Truncate table statements are data definition language (DDL) statements, and as such truncate table statements trigger an implicit COMMIT
to the database upon their execution. If you perform a TABLE TRUNCATE
then the database is implicitly committed to--even if the TABLE TRUNCATE
is within a START TRANSACTION
statement--your table will be truncated and a ROLLBACK
will not restore it.
Because truncate table statements perform implicit commits, Maxence's answer does not perform as expected (but it's not wrong, because the question was "how to truncate a table"). His answer does not perform as expected because it truncates the table in a try
block, and assumes that the table can be restored in the catch
block, if something goes wrong. This is an incorrect assumption.
Other user's comments & experiences in this thread
ChrisAelbrecht was unable to get Maxence's solution to work properly because you cannot rollback a truncate table statement, even if the truncate table statement is in an explicit transaction.
user2130519, unfortunately, was downvoted (-1 until I upvoted) for providing the correct answer--although he did so without justifying his answer, which is like doing math without showing your work.
My recommendation DELETE FROM
My recommendation is to use DELETE FROM
. In most cases, it will perform as the developer expects. But, DELETE FROM
does not come without drawbacks either--you must explicitly reset the auto increment value for the table. To reset the auto increment value for the table, you must use another DDL statement--ALTER TABLE
--and, again, don't use ALTER TABLE
in your try
block. It won't work as expected.
If you want tips on when you should use DELETE FROM
vs TRUNCATE
see Pros & Cons of TRUNCATE vs DELETE FROM.
If you really must, here's how to truncate
Now, with all that said. If you really want to truncate a table using Doctrine2, use this: (Below is the portion of Maxence's answer that correctly truncates a table)
$cmd = $em->getClassMetadata($className);
$connection = $em->getConnection();
$dbPlatform = $connection->getDatabasePlatform();
$connection->query('SET FOREIGN_KEY_CHECKS=0');
$q = $dbPlatform->getTruncateTableSql($cmd->getTableName());
$connection->executeUpdate($q);
$connection->query('SET FOREIGN_KEY_CHECKS=1');
How to delete a table with rollback/commit functionalty.
But, if you want rollback/commit functionality, you must use DELETE FROM
: (Below is a modified version of Maxence's answer.)
$cmd = $em->getClassMetadata($className);
$connection = $em->getConnection();
$connection->beginTransaction();
try {
$connection->query('SET FOREIGN_KEY_CHECKS=0');
$connection->query('DELETE FROM '.$cmd->getTableName());
// Beware of ALTER TABLE here--it's another DDL statement and will cause
// an implicit commit.
$connection->query('SET FOREIGN_KEY_CHECKS=1');
$connection->commit();
} catch (Exception $e) {
$connection->rollback();
}
If you need to reset the auto increment value, remember to call ALTER TABLE <tableName> AUTO_INCREMENT = 1
.