Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Query.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Framework\DB;
7 
8 use Psr\Log\LoggerInterface as Logger;
9 
13 class Query implements QueryInterface
14 {
20  protected $select;
21 
25  protected $criteria;
26 
32  protected $resource;
33 
39  protected $fetchStmt = null;
40 
44  protected $logger;
45 
49  private $fetchStrategy;
50 
54  protected $bindParams = [];
55 
59  protected $totalRecords;
60 
64  protected $data;
65 
71  protected $countSqlSkipParts = [
76  ];
77 
84  public function __construct(
85  \Magento\Framework\DB\Select $select,
86  \Magento\Framework\Api\CriteriaInterface $criteria,
87  \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource,
88  \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
89  ) {
90  $this->select = $select;
91  $this->criteria = $criteria;
92  $this->resource = $resource;
93  $this->fetchStrategy = $fetchStrategy;
94  }
95 
101  public function getCriteria()
102  {
103  return $this->criteria;
104  }
105 
111  public function getAllIds()
112  {
113  $idsSelect = clone $this->getSelect();
114  $idsSelect->reset(\Magento\Framework\DB\Select::ORDER);
115  $idsSelect->reset(\Magento\Framework\DB\Select::LIMIT_COUNT);
116  $idsSelect->reset(\Magento\Framework\DB\Select::LIMIT_OFFSET);
117  $idsSelect->reset(\Magento\Framework\DB\Select::COLUMNS);
118  $idsSelect->columns($this->getResource()->getIdFieldName(), 'main_table');
119  return $this->getConnection()->fetchCol($idsSelect, $this->bindParams);
120  }
121 
129  public function addBindParam($name, $value)
130  {
131  $this->bindParams[$name] = $value;
132  }
133 
139  public function getSize()
140  {
141  if ($this->totalRecords === null) {
142  $sql = $this->getSelectCountSql();
143  $this->totalRecords = $this->getConnection()->fetchOne($sql, $this->bindParams);
144  }
145  return intval($this->totalRecords);
146  }
147 
154  public function getSelectSql($stringMode = false)
155  {
156  if ($stringMode) {
157  return $this->select->__toString();
158  }
159  return $this->select;
160  }
161 
167  public function reset()
168  {
169  $this->fetchStmt = null;
170  $this->data = null;
171  }
172 
178  public function fetchAll()
179  {
180  if ($this->data === null) {
181  $select = $this->getSelect();
182  $this->data = $this->fetchStrategy->fetchAll($select, $this->bindParams);
183  }
184  return $this->data;
185  }
186 
192  public function fetchItem()
193  {
194  if (null === $this->fetchStmt) {
195  $this->fetchStmt = $this->getConnection()->query($this->getSelect(), $this->bindParams);
196  }
197  $data = $this->fetchStmt->fetch();
198  if (!$data) {
199  $data = [];
200  }
201  return $data;
202  }
203 
209  public function getIdFieldName()
210  {
211  return $this->getResource()->getIdFieldName();
212  }
213 
219  public function getConnection()
220  {
221  return $this->getSelect()->getConnection();
222  }
223 
229  public function getResource()
230  {
231  return $this->resource;
232  }
233 
241  public function addCountSqlSkipPart($name, $toSkip = true)
242  {
243  $this->countSqlSkipParts[$name] = $toSkip;
244  }
245 
251  protected function getSelectCountSql()
252  {
253  $countSelect = clone $this->getSelect();
254  foreach ($this->getCountSqlSkipParts() as $part => $toSkip) {
255  if ($toSkip) {
256  $countSelect->reset($part);
257  }
258  }
259  $countSelect->columns('COUNT(*)');
260 
261  return $countSelect;
262  }
263 
269  protected function getCountSqlSkipParts()
270  {
272  }
273 
279  protected function getSelect()
280  {
281  return $this->select;
282  }
283 }
const ORDER
Definition: Select.php:54
__construct(\Magento\Framework\DB\Select $select, \Magento\Framework\Api\CriteriaInterface $criteria, \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource, \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy)
Definition: Query.php:84
getSelectSql($stringMode=false)
Definition: Query.php:154
$value
Definition: gender.phtml:16
const COLUMNS
Definition: Select.php:48
const LIMIT_OFFSET
Definition: Select.php:56
addCountSqlSkipPart($name, $toSkip=true)
Definition: Query.php:241
const LIMIT_COUNT
Definition: Select.php:55
addBindParam($name, $value)
Definition: Query.php:129
if(!isset($_GET['name'])) $name
Definition: log.php:14