示例#1
0
 /**
  * Setup a database connection (to a table)
  * @param string an optional table name, optional only in PHP <= 5.3.0
  */
 public function __construct($tableName = NULL)
 {
     // db connection
     $this->db = Fari_DbPdo::getConnection();
     // table name exists?
     if (isset($tableName)) {
         $this->tableName = $tableName;
     } else {
         if (isset($this->tableName)) {
             assert('!empty($this->tableName); // table name needs to be provided');
         } else {
             // are we using high enough version of PHP for late static binding?
             try {
                 if (version_compare(phpversion(), '5.3.0', '<=') == TRUE) {
                     throw new Fari_Exception('Table name can automatically only be resolved in PHP 5.3.0.');
                 }
             } catch (Fari_Exception $exception) {
                 $exception->fire();
             }
             // ... yes, get the name of the class as the name of the table
             $this->tableName = get_called_class();
         }
     }
     // attach observers
     $this->logger = new Fari_DbLogger();
     $this->logger->attach(new Fari_ApplicationLogger());
     // attach validator
     $this->validator = $this->attachValidator();
     // prep relationships
     $this->relationships = new Fari_DbTableRelationships();
 }
示例#2
0
 /**
  * Connect to the database or return connection instance.
  * @return PDO Instance of PDO connection
  */
 public static function getConnection()
 {
     // do we have an instance already?
     if (!self::$dbConnection instanceof PDO) {
         try {
             // which driver are we using?
             switch (strtolower(DB_DRIVER)) {
                 // MySQL
                 case 'mysql':
                     $pdoInstance = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME . ';unix_socket=/var/run/mysqld/mysqld.sock', DB_USER, DB_PASS);
                     break;
                     // PostgreSQL (untested)
                 // PostgreSQL (untested)
                 case 'pgsql':
                     $pdoInstance = new PDO('pgsql:dbname=' . DB_NAME . ';host=' . DB_HOST, DB_USER, DB_PASS);
                     break;
                     // SQLite 3
                 // SQLite 3
                 case 'sqlite3':
                 case 'sqlite':
                     $pdoInstance = new PDO('sqlite:' . BASEPATH . '/' . DB_NAME);
                     break;
                     // SQLite 2
                 // SQLite 2
                 case 'sqlite2':
                     $pdoInstance = new PDO('sqlite2:' . BASEPATH . '/' . DB_NAME);
                     break;
             }
             // error mode on, throw exceptions
             $pdoInstance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
             // create Fari_DbConnection using the PDO instance
             self::$dbConnection = $pdoInstance;
         } catch (PDOException $exception) {
             try {
                 throw new Fari_Exception('Cannot connect to DB: ' . $exception->getMessage() . '.');
             } catch (Fari_Exception $exception) {
                 $exception->fire();
             }
         }
     }
     // return Fari_DbConnection
     return self::$dbConnection;
 }