Model.class.php 942 Bytes
Newer Older
Zohten's avatar
Zohten committed
1 2
<?php

Zohten's avatar
Zohten committed
3 4 5 6
class Model
{
    protected static function db()
    {
Zohten's avatar
Zohten committed
7 8 9 10 11 12
        return DatabasePDO::singleton();
    }

    // *** Queries in sql/model.sql.php ****
    protected static $requests = array();

Zohten's avatar
Zohten committed
13 14
    public static function addSqlQuery($key, $sql)
    {
Zohten's avatar
Zohten committed
15 16 17
        static::$requests[$key] = $sql;
    }

Zohten's avatar
Zohten committed
18 19
    public static function sqlQueryNamed($key)
    {
Zohten's avatar
Zohten committed
20 21 22
        return static::$requests[$key];
    }

Zohten's avatar
Zohten committed
23 24
    protected static function query($sql)
    {
Zohten's avatar
Zohten committed
25 26 27 28 29
        $st = static::db()->query($sql)  or die("sql query error ! request : " . $sql);
        $st->setFetchMode(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, get_called_class());
        return $st;
    }

Zohten's avatar
Zohten committed
30 31
    protected static function exec($sqlKey, $values=array())
    {
Zohten's avatar
Zohten committed
32 33 34 35 36
        $sth = static::db()->prepare(static::sqlQueryNamed($sqlKey));
        $sth->setFetchMode(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, get_called_class());
        $sth->execute($values);
        return $sth;
    }
Zohten's avatar
Zohten committed
37
}