forked from koala-framework/koala-framework
/
Proxy.php
277 lines (243 loc) · 7.75 KB
/
Proxy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
/**
* @package Model
*/
class Kwf_Model_Proxy extends Kwf_Model_Abstract
{
/**
* @var Kwf_Model_Interface
*/
protected $_proxyModel;
protected $_rowClass = 'Kwf_Model_Proxy_Row';
protected $_rowsetClass = 'Kwf_Model_Proxy_Rowset';
public function __construct(array $config = array())
{
if (isset($config['proxyModel'])) $this->_proxyModel = $config['proxyModel'];
parent::__construct($config);
}
protected function _init()
{
parent::_init();
if (!$this->_proxyModel) {
throw new Kwf_Exception("proxyModel config is required for model '".get_class($this)."'");
}
if (!is_string($this->_proxyModel)) {
$this->_initProxyModel();
}
}
protected function _initProxyModel()
{
$this->_proxyModel->addProxyContainerModel($this);
if (!$this->_proxyModel->getFactoryConfig()) {
$this->_proxyModel->setFactoryConfig(array(
'type' => 'Proxied',
'proxy' => $this
));
}
}
//kann gesetzt werden von proxy (rekursiv bei proxys)
public function addProxyContainerModel($m)
{
parent::addProxyContainerModel($m);
$this->getProxyModel()->addProxyContainerModel($m);
}
public function getProxyModel()
{
if (is_string($this->_proxyModel)) {
$this->_proxyModel = Kwf_Model_Abstract::getInstance($this->_proxyModel);
$this->_initProxyModel();
}
return $this->_proxyModel;
}
public function createRow(array $data=array())
{
$proxyRow = $this->getProxyModel()->createRow();
$ret = new $this->_rowClass(array(
'row' => $proxyRow,
'model' => $this
));
$this->_rows[$proxyRow->getInternalId()] = $ret;
$data = array_merge($this->_default, $data);
foreach ($data as $k=>$i) {
$ret->$k = $i;
}
return $ret;
}
public function getRowByProxiedRow($proxiedRow)
{
$id = $proxiedRow->getInternalId();
if (!isset($this->_rows[$id])) {
$this->_rows[$id] = new $this->_rowClass(array(
'row' => $proxiedRow,
'model' => $this,
'exprValues' => $this->_getExprValues($proxiedRow)
));
}
return $this->_rows[$id];
}
protected function _getExprValues($proxiedRow)
{
$exprValues = array();
if ($this->_exprs) {
$r = $proxiedRow;
while ($r instanceof Kwf_Model_Proxy_Row) {
$r = $r->getProxiedRow();
}
if ($r instanceof Kwf_Model_Db_Row) {
$r = $r->getRow();
foreach (array_keys($this->_exprs) as $k) {
if (isset($r->$k)) {
$exprValues[$k] = $r->$k;
}
}
}
}
return $exprValues;
}
public function getPrimaryKey()
{
return $this->getProxyModel()->getPrimaryKey();
}
public function isEqual(Kwf_Model_Interface $other)
{
if ($this->getProxyModel()->isEqual($other)) return true;
if ($other instanceof Kwf_Model_Proxy) {
return $this->getProxyModel()->isEqual($other->getProxyModel());
}
return false;
}
public function getColumnType($col)
{
if ($this->getProxyModel()->hasColumn($col)) {
return $this->getProxyModel()->getColumnType($col);
}
return parent::getColumnType($col);
}
protected function _getOwnColumns()
{
return $this->getProxyModel()->getColumns();
}
public function hasColumn($col)
{
if ($this->getProxyModel()->hasColumn($col)) return true;
if (in_array($col, $this->getExprColumns())) return true;
foreach ($this->getSiblingModels() as $m) {
if ($m->hasColumn($col)) return true;
}
return false;
}
public function getExprColumns()
{
$ret = parent::getExprColumns();
$ret = array_merge($this->getProxyModel()->getExprColumns(), $ret);
return $ret;
}
public function getIds($where=null, $order=null, $limit=null, $start=null)
{
return $this->getProxyModel()->getIds($where, $order, $limit, $start);
}
public function getRows($where=null, $order=null, $limit=null, $start=null)
{
$proxyRowset = $this->getProxyModel()->getRows($where, $order, $limit, $start);
return new $this->_rowsetClass(array(
'rowset' => $proxyRowset,
'rowClass' => $this->_rowClass,
'model' => $this
));
}
public function getRow($select)
{
if (!$select) {
throw new Kwf_Exception('getRow needs a parameter, null is not allowed.');
}
$proxyRow = $this->getProxyModel()->getRow($select);
if (!$proxyRow) return $proxyRow;
return $this->getRowByProxiedRow($proxyRow);
}
public function deleteRows($where)
{
$ret = $this->getProxyModel()->deleteRows($where);
return $ret;
}
public function updateRows($data, $where)
{
$ret = $this->getProxyModel()->updateRows($data, $where);
return $ret;
}
public function countRows($where = array())
{
return $this->getProxyModel()->countRows($where);
}
public function getUniqueIdentifier() {
return $this->getProxyModel()->getUniqueIdentifier().'_proxy';
}
public function getSupportedImportExportFormats()
{
return $this->getProxyModel()->getSupportedImportExportFormats();
}
public function export($format, $select = array(), $options = array())
{
return $this->getProxyModel()->export($format, $select, $options);
}
public function import($format, $data, $options = array())
{
$this->getProxyModel()->import($format, $data, $options);
}
public function writeBuffer()
{
$this->getProxyModel()->writeBuffer();
}
public function getTable()
{
return $this->getProxyModel()->getTable();
}
public function getSqlForSelect($select)
{
return $this->getProxyModel()->getSqlForSelect($select);
}
public function dependentModelRowUpdated(Kwf_Model_Row_Abstract $row, $action)
{
parent::dependentModelRowUpdated($row, $action);
$this->getProxyModel()->dependentModelRowUpdated($row, $action);
}
public function childModelRowUpdated(Kwf_Model_Row_Abstract $row, $action)
{
parent::childModelRowUpdated($row, $action);
$this->getProxyModel()->childModelRowUpdated($row, $action);
}
/**
* @internal
*/
public function getExistingRows()
{
return $this->_rows;
}
public function fetchColumnByPrimaryId($column, $id)
{
if ($this->getProxyModel()->hasColumn($column)) {
return $this->getProxyModel()->fetchColumnByPrimaryId($column, $id);
} else {
return parent::fetchColumnByPrimaryId($column, $id);
}
}
public function fetchColumnsByPrimaryId(array $columns, $id)
{
return $this->getProxyModel()->fetchColumnsByPrimaryId($columns, $id);
}
public function afterInsert($row)
{
$this->getProxyModel()->afterInsert($row);
}
public function getEventSubscribers()
{
$ret = $this->getProxyModel()->getEventSubscribers();
$fc = $this->getFactoryConfig();
if (!$fc) {
throw new Kwf_Exception("Didn't find factoryConfig for '".get_class($this)."'");
}
$ret[] = Kwf_Model_EventSubscriber::getInstance('Kwf_Model_Proxy_Events', array(
'modelFactoryConfig' => $fc
));
return $ret;
}
}