data.php
1 <?php
2
/**
3  * Base Data class
4  *
5  * @author Zhou Yuan <yuanzhou19@gmail.com>
6  * @link http://www.infopotato.com/
7  * @copyright Copyright &copy; 2009-2011 Zhou Yuan
8  * @license http://www.opensource.org/licenses/mit-license.php MIT Licence
9  */
10
class Data {
11     
12     
/**
13      * Database object instance
14      * @var  object
15      */
16     
protected $db;
17     
18     
/**
19      * Constructor
20      *
21      * RDBMS connection needs to be specified in the subclass's constructor
22      * One database connection for one data file
23      * 
24      * @param    string    connection RDBMS connection name
25      * @return  void
26      */
27     
public function __construct($connection '') {
28         if (
$connection !== '') {
29             
$this->db self::_create_db_obj($connection);
30         }
31     }
32     
33     
/**
34      * Create database object, only when RDBMS is used
35      *
36      * @param    string $connection database connection pool, e.g., 'mysql_dao:default'
37      * @return    a specific database access object
38      */
39     
private static function _create_db_obj($connection) {
40         static 
$db_obj = array();
41         
42         if (isset(
$db_obj[$connection])) {
43             
// Returns object from runtime cache
44             
return $db_obj[$connection];
45         }
46         
47         
// Parse the connection string
48         
$conn explode(':'$connection);
49
50         if ( ! empty(
$conn)) {
51             
// Load data source config
52             
$data_source = require_once APP_CONFIG_DIR.'data_source.php';
53
54             
// Checks if data config exists 
55             //if (array_key_exists($conn[0], $data_source) && array_key_exists($conn[1], $data_source[$conn[0]])) { 
56                 //halt('An Error Was Encountered', 'Incorrect database connection string', 'sys_error');
57             //}
58             // Create instance
59             
$db_obj[$connection] = new $conn[0]($data_source[$conn[0]][$conn[1]]);
60             return 
$db_obj[$connection];
61         }
62     }
63 }
64
65
// End of file: ./system/core/data.php 
66

Page URI: http://www.infopotato.com/index.php/code/core/data/