/** * Make a query. * * Should be used for insert and update queries, but also can be used as iterator for select queries. * * @example models\Db::query('INSERT INTO posts (title) VALUES (?)', ['New post title'], 'pgsql1'); * @example $query = models\Db::query('SELECT * FROM posts', null, 'pgsql1');<br /> * foreach ($query as $item)<br /> * {<br /> * // Do something with the $item<br /> * } * @access public * @static * @param string $query * @param mixed[] $data (default: null) * @param string $name (default: 'default') * @return PDOStatement Returns statement created by query. */ public static function query($query, $data = null, $name = 'default') { $db_link =& self::$db_links[$name]['link']; if (empty($query)) { return null; } if (empty($db_link)) { throw new \Exception('No connection to database'); } // Do request if (!empty(self::$db_links[$name]['config']['debug'])) { Load::startTimer(); } self::$last_statement = $db_link->prepare($query); self::$last_statement->execute((array) $data); if (!empty(self::$db_links[$name]['config']['debug'])) { $log = $query; if (!empty($data)) { $log_data = array_map(function ($item) { return is_integer($item) == true ? $item : "'" . $item . "'"; }, (array) $data); $log = str_replace(array_pad([], substr_count($query, '?'), '?'), $log_data, $query); } Load::stopTimer($log); } // Return last statement return self::$last_statement; }
Load::$config['view_loader'] = new Twig_Loader_Filesystem([APP_MODULES_PATH, SYS_MODULES_PATH . 'Core/Views']); Load::$config['view_engine'] = new Twig_Environment(Load::$config['view_loader'], array('cache' => APP_PATH . 'Cache/Views/', 'debug' => Load::$config['debug'])); // Register default filters and functions // Site url filter $filter = new Twig_SimpleFilter('siteUrl', function ($url = null, $prefix = null, $current_prefix = true) { return Router::siteUrl($url, $prefix, $current_prefix); }); Load::$config['view_engine']->addFilter($filter); // Site url function $function = new Twig_SimpleFunction('siteUrl', function ($url = null, $prefix = null, $current_prefix = true) { return Router::siteUrl($url, $prefix, $current_prefix); }); Load::$config['view_engine']->addFunction($function); // Start timer function $function = new Twig_SimpleFunction('startTimer', function () { Load::startTimer(); }); Load::$config['view_engine']->addFunction($function); // Stop timer function $function = new Twig_SimpleFunction('stopTimer', function ($name) { Load::stopTimer($name); }); Load::$config['view_engine']->addFunction($function); // Mark time function $function = new Twig_SimpleFunction('markTime', function ($name) { Load::markTime($name); }); Load::$config['view_engine']->addFunction($function); // Debug output function $function = new Twig_SimpleFunction('debugOutput', function () { return Load::debugOutput();