create() public static method

public static create ( array $config )
$config array
Example #1
0
 public function testBackup()
 {
     $this->conn->query('DROP DATABASE IF EXISTS backup_test');
     $this->conn->query('CREATE DATABASE IF NOT EXISTS backup_test CHARSET utf8;');
     $dest = Connection::create(['dsn' => 'mysql:host=localhost;dbname=backup_test', 'user' => 'root', 'pass' => null, 'connection_options' => [PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8']]);
     $backup = new MySQLBackup();
     $backup->backup($this->conn, $dest);
 }
Example #2
0
 public function backupToDatabase(Connection $source, $databaseName, $dropAndCreate = false)
 {
     $newDSN = clone $source->getDSN();
     if ($newDSN->getAttribute('dbname') == $databaseName) {
         throw new LogicException('Backup to the same database.');
     }
     if ($dropAndCreate) {
         $source->query('DROP DATABASE IF EXISTS ' . $databaseName);
         $source->query('CREATE DATABASE IF NOT EXISTS ' . $databaseName . ' CHARSET utf8');
     }
     // Create new dest database connection
     $newConfig = $source->getConfig();
     $newDSN->setAttribute('dbname', $databaseName);
     $newConfig['dsn'] = $newDSN->__toString();
     $dest = Connection::create($newConfig);
     return $this->backup($source, $dest);
 }
 /**
  * Create connection.
  *
  *    $dbh = new Connection('mysql:host=localhost;dbname=test', $user, $pass);
  *
  *    $pdo = new Connection(
  *          'mysql:host=hostname;dbname=defaultDbName',
  *          'username',
  *          'password',
  *          array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8") 
  *    ); 
  *
  *    $dbh = new Connection('pgsql:dbname=$dbname; host=$host; username=$username; password=$password'); 
  *    $pdo = new Connection( 'sqlite::memory:', null, null, array(PDO::ATTR_PERSISTENT => true) );
  *                     sqlite2:mydb.sq2
  */
 public function getConnection($sourceId)
 {
     if ($sourceId === 'default' && $this->config) {
         $sourceId = $this->config->getDefaultDataSourceId();
     }
     // use cached connection objects
     if (isset($this->conns[$sourceId])) {
         return $this->conns[$sourceId];
     }
     if (!isset($this->datasources[$sourceId])) {
         throw new UndefinedDataSourceException("data source {$sourceId} not found.");
     }
     // Only for MySQl
     // $conn->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
     return $this->conns[$sourceId] = Connection::create($this->datasources[$sourceId]);
 }