| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* TLazyLoadList, TObjectProxy classes 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: TLazyLoadList.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
10 |
* @package System.Data.SqlMap
|
|
|
11 |
*/
|
|
|
12 |
|
|
|
13 |
/**
|
|
|
14 |
* TLazyLoadList executes mapped statements when the proxy collection is first accessed.
|
|
|
15 |
*
|
|
|
16 |
* @author Wei Zhuo <weizho[at]gmail[dot]com>
|
|
|
17 |
* @version $Id: TLazyLoadList.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
18 |
* @package System.Data.SqlMap
|
|
|
19 |
* @since 3.1
|
|
|
20 |
*/
|
|
|
21 |
class TLazyLoadList
|
|
|
22 |
{
|
|
|
23 |
private $_param;
|
|
|
24 |
private $_target;
|
|
|
25 |
private $_propertyName='';
|
|
|
26 |
private $_statement='';
|
|
|
27 |
private $_loaded=false;
|
|
|
28 |
private $_innerList;
|
|
|
29 |
private $_connection;
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Create a new proxy list that will execute the mapped statement when any
|
|
|
33 |
* of the list's method are accessed for the first time.
|
|
|
34 |
* @param TMappedStatement statement to be executed to load the data.
|
|
|
35 |
* @param mixed parameter value for the statement.
|
|
|
36 |
* @param object result object that contains the lazy collection.
|
|
|
37 |
* @param string property of the result object to set the loaded collection.
|
|
|
38 |
*/
|
|
|
39 |
protected function __construct($mappedStatement, $param, $target, $propertyName)
|
|
|
40 |
{
|
|
|
41 |
$this->_param = $param;
|
|
|
42 |
$this->_target = $target;
|
|
|
43 |
$this->_statement = $mappedStatement;
|
|
|
44 |
$this->_connection=$mappedStatement->getManager()->getDbConnection();
|
|
|
45 |
$this->_propertyName = $propertyName;
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* Create a new instance of a lazy collection.
|
|
|
50 |
* @param TMappedStatement statement to be executed to load the data.
|
|
|
51 |
* @param mixed parameter value for the statement.
|
|
|
52 |
* @param object result object that contains the lazy collection.
|
|
|
53 |
* @param string property of the result object to set the loaded collection.
|
|
|
54 |
* @return TObjectProxy proxied collection object.
|
|
|
55 |
*/
|
|
|
56 |
public static function newInstance($mappedStatement, $param, $target, $propertyName)
|
|
|
57 |
{
|
|
|
58 |
$handler = new self($mappedStatement, $param, $target, $propertyName);
|
|
|
59 |
$statement = $mappedStatement->getStatement();
|
|
|
60 |
$registry=$mappedStatement->getManager()->getTypeHandlers();
|
|
|
61 |
$list = $statement->createInstanceOfListClass($registry);
|
|
|
62 |
if(!is_object($list))
|
|
|
63 |
throw new TSqlMapExecutionException('sqlmap_invalid_lazyload_list',$statement->getID());
|
|
|
64 |
return new TObjectProxy($handler, $list);
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
/**
|
|
|
68 |
* Relay the method call to the underlying collection.
|
|
|
69 |
* @param string method name.
|
|
|
70 |
* @param array method parameters.
|
|
|
71 |
*/
|
|
|
72 |
public function intercept($method, $arguments)
|
|
|
73 |
{
|
|
|
74 |
return call_user_func_array(array($this->_innerList, $method), $arguments);
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
/**
|
|
|
78 |
* Load the data by executing the mapped statement.
|
|
|
79 |
*/
|
|
|
80 |
protected function fetchListData()
|
|
|
81 |
{
|
|
|
82 |
if($this->_loaded == false)
|
|
|
83 |
{
|
|
|
84 |
$this->_innerList = $this->_statement->executeQueryForList($this->_connection,$this->_param);
|
|
|
85 |
$this->_loaded = true;
|
|
|
86 |
//replace the target property with real list
|
|
|
87 |
TPropertyAccess::set($this->_target, $this->_propertyName, $this->_innerList);
|
|
|
88 |
}
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
/**
|
|
|
92 |
* Try to fetch the data when any of the proxy collection method is called.
|
|
|
93 |
* @param string method name.
|
|
|
94 |
* @return boolean true if the underlying collection has the corresponding method name.
|
|
|
95 |
*/
|
|
|
96 |
public function hasMethod($method)
|
|
|
97 |
{
|
|
|
98 |
$this->fetchListData();
|
|
|
99 |
if(is_object($this->_innerList))
|
|
|
100 |
return in_array($method, get_class_methods($this->_innerList));
|
|
|
101 |
return false;
|
|
|
102 |
}
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
/**
|
|
|
106 |
* TObjectProxy sets up a simple object that intercepts method calls to a
|
|
|
107 |
* particular object and relays the call to handler object.
|
|
|
108 |
*
|
|
|
109 |
* @author Wei Zhuo <weizho[at]gmail[dot]com>
|
|
|
110 |
* @version $Id: TLazyLoadList.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
111 |
* @package System.Data.SqlMap
|
|
|
112 |
* @since 3.1
|
|
|
113 |
*/
|
|
|
114 |
class TObjectProxy
|
|
|
115 |
{
|
|
|
116 |
private $_object;
|
|
|
117 |
private $_handler;
|
|
|
118 |
|
|
|
119 |
/**
|
|
|
120 |
* @param object handler to method calls.
|
|
|
121 |
* @param object the object to by proxied.
|
|
|
122 |
*/
|
|
|
123 |
public function __construct($handler, $object)
|
|
|
124 |
{
|
|
|
125 |
$this->_handler = $handler;
|
|
|
126 |
$this->_object = $object;
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
/**
|
|
|
130 |
* Relay the method call to the handler object (if able to be handled), otherwise
|
|
|
131 |
* it calls the proxied object's method.
|
|
|
132 |
* @param string method name called
|
|
|
133 |
* @param array method arguments
|
|
|
134 |
* @return mixed method return value.
|
|
|
135 |
*/
|
|
|
136 |
public function __call($method,$params)
|
|
|
137 |
{
|
|
|
138 |
if($this->_handler->hasMethod($method))
|
|
|
139 |
return $this->_handler->intercept($method, $params);
|
|
|
140 |
else
|
|
|
141 |
return call_user_func_array(array($this->_object, $method), $params);
|
|
|
142 |
}
|
|
|
143 |
}
|
|
|
144 |
|