74 lines
2.1 KiB
PHP
74 lines
2.1 KiB
PHP
<?php
|
|
class Metier_Util_PdoLib
|
|
{
|
|
/**
|
|
*
|
|
* @var \PDO
|
|
*/
|
|
protected $conn = false;
|
|
|
|
/**
|
|
* Connection PDO
|
|
* Note : Transition avant de faire les librairies Metier PSR-4
|
|
* @param array $config
|
|
* 'host' => 'IP',
|
|
* 'port' => '3306',
|
|
* 'dbname' => 'db',
|
|
* 'charset' => 'utf8',
|
|
* 'user' => 'user',
|
|
* 'password' => 'password',
|
|
* @return boolean|PDO
|
|
*/
|
|
public function __construct(array $config = array())
|
|
{
|
|
$user = null;
|
|
$password = null;
|
|
$options = array();
|
|
|
|
// Get Application config from registry
|
|
if (count($config) == 0) {
|
|
$c = Zend_Registry::get('config');
|
|
$params = $c->profil->db->metier->params;
|
|
$dsn = "mysql:host=".$params->host.
|
|
";port=".(isset($params->port) ? $params->port : '3306').
|
|
";dbname=".$params->dbname.
|
|
";charset=utf8";
|
|
$user = $params->username;
|
|
$password = $params->password;
|
|
}
|
|
// Get config from params
|
|
else {
|
|
$dsn = "mysql:host=".$config['host'].
|
|
";port=".(isset($config['port']) ? $config['port'] : '3306').
|
|
";dbname=".$config['dbname'].
|
|
";charset=".(isset($config['charset']) ? $config['charset'] : 'utf8');
|
|
if (array_key_exists('username', $config)) {
|
|
$user = $config['username'];
|
|
}
|
|
if (array_key_exists('password', $config)) {
|
|
$password = $config['password'];
|
|
}
|
|
if (array_key_exists('options', $config)) {
|
|
$options = $config['options'];
|
|
}
|
|
}
|
|
|
|
// Try to load PDO
|
|
try {
|
|
$this->conn = new PDO($dsn, $user, $password, $options);
|
|
$this->conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
|
|
} catch (PDOException $e) {
|
|
echo $e->getMessage();
|
|
$this->conn = false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get connection
|
|
* @return PDO
|
|
*/
|
|
public function getConnection()
|
|
{
|
|
return $this->conn;
|
|
}
|
|
} |