XOOPS  2.6.0
Joint.php
Go to the documentation of this file.
1 <?php
2 /*
3  You may not change or alter any portion of this comment or credits
4  of supporting developers from this source code or any supporting source code
5  which is considered copyrighted (c) material of the original comment or credit authors.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 */
11 
13 
16 
43 {
49  private function validateLinks()
50  {
51  if (empty($this->handler->table_link) || empty($this->handler->field_link)) {
52  trigger_error("The linked table is not set yet.", E_USER_WARNING);
53  return false;
54  }
55  if (empty($this->handler->field_object)) {
56  $this->handler->field_object = $this->handler->field_link;
57  }
58  return true;
59  }
60 
72  public function getByLink(
74  $fields = null,
75  $asObject = true,
76  $field_link = null,
77  $field_object = null
78  ) {
79  if (!empty($field_link)) {
80  $this->handler->field_link = $field_link;
81  }
82  if (!empty($field_object)) {
83  $this->handler->field_object = $field_object;
84  }
85  if (!$this->validateLinks()) {
86  return false;
87  }
88 
89  $qb = $this->handler->db2->createXoopsQueryBuilder();
90  if (is_array($fields) && count($fields)) {
91  if (!in_array("o." . $this->handler->keyName, $fields)) {
92  $fields[] = "o." . $this->handler->keyName;
93  }
94  $first = true;
95  foreach ($fields as $field) {
96  if ($first) {
97  $first = false;
98  $qb->select($field);
99  } else {
100  $qb->addSelect($field);
101  }
102  }
103  } else {
104  $qb ->select('o.*')
105  ->addSelect('l.*');
106  }
107  $qb ->from($this->handler->table, 'o')
108  ->leftJoin(
109  'o',
110  $this->handler->table_link,
111  'l',
112  "o.{$this->handler->field_object} = l.{$this->handler->field_link}"
113  );
114  if (isset($criteria) && ($criteria instanceof CriteriaElement)) {
115  $qb = $criteria->renderQb($qb);
116  }
117  $result = $qb->execute();
118  $ret = array();
119  if ($asObject) {
120  while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) {
121  $object = $this->handler->create(false);
122  $object->assignVars($myrow);
123  $ret[$myrow[$this->handler->keyName]] = $object;
124  unset($object);
125  }
126  } else {
127  $object = $this->handler->create(false);
128  while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) {
129  $object->assignVars($myrow);
130  $ret[$myrow[$this->handler->keyName]] = $object->getValues();
131  }
132  unset($object);
133  }
134  return $ret;
135  }
136 
144  public function getCountByLink(CriteriaElement $criteria = null)
145  {
146  if (!$this->validateLinks()) {
147  return false;
148  }
149 
150  $qb = $this->handler->db2->createXoopsQueryBuilder();
151 
152  $qb ->select("COUNT(DISTINCT o.{$this->handler->keyName})")
153  ->from($this->handler->table, 'o')
154  ->leftJoin(
155  'o',
156  $this->handler->table_link,
157  'l',
158  "o.{$this->handler->field_object} = l.{$this->handler->field_link}"
159  );
160 
161  if (isset($criteria) && ($criteria instanceof CriteriaElement)) {
162  $criteria->renderQb($qb);
163  }
164 
165  $result = $qb->execute();
166  return $result->fetchColumn(0);
167 
168  }
169 
177  public function getCountsByLink(CriteriaElement $criteria = null)
178  {
179  if (!$this->validateLinks()) {
180  return false;
181  }
182 
183  $qb = $this->handler->db2->createXoopsQueryBuilder();
184 
185  $qb ->select("l.{$this->handler->keyName_link}")
186  ->addSelect('COUNT(*)')
187  ->from($this->handler->table, 'o')
188  ->leftJoin(
189  'o',
190  $this->handler->table_link,
191  'l',
192  "o.{$this->handler->field_object} = l.{$this->handler->field_link}"
193  );
194 
195  if (isset($criteria) && ($criteria instanceof CriteriaElement)) {
196  $criteria->renderQb($qb);
197  }
198 
199  $qb ->groupBy("l.{$this->handler->keyName_link}");
200 
201  $result = $qb->execute();
202 
203  $ret = array();
204  while (list ($id, $count) = $result->fetch(\PDO::FETCH_NUM)) {
205  $ret[$id] = $count;
206  }
207  return $ret;
208  }
209 
221  public function updateByLink(array $data, CriteriaElement $criteria = null)
222  {
223  if (!$this->validateLinks()) {
224  return false;
225  }
226  if (empty($data) OR empty($criteria)) { // avoid update all records
227  return false;
228  }
229 
230  $set = array();
231  foreach ($data as $key => $val) {
232  $set[] = "o.{$key}=" . $this->handler->db2->quote($val);
233  }
234  $sql = " UPDATE {$this->handler->table} AS o" . " SET " . implode(", ", $set)
235  . " LEFT JOIN {$this->handler->table_link} AS l "
236  . "ON o.{$this->handler->field_object} = l.{$this->handler->field_link}";
237  if (isset($criteria) && ($criteria instanceof CriteriaElement)) {
238  $sql .= " " . $criteria->renderWhere();
239  }
240 
241  return $this->handler->db2->executeUpdate($sql);
242  }
243 
254  public function deleteByLink(CriteriaElement $criteria = null)
255  {
256  if (!$this->validateLinks()) {
257  return false;
258  }
259  if (empty($criteria)) { //avoid delete all records
260  return false;
261  }
262 
263  $sql = "DELETE FROM {$this->handler->table} AS o "
264  . "LEFT JOIN {$this->handler->table_link} AS l "
265  . "ON o.{$this->handler->field_object} = l.{$this->handler->field_link}";
266  if (isset($criteria) && ($criteria instanceof CriteriaElement)) {
267  $sql .= " " . $criteria->renderWhere();
268  }
269 
270  return $this->handler->db2->executeUpdate($sql);
271  }
272 }
updateByLink(array $data, CriteriaElement $criteria=null)
Definition: Joint.php:221
$result
Definition: pda.php:33
getCountByLink(CriteriaElement $criteria=null)
Definition: Joint.php:144
$id
Definition: admin_menu.php:36
deleteByLink(CriteriaElement $criteria=null)
Definition: Joint.php:254
$sql
Definition: pda.php:32
$criteria
getByLink(CriteriaElement $criteria=null, $fields=null, $asObject=true, $field_link=null, $field_object=null)
Definition: Joint.php:72
getCountsByLink(CriteriaElement $criteria=null)
Definition: Joint.php:177