Created
May 15, 2011 22:00
-
-
Save ondrejmirtes/973594 to your computer and use it in GitHub Desktop.
ModelLoader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Proj; | |
use Nette\Debug, | |
Nette\Environment, | |
PDO, | |
NotORM, | |
NotOrmPanel, | |
NotORM_Structure_Convention; | |
class ModelLoader extends \Nette\Object | |
{ | |
private $dbConnection = null; | |
private $models = array(); | |
public function __construct() | |
{ | |
$this->dbConnect(); | |
} | |
public function dbConnect(DbConfiguration $cfg) | |
{ | |
$pdo = new PDO("{$cfg->driver}:host={$cfg->host};dbname={$cfg->database}", $cfg->username, $cfg->password); | |
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
$pdo->query('SET NAMES utf8'); | |
$conn = new NotORM($pdo, new NotORM_Structure_Convention('id', '%s_id', '%ss')); | |
if ($cfg->profiler) { | |
$panel = NotOrmPanel::getInstance(); | |
$panel->setPlatform($cfg->driver); | |
Debug::addPanel($panel); | |
$conn->debug = function($query, $parameters) { | |
NotOrmPanel::getInstance()->logQuery($query, $parameters); | |
}; | |
} | |
return $this->dbConnection = $conn; | |
} | |
public function getConnection() | |
{ | |
return (null === $this->dbConnection) ? $this->dbConnect() : $this->dbConnection; | |
} | |
public function getModel($model) | |
{ | |
if (!isset($this->models[$model])) { | |
$class = 'Model\\' . ucfirst($model); | |
$this->models[$model] = new $class($this->dbConnection); | |
} | |
return $this->models[$model]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment