Guest User

Untitled

a guest
Jan 24th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. <?php
  2.  
  3. class doctrineClearDbTask extends sfBaseTask
  4. {
  5. protected function configure()
  6. {
  7. $this->addOptions(array(
  8. new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name', 'frontend'),
  9. new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
  10. new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'doctrine'),
  11. ));
  12.  
  13. $this->namespace = 'doctrine';
  14. $this->name = 'clearDb';
  15. $this->briefDescription = 'truncate all tables in the db';
  16. $this->detailedDescription = <<<EOF
  17. The [clearDb|INFO] truncates all tables in the db connected to the current connection.
  18. [php symfony clearDb|INFO]
  19. EOF;
  20. }
  21.  
  22. protected function execute($arguments = array(), $options = array())
  23. {
  24. // initialize the database connection
  25. $databaseManager = new sfDatabaseManager($this->configuration);
  26. $connection = Doctrine_Manager::getInstance()->getCurrentConnection();
  27.  
  28. $this->dbh = $connection->getDbh();
  29. $this->dbh->query(sprintf('SET FOREIGN_KEY_CHECKS = 0;'));
  30. $tables = $connection->import->listTables();
  31. foreach ($tables as $table)
  32. {
  33. $this->truncateTable($table);
  34. }
  35. $this->dbh->query(sprintf('SET FOREIGN_KEY_CHECKS = 1;'));
  36.  
  37. unset($this->dbh);
  38. }
  39.  
  40. protected function truncateTable($tableName)
  41. {
  42. $sql = sprintf('TRUNCATE TABLE %s', $tableName);
  43. $this->dbh->query($sql);
  44. }
  45. }
Add Comment
Please, Sign In to add comment