74 lines
2.1 KiB
PHP
Raw Normal View History

2016-08-10 09:40:55 +02:00
<?php
class Metier_Util_PdoLib
{
2016-09-26 11:43:02 +02:00
/**
*
* @var \PDO
*/
protected $conn = false;
2016-08-10 09:40:55 +02:00
/**
* 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');
2016-09-26 11:43:02 +02:00
if (array_key_exists('username', $config)) {
$user = $config['username'];
2016-08-10 09:40:55 +02:00
}
2016-09-26 11:43:02 +02:00
if (array_key_exists('password', $config)) {
2016-08-10 09:40:55 +02:00
$password = $config['password'];
}
if (array_key_exists('options', $config)) {
$options = $config['options'];
}
}
// Try to load PDO
try {
2016-09-26 11:43:02 +02:00
$this->conn = new PDO($dsn, $user, $password, $options);
$this->conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
2016-08-10 09:40:55 +02:00
} catch (PDOException $e) {
2016-09-26 11:43:02 +02:00
echo $e->getMessage();
$this->conn = false;
2016-08-10 09:40:55 +02:00
}
2016-09-26 11:43:02 +02:00
}
2016-08-10 09:40:55 +02:00
2016-09-26 11:43:02 +02:00
/**
* Get connection
* @return PDO
*/
public function getConnection()
{
return $this->conn;
2016-08-10 09:40:55 +02:00
}
}