PHP Classes

File: DBAbstract.php

Recommend this page to a friend!
  Classes of mr   PDO Model   DBAbstract.php   Download  
File: DBAbstract.php
Role: Class source
Content type: text/plain
Description: PDO sub class
Class: PDO Model
Access database model classes using PDO
Author: By
Last change: fix access limit
Date: 9 years ago
Size: 2,886 bytes
 

Contents

Class file image Download
<?php

class DB_DBAbstract extends PDO {
   
    private static
$instance;
   
    public
$trans_started = false;
    public
$trans_ok = true;
   
    public function
__construct($dsn, $username, $passwd, $options=array()) {
      
       
parent::__construct($dsn, $username, $passwd, $options);
    }
   
   
/**
     * @final DB_DBAbstract $db
     * @param DB_DBAbstract $db
     * @return DB_DBAbstract
     */
   
public static function getInstance(&$db=-1,$config=array()) {
        if(!
self::$instance) {
            if(empty(
$config)) {
                try{
                   
self::$instance = new self(
                           
Yaf_Application::app()->getConfig()->get('db_dsn').';dbname='.
                           
Yaf_Application::app()->getConfig()->get('db_name'),
                           
Yaf_Application::app()->getConfig()->get('db_user'),
                           
Yaf_Application::app()->getConfig()->get('db_password'),
                            array(
                               
PDO::ATTR_TIMEOUT => 2,
                               
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
                           
)
                    );
                } catch (
PDOException $err) {
                    throw new
Exception($err->getMessage(), $err->getCode());
                } catch (
Exception $e) {
                   
                }
               
self::$instance->query('SET names `'.Yaf_Application::app()->getConfig()->get('db_charset').'`');
            }else {
                try {
                   
self::$instance = new self(
                       
$config['dsn'].';dbname='.$config['name'],
                       
$config['user'],
                       
$config['password'],
                        array(
                           
PDO::ATTR_TIMEOUT => 2,
                           
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
                       
)
                    );
                } catch (
PDOException $err) {
                    throw new
Exception($err->getMessage(), $err->getCode());
                }
               
self::$instance->query('SET names `'.$config['charset'].'`');
            }
        }
        if(
$db!==-1)
           
$db = self::$instance;
        return
self::$instance;
    }
   
    public function
setTransOk($ok=true) {
       
$this->trans_ok = $ok;
    }
   
    public function
trans_start() {
        if(!
$this->trans_started && $this->beginTransaction())
           
$this->trans_started = true;
        return
$this->trans_started;
    }
   
    public function
trans_stop() {
        if(
$this->inTransaction()) {
            if(
$this->trans_ok)
               
$this->commit();
            else
               
$this->rollBack();
        }
       
$this->trans_started = false;
    }
}