1

I'm trying to make a user friendly way of testing via lime in symfony 1. I want to load a specific sql dump for each test I write (if required). The problem I face is that I don't know how to make dump loading independent of database type. Currently I'm using the shell exec() command. Here is the code :

  public function loadSql()
  {
    $this->diag("Loading dump...");
    if ($this->_sql_is_set)
    {
      if (file_exists($this->_getSqlPath()))
      {
        $this->_emptyDataBase();
        $options   = $this->_connection_manager->connection()->getOptions();
        $dsn_parts = $this->_connection_manager->parsePdoDsn($options['dsn']);
        exec("mysql -u{$options['username']} -p{$options['password']} {$dsn_parts['dbname']} < {$this->_getSqlPath()}");
        return $this;
      }
      else
      {
        $this->error("Nothing to load : sql file was not found in ".$this->_getDataDir());
        exit;
      }
    }
    else
    {
      $this->error("Nothing to load : sql dump was not set");
      exit;
    }
  }

$this->_connection_manager is an instance of Doctrine_Manager. Any help will with that?

1 Answer 1

1

Try with something like that:

public function loadSqlFiles(sfEvent $event)
{
    $task = $event->getSubject();
    $taskName = $task->getName();
    if ($taskName == 'insert-sql') {
        $conn = Doctrine_Manager::connection();
        $filesPath = sfConfig::get('sf_data_dir') . '/sql/full-data';

        // get all files
        $files = sfFinder::type('file')->sort_by_name()->name('*.sql')->in($filesPath);
        foreach ($files as $file) {
            $task->logSection('custom-sql', sprintf('Inserting custom sql file (%s)', $file));
            $res = $conn->getDbh()->exec(file_get_contents($file));
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Where did you get the getDbh() method ? looked through the code in Doctrine_Manager class and there is no such method. Also ran this code and getting this error Call to undefined method Doctrine_Manager::getDbh()
Oh well, made a little research, and to have access to getDbh method we need to call getCurrentConnection() first. Thx for hinting that anyway.
Yes You need to have current connection to get access to getDbh thats why $conn = Doctrine_Manager::connection();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.