XOOPS  2.6.0
Read.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 
12 namespace Xoops\Core\Kernel\Model;
13 
16 
28 class Read extends XoopsModelAbstract
29 {
40  public function getAll(CriteriaElement $criteria = null, $fields = null, $asObject = true, $id_as_key = true)
41  {
42  //$qb = Xoops::getInstance()->db()->createXoopsQueryBuilder();
43  $qb = $this->handler->db2->createXoopsQueryBuilder();
44  $eb = $qb->expr();
45 
46  if (is_array($fields) && count($fields) > 0) {
47  if (!in_array($this->handler->keyName, $fields)) {
48  $fields[] = $this->handler->keyName;
49  }
50  $first=true;
51  foreach ($fields as $field) {
52  if ($first) {
53  $first=false;
54  $qb->select($field);
55  } else {
56  $qb->addSelect($field);
57  }
58  }
59  } else {
60  $qb->select('*');
61  }
62  $qb->from($this->handler->table, null);
63  if (isset($criteria)) {
64  $qb = $criteria->renderQb($qb);
65  }
66 
67  $ret = array();
68  $result = $qb->execute();
69  if (!$result) {
70  return $ret;
71  }
72  if ($asObject) {
73  while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) {
74  $object = $this->handler->create(false);
75  $object->assignVars($myrow);
76  if ($id_as_key) {
77  $ret[$myrow[$this->handler->keyName]] = $object;
78  } else {
79  $ret[] = $object;
80  }
81  unset($object);
82  }
83  } else {
84  $object = $this->handler->create(false);
85  while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) {
86  $object->assignVars($myrow);
87  if ($id_as_key) {
88  $ret[$myrow[$this->handler->keyName]] = $object->getValues();
89  } else {
90  $ret[] = $object->getValues();
91  }
92  }
93  unset($object);
94  }
95  return $ret;
96  }
97 
109  public function getObjects(CriteriaElement $criteria = null, $id_as_key = false, $as_object = true)
110  {
111  $objects = $this->getAll($criteria, null, $as_object, $id_as_key);
112  return $objects;
113  }
114 
124  public function getList(CriteriaElement $criteria = null, $limit = 0, $start = 0)
125  {
126  //$qb = Xoops::getInstance()->db()->createXoopsQueryBuilder();
127  $qb = $this->handler->db2->createXoopsQueryBuilder();
128  $eb = $qb->expr();
129 
130  $ret = array();
131 
132  $qb->select($this->handler->keyName);
133  if (!empty($this->handler->identifierName)) {
134  $qb->addSelect($this->handler->identifierName);
135  }
136  $qb->from($this->handler->table, null);
137  $qb->orderBy($this->handler->keyName); // any criteria order will override
138  if (!empty($criteria)) {
139  $qb = $criteria->renderQb($qb);
140  }
141  $result = $qb->execute();
142  if (!$result) {
143  return $ret;
144  }
145 
147  while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) {
148  // identifiers should be textboxes, so sanitize them like that
149  $ret[$myrow[$this->handler->keyName]] = empty($this->handler->identifierName) ? 1
150  : $myts->htmlSpecialChars($myrow[$this->handler->identifierName]);
151  }
152  return $ret;
153  }
154 
162  public function getIds(CriteriaElement $criteria = null)
163  {
164  //$qb = Xoops::getInstance()->db()->createXoopsQueryBuilder();
165  $qb = $this->handler->db2->createXoopsQueryBuilder();
166  $eb = $qb->expr();
167 
168  $ret = array();
169 
170  $qb->select($this->handler->keyName);
171  $qb->from($this->handler->table, null);
172  $sql = "SELECT `{$this->handler->keyName}` FROM `{$this->handler->table}`";
173  $limit = $start = null;
174  if (!empty($criteria)) {
175  $qb = $criteria->renderQb($qb);
176  }
177  $result = $qb->execute();
178  if (!$result) {
179  return $ret;
180  }
181 
182  while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) {
183  $ret[] = $myrow[$this->handler->keyName];
184  }
185  return $ret;
186  }
187 }
getIds(CriteriaElement $criteria=null)
Definition: Read.php:162
$result
Definition: pda.php:33
getAll(CriteriaElement $criteria=null, $fields=null, $asObject=true, $id_as_key=true)
Definition: Read.php:40
$sql
Definition: pda.php:32
$limit
Definition: findusers.php:202
$criteria
$start
$myts
Definition: edituser.php:38
getObjects(CriteriaElement $criteria=null, $id_as_key=false, $as_object=true)
Definition: Read.php:109
getList(CriteriaElement $criteria=null, $limit=0, $start=0)
Definition: Read.php:124