コード例 #1
0
 /**
  * @return Nette\Database\Connection
  * @throws Nette\InvalidArgumentException
  */
 private function createConnection($name)
 {
     if (empty($this->configs[$name])) {
         throw new Nette\InvalidArgumentException("Connection '{$name}' definition is missing in config!");
     }
     $config = $this->configs[$name];
     $connection = new Nette\Database\Connection($config["dsn"], $config["user"], $config["password"]);
     $connection->setCacheStorage($this->cacheStorage);
     $connection->setDatabaseReflection($this->databaseReflection);
     // Panels are not rendered on production but they are still logging!
     // Preventing them from log in production will decrease memory usage!
     if (!$this->isProduction) {
         $connectionPanel = new Nette\Database\Diagnostics\ConnectionPanel();
         Nette\Diagnostics\Debugger::$bar->addPanel($connectionPanel);
         $connection->onQuery[] = $connectionPanel->logQuery;
     }
     return $connection;
 }
コード例 #2
0
<?php

use Nette\Caching\Storages\FileStorage;
use Nette\Database\Connection;
use Nette\Database\Reflection\DiscoveredReflection;
use Nette\Framework;
require_once __DIR__ . '/../../bootstrap.php';
Bootstrap::init();
Bootstrap::check(__DIR__);
$connection = new Connection(Bootstrap::$config['db']['driver'] . ':dbname=' . Bootstrap::$config['db']['dbname'], Bootstrap::$config['db']['user'], Bootstrap::$config['db']['password']);
$cacheStorage = Bootstrap::$config['cache'] ? new FileStorage(__DIR__ . '/temp') : NULL;
$connection->setCacheStorage($cacheStorage);
$connection->setDatabaseReflection(new DiscoveredReflection($cacheStorage));
$dao = $connection;
$startTime = -microtime(TRUE);
ob_start();
foreach ($dao->table('employees')->limit(Bootstrap::$config['limit']) as $employe) {
    echo "{$employe->first_name} {$employe->last_name} ({$employe->emp_no})\n";
    echo "Salaries:\n";
    foreach ($employe->related('salaries') as $salary) {
        echo $salary->salary, "\n";
    }
    echo "Departments:\n";
    foreach ($employe->related('dept_emp') as $department) {
        echo $department->dept->dept_name, "\n";
    }
}
ob_end_clean();
$endTime = microtime(TRUE);
Bootstrap::result('NetteDatabase', '~2.0.0', $startTime, $endTime);