Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
Query Class Reference
Inheritance diagram for Query:
QueryInterface

Public Member Functions

 __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)
 
 getCriteria ()
 
 getAllIds ()
 
 addBindParam ($name, $value)
 
 getSize ()
 
 getSelectSql ($stringMode=false)
 
 reset ()
 
 fetchAll ()
 
 fetchItem ()
 
 getIdFieldName ()
 
 getConnection ()
 
 getResource ()
 
 addCountSqlSkipPart ($name, $toSkip=true)
 

Protected Member Functions

 getSelectCountSql ()
 
 getCountSqlSkipParts ()
 
 getSelect ()
 

Protected Attributes

 $select
 
 $criteria
 
 $resource
 
 $fetchStmt = null
 
 $logger
 
 $bindParams = []
 
 $totalRecords
 
 $data
 
 $countSqlSkipParts
 

Detailed Description

Class Query

Definition at line 13 of file Query.php.

Constructor & Destructor Documentation

◆ __construct()

Parameters
\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 at line 84 of file Query.php.

89  {
90  $this->select = $select;
91  $this->criteria = $criteria;
92  $this->resource = $resource;
93  $this->fetchStrategy = $fetchStrategy;
94  }

Member Function Documentation

◆ addBindParam()

addBindParam (   $name,
  $value 
)

Add variable to bind list

Parameters
string$name
mixed$value
Returns
void

Implements QueryInterface.

Definition at line 129 of file Query.php.

130  {
131  $this->bindParams[$name] = $value;
132  }
$value
Definition: gender.phtml:16
if(!isset($_GET['name'])) $name
Definition: log.php:14

◆ addCountSqlSkipPart()

addCountSqlSkipPart (   $name,
  $toSkip = true 
)

Add Select Part to skip from count query

Parameters
string$name
bool$toSkip
Returns
void

Implements QueryInterface.

Definition at line 241 of file Query.php.

242  {
243  $this->countSqlSkipParts[$name] = $toSkip;
244  }
if(!isset($_GET['name'])) $name
Definition: log.php:14

◆ fetchAll()

fetchAll ( )

Fetch all statement

Returns
array

Implements QueryInterface.

Definition at line 178 of file Query.php.

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  }

◆ fetchItem()

fetchItem ( )

Fetch statement

Returns
mixed

Implements QueryInterface.

Definition at line 192 of file Query.php.

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  }

◆ getAllIds()

getAllIds ( )

Retrieve all ids for query

Returns
array

Implements QueryInterface.

Definition at line 111 of file Query.php.

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  }
const ORDER
Definition: Select.php:54
const COLUMNS
Definition: Select.php:48
const LIMIT_OFFSET
Definition: Select.php:56
const LIMIT_COUNT
Definition: Select.php:55

◆ getConnection()

getConnection ( )

Retrieve connection object

Returns
\Magento\Framework\DB\Adapter\AdapterInterface

Implements QueryInterface.

Definition at line 219 of file Query.php.

220  {
221  return $this->getSelect()->getConnection();
222  }

◆ getCountSqlSkipParts()

getCountSqlSkipParts ( )
protected

Returned count SQL skip parts

Returns
array

Definition at line 269 of file Query.php.

270  {
272  }

◆ getCriteria()

getCriteria ( )

Retrieve source Criteria object

Returns
\Magento\Framework\Api\CriteriaInterface

Implements QueryInterface.

Definition at line 101 of file Query.php.

102  {
103  return $this->criteria;
104  }

◆ getIdFieldName()

getIdFieldName ( )

Get Identity Field Name

Returns
string

Implements QueryInterface.

Definition at line 209 of file Query.php.

210  {
211  return $this->getResource()->getIdFieldName();
212  }

◆ getResource()

getResource ( )

Get resource instance

Returns
\Magento\Framework\Model\ResourceModel\Db\AbstractDb

Implements QueryInterface.

Definition at line 229 of file Query.php.

230  {
231  return $this->resource;
232  }

◆ getSelect()

getSelect ( )
protected

Get \Magento\Framework\DB\Select object instance

Returns
Select

Definition at line 279 of file Query.php.

280  {
281  return $this->select;
282  }

◆ getSelectCountSql()

getSelectCountSql ( )
protected

Get SQL for get record count

Returns
Select

Definition at line 251 of file Query.php.

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  }

◆ getSelectSql()

getSelectSql (   $stringMode = false)

Get sql select string or object

Parameters
bool$stringMode
Returns
string || Select

Implements QueryInterface.

Definition at line 154 of file Query.php.

155  {
156  if ($stringMode) {
157  return $this->select->__toString();
158  }
159  return $this->select;
160  }

◆ getSize()

getSize ( )

Get collection size

Returns
int

Implements QueryInterface.

Definition at line 139 of file Query.php.

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  }

◆ reset()

reset ( )

Reset Statement object

Returns
void

Implements QueryInterface.

Definition at line 167 of file Query.php.

168  {
169  $this->fetchStmt = null;
170  $this->data = null;
171  }

Field Documentation

◆ $bindParams

$bindParams = []
protected

Definition at line 54 of file Query.php.

◆ $countSqlSkipParts

$countSqlSkipParts
protected

◆ $criteria

$criteria
protected

Definition at line 25 of file Query.php.

◆ $data

$data
protected

Definition at line 64 of file Query.php.

◆ $fetchStmt

$fetchStmt = null
protected

Definition at line 39 of file Query.php.

◆ $logger

$logger
protected

Definition at line 44 of file Query.php.

◆ $resource

$resource
protected

Definition at line 32 of file Query.php.

◆ $select

$select
protected

Definition at line 20 of file Query.php.

◆ $totalRecords

$totalRecords
protected

Definition at line 59 of file Query.php.


The documentation for this class was generated from the following file: