| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* TActiveRecordManager class file.
|
|
|
4 |
*
|
|
|
5 |
* @author Wei Zhuo <weizhuo[at]gmail[dot]com>
|
|
|
6 |
* @link http://www.pradosoft.com/
|
|
|
7 |
* @copyright Copyright © 2005-2008 PradoSoft
|
|
|
8 |
* @license http://www.pradosoft.com/license/
|
|
|
9 |
* @version $Id: TActiveRecordManager.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
10 |
* @package System.Data.ActiveRecord
|
|
|
11 |
*/
|
|
|
12 |
|
|
|
13 |
Prado::using('System.Data.TDbConnection');
|
|
|
14 |
Prado::using('System.Data.ActiveRecord.TActiveRecord');
|
|
|
15 |
Prado::using('System.Data.ActiveRecord.Exceptions.TActiveRecordException');
|
|
|
16 |
Prado::using('System.Data.ActiveRecord.TActiveRecordGateway');
|
|
|
17 |
|
|
|
18 |
/**
|
|
|
19 |
* TActiveRecordManager provides the default DB connection,
|
|
|
20 |
* default active record gateway, and table meta data inspector.
|
|
|
21 |
*
|
|
|
22 |
* The default connection can be set as follows:
|
|
|
23 |
* <code>
|
|
|
24 |
* TActiveRecordManager::getInstance()->setDbConnection($conn);
|
|
|
25 |
* </code>
|
|
|
26 |
* All new active record created after setting the
|
|
|
27 |
* {@link DbConnection setDbConnection()} will use that connection unless
|
|
|
28 |
* the custom ActiveRecord class overrides the ActiveRecord::getDbConnection().
|
|
|
29 |
*
|
|
|
30 |
* Set the {@link setCache Cache} property to an ICache object to allow
|
|
|
31 |
* the active record gateway to cache the table meta data information.
|
|
|
32 |
*
|
|
|
33 |
* @author Wei Zhuo <weizho[at]gmail[dot]com>
|
|
|
34 |
* @version $Id: TActiveRecordManager.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
35 |
* @package System.Data.ActiveRecord
|
|
|
36 |
* @since 3.1
|
|
|
37 |
*/
|
|
|
38 |
class TActiveRecordManager extends TComponent
|
|
|
39 |
{
|
|
|
40 |
private $_gateway;
|
|
|
41 |
private $_meta=array();
|
|
|
42 |
private $_connection;
|
|
|
43 |
|
|
|
44 |
private $_cache;
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
* @return ICache application cache.
|
|
|
48 |
*/
|
|
|
49 |
public function getCache()
|
|
|
50 |
{
|
|
|
51 |
return $this->_cache;
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
* @param ICache application cache
|
|
|
56 |
*/
|
|
|
57 |
public function setCache($value)
|
|
|
58 |
{
|
|
|
59 |
$this->_cache=$value;
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
/**
|
|
|
63 |
* @param TDbConnection default database connection
|
|
|
64 |
*/
|
|
|
65 |
public function setDbConnection($conn)
|
|
|
66 |
{
|
|
|
67 |
$this->_connection=$conn;
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
/**
|
|
|
71 |
* @return TDbConnection default database connection
|
|
|
72 |
*/
|
|
|
73 |
public function getDbConnection()
|
|
|
74 |
{
|
|
|
75 |
return $this->_connection;
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
/**
|
|
|
79 |
* @return TActiveRecordManager static instance of record manager.
|
|
|
80 |
*/
|
|
|
81 |
public static function getInstance($self=null)
|
|
|
82 |
{
|
|
|
83 |
static $instance;
|
|
|
84 |
if($self!==null)
|
|
|
85 |
$instance=$self;
|
|
|
86 |
else if($instance===null)
|
|
|
87 |
$instance = new self;
|
|
|
88 |
return $instance;
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
/**
|
|
|
92 |
* @return TActiveRecordGateway record gateway.
|
|
|
93 |
*/
|
|
|
94 |
public function getRecordGateway()
|
|
|
95 |
{
|
|
|
96 |
if($this->_gateway === null) {
|
|
|
97 |
$this->_gateway = $this->createRecordGateway();
|
|
|
98 |
}
|
|
|
99 |
return $this->_gateway;
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
/**
|
|
|
103 |
* @return TActiveRecordGateway default record gateway.
|
|
|
104 |
*/
|
|
|
105 |
protected function createRecordGateway()
|
|
|
106 |
{
|
|
|
107 |
return new TActiveRecordGateway($this);
|
|
|
108 |
}
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
|