Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractCollection.php
Go to the documentation of this file.
1 <?php
7 
13 {
17  protected $_countSelect;
18 
22  protected $searchCriteria;
23 
30  public function setSelectCountSql(\Magento\Framework\DB\Select $countSelect)
31  {
32  $this->_countSelect = $countSelect;
33  return $this;
34  }
35 
41  public function getSelectCountSql()
42  {
43  if (!$this->_countSelect instanceof \Magento\Framework\DB\Select) {
44  $this->setSelectCountSql(parent::getSelectCountSql());
45  }
46  return $this->_countSelect;
47  }
48 
56  protected function _attributeToField($attribute)
57  {
58  $field = false;
59  if (is_string($attribute)) {
60  $field = $attribute;
61  } elseif ($attribute instanceof \Magento\Eav\Model\Entity\Attribute) {
62  $field = $attribute->getAttributeCode();
63  }
64  if (!$field) {
65  throw new \Magento\Framework\Exception\LocalizedException(__('We cannot determine the field name.'));
66  }
67  return $field;
68  }
69 
78  {
80  return $this;
81  }
82 
91  public function addAttributeToFilter($attribute, $condition = null)
92  {
93  $this->addFieldToFilter($this->_attributeToField($attribute), $condition);
94  return $this;
95  }
96 
105  public function addAttributeToSort($attribute, $dir = 'asc')
106  {
107  $this->addOrder($this->_attributeToField($attribute), $dir);
108  return $this;
109  }
110 
119  public function setPage($pageNum, $pageSize)
120  {
121  $this->setCurPage($pageNum)->setPageSize($pageSize);
122  return $this;
123  }
124 
133  protected function _getAllIdsSelect($limit = null, $offset = null)
134  {
135  $idsSelect = clone $this->getSelect();
136  $idsSelect->reset(\Magento\Framework\DB\Select::ORDER);
137  $idsSelect->reset(\Magento\Framework\DB\Select::LIMIT_COUNT);
138  $idsSelect->reset(\Magento\Framework\DB\Select::LIMIT_OFFSET);
139  $idsSelect->reset(\Magento\Framework\DB\Select::COLUMNS);
140  $idsSelect->columns($this->getResource()->getIdFieldName(), 'main_table');
141  $idsSelect->limit($limit, $offset);
142  return $idsSelect;
143  }
144 
153  public function getAllIds($limit = null, $offset = null)
154  {
155  return $this->getConnection()->fetchCol($this->_getAllIdsSelect($limit, $offset), $this->_bindParams);
156  }
157 
163  public function getSearchCriteria()
164  {
165  return $this->searchCriteria;
166  }
167 
175  public function setSearchCriteria(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria = null)
176  {
177  $this->searchCriteria = $searchCriteria;
178  return $this;
179  }
180 
186  public function getTotalCount()
187  {
188  return $this->getSize();
189  }
190 
198  public function setTotalCount($totalCount)
199  {
200  return $this;
201  }
202 
209  public function setItems(array $items = null)
210  {
211  if (!$items) {
212  return $this;
213  }
214  foreach ($items as $item) {
215  $this->addItem($item);
216  }
217  return $this;
218  }
219 }
const ORDER
Definition: Select.php:54
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__()
Definition: __.php:13
setSelectCountSql(\Magento\Framework\DB\Select $countSelect)
const COLUMNS
Definition: Select.php:48
const LIMIT_OFFSET
Definition: Select.php:56
setSearchCriteria(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria=null)
addOrder($field, $direction=self::SORT_ORDER_DESC)
Definition: AbstractDb.php:286
const LIMIT_COUNT
Definition: Select.php:55
addItem(\Magento\Framework\DataObject $item)
Definition: Collection.php:399
$items