SearchResult.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Element\UiComponent\DataProvider;
  7. use Magento\Framework\Api;
  8. use Magento\Framework\Data\Collection\Db\FetchStrategyInterface as FetchStrategy;
  9. use Magento\Framework\Data\Collection\EntityFactoryInterface as EntityFactory;
  10. use Magento\Framework\Event\ManagerInterface as EventManager;
  11. use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
  12. use Psr\Log\LoggerInterface as Logger;
  13. use Magento\Framework\App\ResourceConnection;
  14. use Magento\Framework\App\ObjectManager;
  15. /**
  16. * Class SearchResult
  17. * Generic Search Result
  18. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  19. */
  20. class SearchResult extends AbstractCollection implements Api\Search\SearchResultInterface
  21. {
  22. /**
  23. * @var Api\Search\AggregationInterface
  24. */
  25. protected $aggregations;
  26. /**
  27. * @var Api\Search\SearchCriteriaInterface
  28. */
  29. protected $searchCriteria;
  30. /**
  31. * @var int
  32. */
  33. protected $totalCount;
  34. /**
  35. * @var string class name of document
  36. */
  37. protected $document = Document::class;
  38. /**
  39. * @var ResourceConnection
  40. */
  41. private $resourceConnection;
  42. /**
  43. * @var string
  44. */
  45. private $identifierName;
  46. /**
  47. * SearchResult constructor.
  48. * @param EntityFactory $entityFactory
  49. * @param Logger $logger
  50. * @param FetchStrategy $fetchStrategy
  51. * @param EventManager $eventManager
  52. * @param string $mainTable
  53. * @param null|string $resourceModel
  54. * @param null|string $identifierName
  55. * @param null|string $connectionName
  56. * @throws \Magento\Framework\Exception\LocalizedException
  57. */
  58. public function __construct(
  59. EntityFactory $entityFactory,
  60. Logger $logger,
  61. FetchStrategy $fetchStrategy,
  62. EventManager $eventManager,
  63. $mainTable,
  64. $resourceModel = null,
  65. $identifierName = null,
  66. $connectionName = null
  67. ) {
  68. $this->_init($this->document, $resourceModel);
  69. $this->setMainTable(true);
  70. if ($connectionName) {
  71. $connection = $this->getResourceConnection()->getConnectionByName($connectionName);
  72. } else {
  73. $connection = $this->getResourceConnection()->getConnection();
  74. }
  75. $this->setMainTable($this->getResourceConnection()->getTableName($mainTable));
  76. $this->identifierName = $identifierName;
  77. parent::__construct(
  78. $entityFactory,
  79. $logger,
  80. $fetchStrategy,
  81. $eventManager,
  82. $connection,
  83. null
  84. );
  85. $this->_setIdFieldName($this->getIdentifierName());
  86. }
  87. /**
  88. * @deprecated 101.0.0
  89. * @return ResourceConnection
  90. */
  91. private function getResourceConnection()
  92. {
  93. if ($this->resourceConnection == null) {
  94. $this->resourceConnection = ObjectManager::getInstance()->get(ResourceConnection::class);
  95. }
  96. return $this->resourceConnection;
  97. }
  98. /**
  99. * @return \Magento\Framework\Api\Search\AggregationInterface
  100. */
  101. public function getAggregations()
  102. {
  103. return $this->aggregations;
  104. }
  105. /**
  106. * Get resource instance
  107. *
  108. * @return ResourceConnection|\Magento\Framework\Model\ResourceModel\Db\AbstractDb
  109. */
  110. public function getResource()
  111. {
  112. if ($this->_resourceModel) {
  113. return parent::getResource();
  114. }
  115. return $this->getResourceConnection();
  116. }
  117. /**
  118. * @param \Magento\Framework\Api\Search\AggregationInterface $aggregations
  119. * @return void
  120. */
  121. public function setAggregations($aggregations)
  122. {
  123. $this->aggregations = $aggregations;
  124. }
  125. /**
  126. * @return \Magento\Framework\Api\Search\SearchCriteriaInterface|null
  127. */
  128. public function getSearchCriteria()
  129. {
  130. return $this->searchCriteria;
  131. }
  132. /**
  133. * @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
  134. * @return $this
  135. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  136. */
  137. public function setSearchCriteria(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
  138. {
  139. $this->searchCriteria = $searchCriteria;
  140. return $this;
  141. }
  142. /**
  143. * @return int
  144. */
  145. public function getTotalCount()
  146. {
  147. if (!$this->totalCount) {
  148. $this->totalCount = $this->getSize();
  149. }
  150. return $this->totalCount;
  151. }
  152. /**
  153. * @param int $totalCount
  154. * @return $this
  155. */
  156. public function setTotalCount($totalCount)
  157. {
  158. $this->totalCount = $totalCount;
  159. return $this;
  160. }
  161. /**
  162. * Set items list.
  163. *
  164. * @param Document[] $items
  165. * @return $this
  166. */
  167. public function setItems(array $items = null)
  168. {
  169. if ($items) {
  170. foreach ($items as $item) {
  171. $this->addItem($item);
  172. }
  173. unset($this->totalCount);
  174. }
  175. return $this;
  176. }
  177. /**
  178. * Retrieve table name
  179. *
  180. * @param string $table
  181. * @return string
  182. */
  183. public function getTable($table)
  184. {
  185. return $this->getResourceConnection()->getTableName($table);
  186. }
  187. /**
  188. * Get Identifier name
  189. *
  190. * @return string|bool
  191. * @throws \Magento\Framework\Exception\LocalizedException
  192. */
  193. private function getIdentifierName()
  194. {
  195. if ($this->_resourceModel) {
  196. return $this->getResource()->getIdFieldName();
  197. } elseif ($this->identifierName !== null) {
  198. return $this->identifierName;
  199. } else {
  200. return $this->getResourceConnection()->getConnection()->getAutoIncrementField($this->getMainTable());
  201. }
  202. }
  203. /**
  204. * Initialize initial fields to select like id field
  205. *
  206. * @return $this
  207. */
  208. protected function _initInitialFieldsToSelect()
  209. {
  210. $idFieldName = $this->getIdentifierName();
  211. if ($idFieldName) {
  212. $this->_initialFieldsToSelect[] = $idFieldName;
  213. }
  214. return $this;
  215. }
  216. /**
  217. * Retrieve all ids for collection
  218. *
  219. * @return array
  220. */
  221. public function getAllIds()
  222. {
  223. $idsSelect = clone $this->getSelect();
  224. $idsSelect->reset(\Magento\Framework\DB\Select::ORDER);
  225. $idsSelect->reset(\Magento\Framework\DB\Select::LIMIT_COUNT);
  226. $idsSelect->reset(\Magento\Framework\DB\Select::LIMIT_OFFSET);
  227. $idsSelect->reset(\Magento\Framework\DB\Select::COLUMNS);
  228. $idsSelect->columns($this->getIdentifierName(), 'main_table');
  229. return $this->getConnection()->fetchCol($idsSelect, $this->_bindParams);
  230. }
  231. }