Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to truncate a table using Doctrine 2?

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

$emptyRsm = new \Doctrine\ORM\Query\ResultSetMapping(); $sql = 'TRUNCATE TABLE Article'; $query = em()->createNativeQuery($sql, $emptyRsm); $query->execute(); 

This gives the error

SQLSTATE[HY000]: General error 

What do I need to change to my code to make this work?

like image 314
murze Avatar asked Mar 13 '12 15:03

murze


People also ask

How do I truncate a table in PostgreSQL?

The PostgreSQL TRUNCATE TABLE command is used to delete complete data from an existing table. You can also use DROP TABLE command to delete complete table but it would remove complete table structure from the database and you would need to re-create this table once again if you wish to store some data.

Can we rollback truncate in Postgres?

It depends on the database supporting DDL to be transactional. For example IBM DB2, Ingres and PostgreSQL support this feature, but Oracle on the other hand does not (it supports something different though) - you cannot rollback a truncate statement in Oracle.


1 Answers

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.

like image 136
cmt Avatar answered Sep 23 '22 05:09

cmt