/** Flush the database and reload base fixtures.
  *
  * @param bool $rebuild
  *  true:   The database will be dropped and rebuilt.
  *  false:  The method will try just to flush the data.
  *
  * Note that the first time flushDatabase() is called (per execution), the
  *  database will be rebuilt regardless of $rebuild.
  *
  * @return static
  */
 public function flushDatabase($rebuild = false)
 {
     $this->_db->flushDatabase($rebuild);
     return $this;
 }
 /** Flush the database and reload base fixtures.
  *
  * @param bool $rebuild
  *  true:   The database will be dropped and rebuilt.
  *  false:  The method will try just to flush the data.
  *
  * Note that the first time flushDatabase() is called (per execution), the
  *  database will be rebuilt regardless of $rebuild.
  *
  * @return static
  */
 public function flushDatabase($rebuild = false)
 {
     if ($this->_connection) {
         /* The first time we run a test case, drop and rebuild the database.
          *
          * After that, we can simply truncate all tables for speed.
          */
         if (empty(self::$_dbRebuilt) or $rebuild) {
             /* Don't try to drop the database unless it exists. */
             $name = $this->getDatabaseName();
             /** @noinspection PhpUndefinedFieldInspection */
             if ($name and $this->_connection->import->databaseExists($name)) {
                 $this->_connection->dropDatabase();
             }
             $this->_connection->createDatabase();
             Doctrine_Core::loadModels(sfConfig::get('sf_lib_dir') . '/model/doctrine', Doctrine_Core::MODEL_LOADING_CONSERVATIVE);
             Doctrine_Core::createTablesFromArray(Doctrine_Core::getLoadedModels());
             self::$_dbRebuilt = true;
         } else {
             /* Determine the order we need to load models. */
             if (!isset(self::$_dbFlushTree)) {
                 /** @noinspection PhpUndefinedFieldInspection */
                 $models = $this->_connection->unitOfWork->buildFlushTree(Doctrine_Core::getLoadedModels());
                 self::$_dbFlushTree = array_reverse($models);
             }
             $this->_doPreFlush();
             /* Delete records, paying special attention to SoftDelete. */
             foreach (self::$_dbFlushTree as $model) {
                 $table = Doctrine_Core::getTable($model);
                 if ($table->hasTemplate('SoftDelete')) {
                     /** @var $record Doctrine_Template_SoftDelete */
                     foreach ($table->createQuery()->execute() as $record) {
                         $record->hardDelete();
                     }
                 }
                 $table->createQuery()->delete()->execute();
                 $table->clear();
             }
             $this->_doPostFlush();
             /** Clear all Doctrine table repositories to prevent memory leaks
              *    between tests.
              */
             $this->_connection->clear();
         }
     }
     return $this;
 }