18

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

Community
  • 1
  • 1
Dawid Ohia
  • 16,129
  • 24
  • 81
  • 95

6 Answers6

28

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.

Herzult
  • 3,429
  • 22
  • 15
  • 2
    `executeUpdate()` is now deprecated. Can this answer be updated? – Alec Nov 12 '15 at 12:01
  • @Alec Are you sure the method is deprecated? In [the code](https://github.com/doctrine/dbal/blob/2.9/lib/Doctrine/DBAL/Connection.php#L1038) the method doesn't have any `@deprecated`. – Anthony Mar 27 '19 at 15:54
  • `@deprecated This API is deprecated and will be removed after 2022` in the `Connection.php` source code. Use `executeQuery()` instead. – Xavi Montero Mar 15 '22 at 11:00
15

You can truncate data in MySQL via Doctrine so that it ignores foreign key constraints...

$connection->executeQuery('SET FOREIGN_KEY_CHECKS = 0;');
$truncateSql = $platform->getTruncateTableSQL('table_name');
$connection->executeUpdate($truncateSql);
$connection->executeQuery('SET FOREIGN_KEY_CHECKS = 1;');
JamesHalsall
  • 13,224
  • 4
  • 41
  • 66
6

I generalized the answer before to a nice function which I have used in my project, feel free to share.

/** 
 * @param array $tableNames Name of the tables which will be truncated.
 * @param bool $cascade 
 * @return void
 */
 public function truncateTables($tableNames = array(), $cascade = false) {
    $connection = $this->em->getConnection();
    $platform = $connection->getDatabasePlatform();
    $connection->executeQuery('SET FOREIGN_KEY_CHECKS = 0;');
    foreach ($tableNames as $name) {
        $connection->executeUpdate($platform->getTruncateTableSQL($name,$cascade));
    }
    $connection->executeQuery('SET FOREIGN_KEY_CHECKS = 1;');
 }
user3585918
  • 61
  • 1
  • 6
3

If you have a problem with foreign key I working with :

$connection = $this->em->getConnection();
$connection->beginTransaction();

$connection->query('DELETE FROM reception_detail');
$connection->query('ALTER TABLE reception_detail AUTO_INCREMENT = 1');
Shigiang Liu
  • 614
  • 5
  • 10
1

If you want to remove entities including associated entities that are eventually connected by foreign keys you could use a simply DQL batch query instead of truncating:

$q = $em->createQuery('delete from AppBundle\Entity\Customer');
$numDeleted = $q->execute();

http://doctrine-orm.readthedocs.org/en/latest/reference/batch-processing.html#dql-delete

This will only work with associations if you correctly configured cascade operations and orphanRemoval e.g.:

class Customer
{
    /**
     * @ORM\OneToOne(targetEntity="Address", cascade={"all"}, orphanRemoval=true)
     */
    public $address;
}

This is not a direct answer regarding the MySQL TRUNCATE command but since it is realted to Doctrine this approach could solve your issue.

webDEVILopers
  • 1,886
  • 1
  • 21
  • 35
  • Be aware that cascading all events has negative preformance impact and I wouldn't say it is a good practice. – Jan Klan Apr 17 '22 at 23:20
0

Short variant (most useful in migrations) !

Doctrine_Manager::getInstance()->getConnection('doctrine')->getDbh()->exec("TRUNCATE name");
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
Oleg Matei
  • 866
  • 13
  • 9