1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
public function connect()
{
$dsn = $this->getParameter('dsn');
if ($dsn == null) {
// missing required dsn parameter
throw new sfDatabaseException('Database configuration specifies method "dsn", but is missing dsn parameter.');
}
try {
$username = $this->getParameter('username');
$password = $this->getParameter('password');
$persistent = $this->getParameter('persistent');
$options = ($persistent) ? array(PDO::ATTR_PERSISTENT => true) : array();
$this->connection = new PDO($dsn, $username, $password, $options);
// encoding
$encoding = $this->getParameter('encoding', 'latin1');
$this->connection->exec('SET NAMES '.$encoding);
}
catch (PDOException $e) {
throw new sfDatabaseException($e->getMessage());
}
// lets generate exceptions instead of silent failures
//~ if ( sfConfig::get('sf_debug') ) {
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//~ }
//~ else {
//~ $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
//~ }
// compatability
if ( $this->getParameter('compat') ) {
$this->connection->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL);
}
// nulls
if ( $this->getParameter('nulls') ) {
$this->connection->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_EMPTY_STRING);
}
// auto commit
if ( $this->getParameter('autocommit') ) {
$this->connection->setAttribute(PDO::ATTR_AUTOCOMMIT, true);
}
$this->resource = $this->connection;
} |
Partager