batch/library/Metier/Util/PdoLib.php

60 lines
1.9 KiB
PHP
Raw Normal View History

2016-08-08 15:51:03 +02:00
<?php
class Metier_Util_PdoLib
{
/**
* 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('user', $config)) {
$user = $config['user'];
}
if (array_key_exists('paswword', $config)) {
$password = $config['password'];
}
if (array_key_exists('options', $config)) {
$options = $config['options'];
}
}
// Try to load PDO
try {
$conn = new PDO($dsn, $user, $password, $options);
$conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
} catch (PDOException $e) {
return false;
}
return $conn;
}
}