AbstractCollection.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Model\ResourceModel;
  7. /**
  8. * Flat abstract collection
  9. *
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. */
  12. abstract class AbstractCollection extends \Magento\Framework\Model\ResourceModel\Db\VersionControl\Collection
  13. {
  14. /**
  15. * @var \Magento\Framework\DB\Select
  16. */
  17. protected $_countSelect;
  18. /**
  19. * @var \Magento\Framework\Api\SearchCriteriaInterface
  20. */
  21. protected $searchCriteria;
  22. /**
  23. * Set select count sql
  24. *
  25. * @param \Magento\Framework\DB\Select $countSelect
  26. * @return $this
  27. */
  28. public function setSelectCountSql(\Magento\Framework\DB\Select $countSelect)
  29. {
  30. $this->_countSelect = $countSelect;
  31. return $this;
  32. }
  33. /**
  34. * Get select count sql
  35. *
  36. * @return \Magento\Framework\DB\Select
  37. */
  38. public function getSelectCountSql()
  39. {
  40. if (!$this->_countSelect instanceof \Magento\Framework\DB\Select) {
  41. $this->setSelectCountSql(parent::getSelectCountSql());
  42. }
  43. return $this->_countSelect;
  44. }
  45. /**
  46. * Check if $attribute is \Magento\Eav\Model\Entity\Attribute and convert to string field name
  47. *
  48. * @param string|\Magento\Eav\Model\Entity\Attribute $attribute
  49. * @return string
  50. * @throws \Magento\Framework\Exception\LocalizedException
  51. */
  52. protected function _attributeToField($attribute)
  53. {
  54. $field = false;
  55. if (is_string($attribute)) {
  56. $field = $attribute;
  57. } elseif ($attribute instanceof \Magento\Eav\Model\Entity\Attribute) {
  58. $field = $attribute->getAttributeCode();
  59. }
  60. if (!$field) {
  61. throw new \Magento\Framework\Exception\LocalizedException(__('We cannot determine the field name.'));
  62. }
  63. return $field;
  64. }
  65. /**
  66. * Add attribute to select result set.
  67. *
  68. * Backward compatibility with EAV collection
  69. *
  70. * @param string $attribute
  71. * @return $this
  72. */
  73. public function addAttributeToSelect($attribute)
  74. {
  75. $this->addFieldToSelect($this->_attributeToField($attribute));
  76. return $this;
  77. }
  78. /**
  79. * Specify collection select filter by attribute value
  80. *
  81. * Backward compatibility with EAV collection
  82. *
  83. * @param string|\Magento\Eav\Model\Entity\Attribute $attribute
  84. * @param array|int|string|null $condition
  85. * @return $this
  86. */
  87. public function addAttributeToFilter($attribute, $condition = null)
  88. {
  89. $this->addFieldToFilter($this->_attributeToField($attribute), $condition);
  90. return $this;
  91. }
  92. /**
  93. * Specify collection select order by attribute value
  94. *
  95. * Backward compatibility with EAV collection
  96. *
  97. * @param string $attribute
  98. * @param string $dir
  99. * @return $this
  100. */
  101. public function addAttributeToSort($attribute, $dir = 'asc')
  102. {
  103. $this->addOrder($this->_attributeToField($attribute), $dir);
  104. return $this;
  105. }
  106. /**
  107. * Set collection page start and records to show
  108. *
  109. * Backward compatibility with EAV collection
  110. *
  111. * @param int $pageNum
  112. * @param int $pageSize
  113. * @return $this
  114. */
  115. public function setPage($pageNum, $pageSize)
  116. {
  117. $this->setCurPage($pageNum)->setPageSize($pageSize);
  118. return $this;
  119. }
  120. /**
  121. * Create all ids retrieving select with limitation
  122. *
  123. * Backward compatibility with EAV collection
  124. *
  125. * @param int $limit
  126. * @param int $offset
  127. * @return \Magento\Framework\DB\Select
  128. */
  129. protected function _getAllIdsSelect($limit = null, $offset = null)
  130. {
  131. $idsSelect = clone $this->getSelect();
  132. $idsSelect->reset(\Magento\Framework\DB\Select::ORDER);
  133. $idsSelect->reset(\Magento\Framework\DB\Select::LIMIT_COUNT);
  134. $idsSelect->reset(\Magento\Framework\DB\Select::LIMIT_OFFSET);
  135. $idsSelect->reset(\Magento\Framework\DB\Select::COLUMNS);
  136. $idsSelect->columns($this->getResource()->getIdFieldName(), 'main_table');
  137. $idsSelect->limit($limit, $offset);
  138. return $idsSelect;
  139. }
  140. /**
  141. * Retrieve all ids for collection
  142. *
  143. * Backward compatibility with EAV collection
  144. *
  145. * @param int $limit
  146. * @param int $offset
  147. * @return array
  148. */
  149. public function getAllIds($limit = null, $offset = null)
  150. {
  151. return $this->getConnection()->fetchCol($this->_getAllIdsSelect($limit, $offset), $this->_bindParams);
  152. }
  153. /**
  154. * Get search criteria.
  155. *
  156. * @return \Magento\Framework\Api\SearchCriteriaInterface|null
  157. */
  158. public function getSearchCriteria()
  159. {
  160. return $this->searchCriteria;
  161. }
  162. /**
  163. * Set search criteria.
  164. *
  165. * @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
  166. * @return $this
  167. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  168. */
  169. public function setSearchCriteria(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria = null)
  170. {
  171. $this->searchCriteria = $searchCriteria;
  172. return $this;
  173. }
  174. /**
  175. * Get total count.
  176. *
  177. * @return int
  178. */
  179. public function getTotalCount()
  180. {
  181. return $this->getSize();
  182. }
  183. /**
  184. * Set total count.
  185. *
  186. * @param int $totalCount
  187. * @return $this
  188. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  189. */
  190. public function setTotalCount($totalCount)
  191. {
  192. return $this;
  193. }
  194. /**
  195. * Set items list.
  196. *
  197. * @param \Magento\Framework\Api\ExtensibleDataInterface[] $items
  198. * @return $this
  199. */
  200. public function setItems(array $items = null)
  201. {
  202. if (!$items) {
  203. return $this;
  204. }
  205. foreach ($items as $item) {
  206. $this->addItem($item);
  207. }
  208. return $this;
  209. }
  210. }