|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Object joint handler class. |
|
4
|
|
|
* |
|
5
|
|
|
* You may not change or alter any portion of this comment or credits |
|
6
|
|
|
* of supporting developers from this source code or any supporting source code |
|
7
|
|
|
* which is considered copyrighted (c) material of the original comment or credit authors. |
|
8
|
|
|
* This program is distributed in the hope that it will be useful, |
|
9
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|
11
|
|
|
* |
|
12
|
|
|
* @copyright (c) 2000-2016 XOOPS Project (www.xoops.org) |
|
13
|
|
|
* @license GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html) |
|
14
|
|
|
* @package kernel |
|
15
|
|
|
* @subpackage model |
|
16
|
|
|
* @since 2.3.0 |
|
17
|
|
|
* @author Taiwen Jiang <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
defined('XOOPS_ROOT_PATH') || exit('Restricted access'); |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Object joint handler class. |
|
23
|
|
|
* |
|
24
|
|
|
* @author Taiwen Jiang <[email protected]> |
|
25
|
|
|
* |
|
26
|
|
|
* {@link XoopsModelAbstract} |
|
27
|
|
|
*/ |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Usage of methods provided by XoopsModelJoint: |
|
31
|
|
|
* |
|
32
|
|
|
* Step #1: set linked table and adjoint fields through XoopsPersistableObjectHandler: |
|
33
|
|
|
* $handler->table_link = $handler->db->prefix("the_linked_table"); // full name of the linked table that is used for the query |
|
34
|
|
|
* $handler->field_link = "the_linked_field"; // name of field in linked table that will be used to link the linked table with current table |
|
35
|
|
|
* $handler->field_object = "the_object_field"; // name of field in current table that will be used to link the linked table with current table; linked field name will be used if the field name is not set |
|
36
|
|
|
* Step #2: fetch data |
|
37
|
|
|
*/ |
|
38
|
|
|
class XoopsModelJoint extends XoopsModelAbstract |
|
39
|
|
|
{ |
|
40
|
|
|
/** |
|
41
|
|
|
* Validate information for the link |
|
42
|
|
|
* |
|
43
|
|
|
* @access private |
|
44
|
|
|
*/ |
|
45
|
|
|
public function validateLinks() |
|
46
|
|
|
{ |
|
47
|
|
|
if (empty($this->handler->table_link) || empty($this->handler->field_link)) { |
|
48
|
|
|
trigger_error('The linked table is not set yet.', E_USER_WARNING); |
|
49
|
|
|
|
|
50
|
|
|
return null; |
|
51
|
|
|
} |
|
52
|
|
|
if (empty($this->handler->field_object)) { |
|
53
|
|
|
$this->handler->field_object = $this->handler->field_link; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return true; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* get a list of objects matching a condition joint with another related object |
|
61
|
|
|
* |
|
62
|
|
|
* @param CriteriaElement|CriteriaCompo $criteria |
|
63
|
|
|
* @param array $fields variables to fetch |
|
64
|
|
|
* @param bool $asObject flag indicating as object, otherwise as array |
|
65
|
|
|
* @param string $field_link field of linked object for JOIN; deprecated, for backward compatibility |
|
66
|
|
|
* @param string $field_object field of current object for JOIN; deprecated, for backward compatibility |
|
67
|
|
|
* @return array of objects <a href='psi_element://XoopsObject'>XoopsObject</a> |
|
68
|
|
|
* @internal param CriteriaElement $object <a href='psi_element://CriteriaElement'>CriteriaElement</a> to match to match |
|
69
|
|
|
*/ |
|
70
|
|
|
public function &getByLink(CriteriaElement $criteria = null, $fields = null, $asObject = true, $field_link = null, $field_object = null) |
|
71
|
|
|
{ |
|
72
|
|
|
if (!empty($field_link)) { |
|
73
|
|
|
$this->handler->field_link = $field_link; |
|
74
|
|
|
} |
|
75
|
|
|
if (!empty($field_object)) { |
|
76
|
|
|
$this->handler->field_object = $field_object; |
|
77
|
|
|
} |
|
78
|
|
|
if (!$this->validateLinks()) { |
|
|
|
|
|
|
79
|
|
|
return null; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
if (is_array($fields) && count($fields)) { |
|
83
|
|
|
if (!in_array('o.' . $this->handler->keyName, $fields)) { |
|
84
|
|
|
$fields[] = 'o.' . $this->handler->keyName; |
|
85
|
|
|
} |
|
86
|
|
|
$select = implode(',', $fields); |
|
87
|
|
|
} else { |
|
88
|
|
|
$select = 'o.*, l.*'; |
|
89
|
|
|
} |
|
90
|
|
|
$limit = null; |
|
91
|
|
|
$start = null; |
|
92
|
|
|
// $field_object = empty($field_object) ? $field_link : $field_object; |
|
93
|
|
|
$sql = " SELECT {$select}" . " FROM {$this->handler->table} AS o" . " LEFT JOIN {$this->handler->table_link} AS l ON o.{$this->handler->field_object} = l.{$this->handler->field_link}"; |
|
94
|
|
|
if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
|
95
|
|
|
$sql .= ' ' . $criteria->renderWhere(); |
|
|
|
|
|
|
96
|
|
|
if ($sort = $criteria->getSort()) { |
|
97
|
|
|
$sql .= " ORDER BY {$sort} " . $criteria->getOrder(); |
|
98
|
|
|
$orderSet = true; |
|
99
|
|
|
} |
|
100
|
|
|
$limit = $criteria->getLimit(); |
|
101
|
|
|
$start = $criteria->getStart(); |
|
102
|
|
|
} |
|
103
|
|
|
if (empty($orderSet)) { |
|
104
|
|
|
$sql .= " ORDER BY o.{$this->handler->keyName} DESC"; |
|
105
|
|
|
} |
|
106
|
|
|
$result = $this->handler->db->query($sql, $limit, $start); |
|
107
|
|
|
if (!$this->handler->db->isResultSet($result)) { |
|
108
|
|
|
\trigger_error("Query Failed! SQL: $sql- Error: " . $this->handler->db->error(), E_USER_ERROR); |
|
109
|
|
|
} |
|
110
|
|
|
$ret = array(); |
|
111
|
|
|
if ($asObject) { |
|
112
|
|
|
while (false !== ($myrow = $this->handler->db->fetchArray($result))) { |
|
113
|
|
|
$object = $this->handler->create(false); |
|
114
|
|
|
$object->assignVars($myrow); |
|
115
|
|
|
$ret[$myrow[$this->handler->keyName]] = $object; |
|
116
|
|
|
unset($object); |
|
117
|
|
|
} |
|
118
|
|
|
} else { |
|
119
|
|
|
$object = $this->handler->create(false); |
|
120
|
|
|
while (false !== ($myrow = $this->handler->db->fetchArray($result))) { |
|
121
|
|
|
$object->assignVars($myrow); |
|
122
|
|
|
$ret[$myrow[$this->handler->keyName]] = $object->getValues(array_keys($myrow)); |
|
123
|
|
|
} |
|
124
|
|
|
unset($object); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return $ret; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Count of objects matching a condition |
|
132
|
|
|
* |
|
133
|
|
|
* @param CriteriaElement|CriteriaCompo $criteria {@link CriteriaElement} to match |
|
134
|
|
|
* @return int|false count of objects |
|
135
|
|
|
*/ |
|
136
|
|
|
public function getCountByLink(CriteriaElement $criteria = null) |
|
137
|
|
|
{ |
|
138
|
|
|
if (!$this->validateLinks()) { |
|
|
|
|
|
|
139
|
|
|
return null; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
$sql = " SELECT COUNT(DISTINCT o.{$this->handler->keyName}) AS count" . " FROM {$this->handler->table} AS o" . " LEFT JOIN {$this->handler->table_link} AS l ON o.{$this->handler->field_object} = l.{$this->handler->field_link}"; |
|
143
|
|
|
if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
|
144
|
|
|
$sql .= ' ' . $criteria->renderWhere(); |
|
145
|
|
|
} |
|
146
|
|
|
$result = $this->handler->db->query($sql); |
|
147
|
|
|
if (!$this->handler->db->isResultSet($result)) { |
|
148
|
|
|
// \trigger_error("Query Failed! SQL: $sql- Error: " . $this->handler->db->error(), E_USER_ERROR); |
|
149
|
|
|
return false; |
|
150
|
|
|
} |
|
151
|
|
|
$myrow = $this->handler->db->fetchArray($result); |
|
152
|
|
|
|
|
153
|
|
|
return (int)$myrow['count']; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* array of count of objects matching a condition of, groupby linked object keyname |
|
158
|
|
|
* |
|
159
|
|
|
* @param CriteriaElement|CriteriaCompo $criteria {@link CriteriaElement} to match |
|
160
|
|
|
* @return int|false|array|null count of objects |
|
161
|
|
|
*/ |
|
162
|
|
|
public function getCountsByLink(CriteriaElement $criteria = null) |
|
163
|
|
|
{ |
|
164
|
|
|
if (!$this->validateLinks()) { |
|
|
|
|
|
|
165
|
|
|
return null; |
|
166
|
|
|
} |
|
167
|
|
|
$sql = " SELECT l.{$this->handler->field_link}, COUNT(*)" . " FROM {$this->handler->table} AS o" . " LEFT JOIN {$this->handler->table_link} AS l ON o.{$this->handler->field_object} = l.{$this->handler->field_link}"; |
|
168
|
|
|
if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
|
169
|
|
|
$sql .= ' ' . $criteria->renderWhere(); |
|
170
|
|
|
} |
|
171
|
|
|
$sql .= " GROUP BY l.{$this->handler->field_link}"; |
|
172
|
|
|
$result = $this->handler->db->query($sql); |
|
173
|
|
|
if (!$this->handler->db->isResultSet($result)) { |
|
174
|
|
|
// \trigger_error("Query Failed! SQL: $sql- Error: " . $this->handler->db->error(), E_USER_ERROR); |
|
175
|
|
|
return false; |
|
176
|
|
|
} |
|
177
|
|
|
$ret = array(); |
|
178
|
|
|
while (false !== (list($id, $count) = $this->handler->db->fetchRow($result))) { |
|
179
|
|
|
$ret[$id] = $count; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
return $ret; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* update objects matching a condition against linked objects |
|
187
|
|
|
* |
|
188
|
|
|
* @param array $data array of key => value |
|
189
|
|
|
* @param CriteriaElement|CriteriaCompo $criteria {@link CriteriaElement} to match |
|
190
|
|
|
* @return int|null count of objects |
|
191
|
|
|
*/ |
|
192
|
|
|
public function updateByLink($data, CriteriaElement $criteria = null) |
|
193
|
|
|
{ |
|
194
|
|
|
if (!$this->validateLinks()) { |
|
|
|
|
|
|
195
|
|
|
return null; |
|
196
|
|
|
} |
|
197
|
|
|
$set = array(); |
|
198
|
|
|
foreach ($data as $key => $val) { |
|
199
|
|
|
$set[] = "o.{$key}=" . $this->handler->db->quoteString($val); |
|
200
|
|
|
} |
|
201
|
|
|
$sql = " UPDATE {$this->handler->table} AS o" . ' SET ' . implode(', ', $set) . " LEFT JOIN {$this->handler->table_link} AS l ON o.{$this->handler->field_object} = l.{$this->handler->field_link}"; |
|
202
|
|
|
if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
|
203
|
|
|
$sql .= ' ' . $criteria->renderWhere(); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
return $this->handler->db->query($sql); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Delete objects matching a condition against linked objects |
|
211
|
|
|
* |
|
212
|
|
|
* @param CriteriaElement|CriteriaCompo $criteria {@link CriteriaElement} to match |
|
213
|
|
|
* @return int|null count of objects |
|
214
|
|
|
*/ |
|
215
|
|
|
public function deleteByLink(CriteriaElement $criteria = null) |
|
216
|
|
|
{ |
|
217
|
|
|
if (!$this->validateLinks()) { |
|
|
|
|
|
|
218
|
|
|
return null; |
|
219
|
|
|
} |
|
220
|
|
|
$sql = "DELETE FROM {$this->handler->table} AS o " . " LEFT JOIN {$this->handler->table_link} AS l ON o.{$this->handler->field_object} = l.{$this->handler->field_link}"; |
|
221
|
|
|
if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
|
222
|
|
|
$sql .= ' ' . $criteria->renderWhere(); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
return $this->handler->db->query($sql); |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|
If an expression can have both
false, andnullas possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.