/** * Realizar la conexión con la BBDD. * Esta función utiliza PDO para conectar con la base de datos. * * @throws SPException * @return \PDO */ public function getConnection() { if (!$this->_db) { $isInstalled = Config::getValue('installed'); $dbhost = Config::getValue('dbhost'); $dbuser = Config::getValue('dbuser'); $dbpass = Config::getValue('dbpass'); $dbname = Config::getValue('dbname'); $dbport = Config::getValue('dbport', 3306); if (empty($dbhost) || empty($dbuser) || empty($dbpass) || empty($dbname)) { if ($isInstalled) { Init::initError(_('No es posible conectar con la BD'), _('Compruebe los datos de conexión')); } else { throw new SPException(SPException::SP_CRITICAL, _('No es posible conectar con la BD'), _('Compruebe los datos de conexión')); } } try { $dsn = 'mysql:host=' . $dbhost . ';port=' . $dbport . ';dbname=' . $dbname . ';charset=utf8'; // $this->db = new PDO($dsn, $dbuser, $dbpass, array(PDO::ATTR_PERSISTENT => true)); $this->_db = new PDO($dsn, $dbuser, $dbpass); } catch (\Exception $e) { if ($isInstalled) { if ($e->getCode() === 1049) { Config::setValue('installed', '0'); } Init::initError(_('No es posible conectar con la BD'), 'Error ' . $e->getCode() . ': ' . $e->getMessage()); } else { throw new SPException(SPException::SP_CRITICAL, $e->getMessage(), $e->getCode()); } } } $this->_db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $this->_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); return $this->_db; }