I worked on the same problem and did not find a standard way to solve it. So I got a working, but not standard zf2
- Create a small interface for managing Db connections
- Implements it as a small class to get a PDO connection object for your database
- execute arbitrary requests
Code example
// Filename: /module/MyTools/src/MyTools/Service/DbModelServiceInterface.php namespace MyTools\Service; interface DbModelServiceInterface { /** * Will return the result of querying the curret database * * @param type $query * @result mixed */ public function dbQuery($query); /** * Will return a connection object that links to curret database * * @result mixed */ public function getConnection(); }
The class that implements the interface. It creates and offers a PDO connection. Note It needs additional code to close conns and refine security ... It checks and is fully functional. the code:
// Filename: /module/MyTools/src/MyTools/Service/DbModelServiceMySql.php namespace MyTools\Service; use MyTools\Service\DbModelServiceInterface; use PDO; class DbModelServiceMySql implements DbModelServiceInterface { protected $driverConfig; protected $connection; protected $isconnected = FALSE; protected $dbname = ''; /** * Creates a connection to main database */ public function __construct() { $driverConfig = self::getDriverDef(); $this->driverConfig = $driverConfig; // new PDO($driverConfig['dsn'], $driverConfig['username'], $driverConfig['password']); $this->_connect(); } protected function _connect(){ $dsn = (isset($this->driverConfig['dsn'])) ? $this->driverConfig['dsn'] : ''; $username = (isset($this->driverConfig['username'])) ? $this->driverConfig['username'] : ''; $password = (isset($this->driverConfig['password'])) ? $this->driverConfig['password'] : ''; if( ($dsn) && ($username) && ($password)){ $options = [PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8', ]; try { $this->connection = new PDO($dsn, $username, $password, $options); $this->isconnected = TRUE; $this->_setdbname($dsn); } catch (Exception $ex) { throw new RuntimeException('YOUR ERROR MESSAGE.'); } } return $this->isconnected; } protected function _setdbname($dsn){ if($dsn){ $chunks = explode(';', ''.$dsn); foreach($chunks as $chunk){ if(strpos('***'.$chunk, 'dbname') > 2){ $nombre = explode('=', $chunk); $this->dbname = $nombre[1]; break; } } } } /** * {@inheritDoc} */ public function dbQuery($query) { if($this->connection){ $resultset = $this->connection->query($query); if($resultset){ return $resultset->fetchAll(PDO::FETCH_ASSOC); }else{ return ['Error' => 'YOUR CUSTOM ERROR MESSAGE.']; } }else{ return ['Error' => 'OTHER CUSTOM ERROR MESSAGE']; } } public static function getDriverDef() { $autoloadDir = __DIR__ . '../../../../../../config/autoload/'; $credentialsdb = include $autoloadDir . 'local.php'; $globaldb = include $autoloadDir . 'global.php'; $def = (isset($globaldb['db'])) ? $globaldb['db'] : array(); $credentials = (isset($credentialsdb['db'])) ? $credentialsdb['db'] : $credentialsdb; return array_merge($def, $credentials); } /** * {@inheritDoc} */ public function getConnection() { if($this->connection){ return $this->connection; }else{ return 'Error: YOUR CUSTOM ERROR MESSAGE'; } } /** * {@inheritDoc} */ public function getDbName(){ return $this->dbname; } }
You now have a class that you can create elsewhere to fulfill the queries you need.
usage: code:
$myQuery = 'the very very complex query you need to execute' $myDbConn = new MyTools\Service\DbModelServiceMySql(); $result = $myDbConn->dbQuery($myQuery);
If you succeed, you get the resulting array of pairs columnName => value
source share