1: | <?php
|
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: | |
18: |
|
19: | defined('XOOPS_ROOT_PATH') || exit('Restricted access');
|
20: |
|
21: | |
22: | |
23: | |
24: | |
25: | |
26: | |
27: |
|
28: |
|
29: | |
30: | |
31: | |
32: | |
33: | |
34: | |
35: | |
36: | |
37: |
|
38: | class XoopsModelJoint extends XoopsModelAbstract
|
39: | {
|
40: | |
41: | |
42: | |
43: | |
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: | |
61: | |
62: | |
63: | |
64: | |
65: | |
66: | |
67: | |
68: | |
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 (!empty($fields) && \is_array($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: |
|
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) && \method_exists($criteria, 'renderWhere')) {
|
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: | throw new \RuntimeException(
|
109: | \sprintf(_DB_QUERY_ERROR, $sql) . $this->handler->db->error(), E_USER_ERROR
|
110: | );
|
111: | }
|
112: | $ret = array();
|
113: | if ($asObject) {
|
114: | while (false !== ($myrow = $this->handler->db->fetchArray($result))) {
|
115: | $object = $this->handler->create(false);
|
116: | $object->assignVars($myrow);
|
117: | $ret[$myrow[$this->handler->keyName]] = $object;
|
118: | unset($object);
|
119: | }
|
120: | } else {
|
121: | $object = $this->handler->create(false);
|
122: | while (false !== ($myrow = $this->handler->db->fetchArray($result))) {
|
123: | $object->assignVars($myrow);
|
124: | $ret[$myrow[$this->handler->keyName]] = $object->getValues(array_keys($myrow));
|
125: | }
|
126: | unset($object);
|
127: | }
|
128: |
|
129: | return $ret;
|
130: | }
|
131: |
|
132: | |
133: | |
134: | |
135: | |
136: | |
137: |
|
138: | public function getCountByLink(CriteriaElement $criteria = null)
|
139: | {
|
140: | if (!$this->validateLinks()) {
|
141: | return null;
|
142: | }
|
143: |
|
144: | $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}";
|
145: | if (isset($criteria) && \method_exists($criteria, 'renderWhere')) {
|
146: | $sql .= ' ' . $criteria->renderWhere();
|
147: | }
|
148: | $result = $this->handler->db->query($sql);
|
149: | if (!$this->handler->db->isResultSet($result)) {
|
150: | return false;
|
151: | }
|
152: | $myrow = $this->handler->db->fetchArray($result);
|
153: |
|
154: | return (int)$myrow['count'];
|
155: | }
|
156: |
|
157: | |
158: | |
159: | |
160: | |
161: | |
162: |
|
163: | public function getCountsByLink(CriteriaElement $criteria = null)
|
164: | {
|
165: | if (!$this->validateLinks()) {
|
166: | return null;
|
167: | }
|
168: | $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}";
|
169: | if (isset($criteria) && \method_exists($criteria, 'renderWhere')) {
|
170: | $sql .= ' ' . $criteria->renderWhere();
|
171: | }
|
172: | $sql .= " GROUP BY l.{$this->handler->field_link}";
|
173: | $result = $this->handler->db->query($sql);
|
174: | if (!$this->handler->db->isResultSet($result)) {
|
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: | |
187: | |
188: | |
189: | |
190: | |
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) && \method_exists($criteria, 'renderWhere')) {
|
203: | $sql .= ' ' . $criteria->renderWhere();
|
204: | }
|
205: |
|
206: | return $this->handler->db->query($sql);
|
207: | }
|
208: |
|
209: | |
210: | |
211: | |
212: | |
213: | |
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) && \method_exists($criteria, 'renderWhere')) {
|
222: | $sql .= ' ' . $criteria->renderWhere();
|
223: | }
|
224: |
|
225: | return $this->handler->db->query($sql);
|
226: | }
|
227: | }
|
228: | |