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 
16 {
20  protected $_countSelect;
21 
25  protected $searchCriteria;
26 
33  public function setSelectCountSql(\Magento\Framework\DB\Select $countSelect)
34  {
35  $this->_countSelect = $countSelect;
36  return $this;
37  }
38 
44  public function getSelectCountSql()
45  {
46  if (!$this->_countSelect instanceof \Magento\Framework\DB\Select) {
47  $this->setSelectCountSql(parent::getSelectCountSql());
48  }
49  return $this->_countSelect;
50  }
51 
59  protected function _attributeToField($attribute)
60  {
61  $field = false;
62  if (is_string($attribute)) {
63  $field = $attribute;
64  } elseif ($attribute instanceof \Magento\Eav\Model\Entity\Attribute) {
65  $field = $attribute->getAttributeCode();
66  }
67  if (!$field) {
68  throw new \Magento\Framework\Exception\LocalizedException(__('We cannot determine the field name.'));
69  }
70  return $field;
71  }
72 
81  {
83  return $this;
84  }
85 
94  public function addAttributeToFilter($attribute, $condition = null)
95  {
96  $this->addFieldToFilter($this->_attributeToField($attribute), $condition);
97  return $this;
98  }
99 
108  public function addAttributeToSort($attribute, $dir = 'asc')
109  {
110  $this->addOrder($this->_attributeToField($attribute), $dir);
111  return $this;
112  }
113 
122  public function setPage($pageNum, $pageSize)
123  {
124  $this->setCurPage($pageNum)->setPageSize($pageSize);
125  return $this;
126  }
127 
136  protected function _getAllIdsSelect($limit = null, $offset = null)
137  {
138  $idsSelect = clone $this->getSelect();
139  $idsSelect->reset(\Magento\Framework\DB\Select::ORDER);
140  $idsSelect->reset(\Magento\Framework\DB\Select::LIMIT_COUNT);
141  $idsSelect->reset(\Magento\Framework\DB\Select::LIMIT_OFFSET);
142  $idsSelect->reset(\Magento\Framework\DB\Select::COLUMNS);
143  $idsSelect->columns($this->getResource()->getIdFieldName(), 'main_table');
144  $idsSelect->limit($limit, $offset);
145  return $idsSelect;
146  }
147 
156  public function getAllIds($limit = null, $offset = null)
157  {
158  return $this->getConnection()->fetchCol($this->_getAllIdsSelect($limit, $offset), $this->_bindParams);
159  }
160 
166  public function getSearchCriteria()
167  {
168  return $this->searchCriteria;
169  }
170 
178  public function setSearchCriteria(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria = null)
179  {
180  $this->searchCriteria = $searchCriteria;
181  return $this;
182  }
183 
189  public function getTotalCount()
190  {
191  return $this->getSize();
192  }
193 
201  public function setTotalCount($totalCount)
202  {
203  return $this;
204  }
205 
212  public function setItems(array $items = null)
213  {
214  if (!$items) {
215  return $this;
216  }
217  foreach ($items as $item) {
218  $this->addItem($item);
219  }
220  return $this;
221  }
222 }
setSearchCriteria(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria=null)
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
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