Overview

Namespaces

  • Core
    • Auth
    • Exceptions
    • Helpers
    • Providers
  • Kernel
    • Dispatch
    • Providers
  • None
  • Vendor
    • ExampleVendor

Classes

  • Controller
  • Core\AbstractClass
  • Core\Auth\AuthenticateController
  • Core\Auth\PasswordModel
  • Core\ErrorController
  • Core\Exceptions\Exception
  • Core\Helpers\Flash
  • Core\Helpers\Hooks
  • Core\Language
  • Core\ParentController
  • Core\ParentModel
  • Core\Providers\Config
  • Core\Providers\Service
  • Core\Providers\Session
  • ErrorController
  • HomeController
  • HomeModel
  • Kernel\AppKernel
  • Kernel\Dispatch\Logger
  • Kernel\Dispatch\Mailer
  • Kernel\Dispatch\Mailer_PHP
  • Kernel\Providers\Permission
  • Kernel\Providers\Router
  • LoginController
  • LoginModel
  • Model
  • Vendor\ExampleVendor\ExampleVendorController
  • Vendor\ExampleVendor\ExampleVendorModel
  • Vendor\VendorController

Functions

  • _die
  • asset
  • clock_end
  • clock_start
  • clock_time
  • config
  • ddie
  • deleteDir
  • email_png
  • env
  • folder_action
  • folder_recurse
  • generateFile
  • getClientIP
  • getClientUserAgent
  • getVersion
  • isAjax
  • isAuth
  • isDev
  • isLocalServer
  • isProd
  • nocache
  • php
  • ppie
  • query
  • queryIndexed
  • save_ini_file
  • t
  • versioning
  • view
  • write_ini
  • Overview
  • Namespace
  • Class
  • Download
  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 
<?php

namespace Core;
use Core\Providers\Config;

/**
 * Class ParentModel connects to the database and provides useful functions (<b>query</b>, <b>queryIndexed</b>,
 * <b>updateRow</b>, <b>getRow</b>). See each one for more details.
 * @author Nico Kupfer nico.kupfer&#64;mamasu.es
 */
class ParentModel {

    /**
     * @var ParentModel The class instance.
     * @internal
     */
    protected static $instance;

    /**
     * @var \PDO The database object.
     */
    protected $db;

    /**
     * @var Config The instance of the Config class.
     */
    protected $config;

    /**
     * @var string The project ID. Set from <b>/app/config/config.ini</b>.
     */
    protected $id = __ID__;

    /**
     * Constructor of the ParentModel. Tries a connection to the database and throws exception on error
     * @throws \PDOException When there is a PDOException (i.e. wrong credentials) throws an Exception that is
     * captured and throwed to AppKernel for further processing
     * @internal
     */
    function __construct() {
        $this->config = Config::singleton();
        try {
            $params = $this->config->get('Database');
            if ($params['DB_SUPPORT'] == '0') {
                return;
            }
            $this->db = !isset($GLOBALS['db'])
                   ? ($GLOBALS['db'] = new \PDO ('mysql:host='.$params['HOST'].';dbname='.$params['DB_NAME'].';charset=utf8', $params['USERNAME'], $params['PASSWORD']))
                   :  $GLOBALS['db'];
        } catch (\PDOException $e) {
            if (isDev()) {
                throw $e;
            } else {
                throw new \Exception($this->config->get('Exceptions', 'DB_CONNECTION_ERROR'));
            }
        } catch (\Exception $e) {
            throw new \Exception($this->config->get('Exceptions', 'DB_CONNECTION_ERROR'));
        }
    }


    /**
     * Returns the class instance, creating it if it did not exist.
     * @return ParentModel
     */
    public static function singleton() {
        if (!self::$instance) {
            $v = __CLASS__;
            self::$instance = new $v;
        }

        return self::$instance;
    }

    /**
     * Runs a given $query string and returns the PDO object.
     * @param string $query
     * @param array $params = array()
     * @param bool $fetchAll = FALSE perform a fetchAll(PDO::FETCH_ASSOC) or simply return the PDO object
     * @return \PDO
     */
    public function query($query, $params = array(), $fetchAll = FALSE) {
        $res = $this->db->prepare($query);
        $res->execute($params);
        if ($fetchAll) {
            return $res->fetchAll(\PDO::FETCH_ASSOC);
        } else {
            return $res;
        }
    }

    /**
     * Runs a given $query string and returns the instance of the $className.
     * @param string $query
     * @param array $params
     * @param string $className
     * @return \PDO
     */
    public function queryClass($query, $params, $className) {
        if (!is_string($className)) {
            return NULL;
        }
        $res = $this->db->prepare($query);
        $res->execute($params);
        return $res->fetchAll(\PDO::FETCH_CLASS, $className);
    }
    
    /**
     * Runs a given $query string and returns the PDO object or the fetched results,
     * indexed by the first column.
     * @param string $query
     * @param array $params
     * @param bool $unique = TRUE. If set to FALSE, each key will contain an array of values.
     * @return \PDO
     */
    public function queryIndexed($query, $params = array(), $unique = TRUE) {
        $res = $this->db->prepare($query);
        $res->execute($params);
        $rows = $res->fetchAll(\PDO::FETCH_GROUP|\PDO::FETCH_ASSOC);
        if ($unique) {
            return array_map('reset', $rows);
        } else {
            return $rows;
        }
    }
    
    /**
     * Gets a single result from a given table name and a given column name
     * @param string $tableName
     * @param string $columnValue
     * @param string $columnName = 'id'
     * @return array with the fetched row
     */
    public function getRow($tableName, $columnValue, $columnName = 'id') {
        $query = 'SELECT `' . $columnName . '` AS `index`, t.* FROM ' . 
                 $tableName . ' t WHERE t.`' . $columnName . '` = :columnValue LIMIT 1';
        $result = $this->query($query, array(':columnValue' => $columnValue), TRUE);
        return $result ? $result[0] : array();
        
    }
    
    /**
     * Updates a given row of a given table to a given value
     * @param string $tableName Name of the table to update
     * @param string $columnName Name of the column to update
     * @param int $id The ID to index results
     * @param mixed $value The value to set
     * @return int The number of affected rows (1 or 0)
     */
    public function updateRow($tableName, $columnName, $id, $value) {
        $query = 'UPDATE ' . $tableName . ' SET `' . $columnName . '` = :value WHERE id = :id LIMIT 1';
        $res = $this->query($query, array(
            ':id' => $id,
            ':value' => $value
        ));
        return $res->rowCount();
    }
}
Ribosome API documentation generated by ApiGen