Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractSearchResult.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Framework\Data;
7 
12 {
18  protected $dataInterface = \Magento\Framework\DataObject::class;
19 
25  protected $eventPrefix = '';
26 
32  protected $eventObject = '';
33 
39  protected $eventManager = null;
40 
46  protected $totalRecords;
47 
53  protected $isLoaded;
54 
58  protected $entityFactory;
59 
63  protected $query;
64 
69  protected $select;
70 
75 
82  public function __construct(
83  \Magento\Framework\DB\QueryInterface $query,
85  \Magento\Framework\Event\ManagerInterface $eventManager,
87  ) {
88  $this->query = $query;
89  $this->eventManager = $eventManager;
90  $this->entityFactory = $entityFactory;
91  $this->resultIteratorFactory = $resultIteratorFactory;
92  $this->init();
93  }
94 
100  abstract protected function init();
101 
105  public function getItems()
106  {
107  $this->load();
108  return $this->data['items'];
109  }
110 
115  public function setItems(array $items = null)
116  {
117  $this->data['items'] = $items;
118  return $this;
119  }
120 
124  public function getTotalCount()
125  {
126  if (!isset($this->data['total_count'])) {
127  $this->data['total_count'] = $this->query->getSize();
128  }
129  return $this->data['total_count'];
130  }
131 
136  public function setTotalCount($totalCount)
137  {
138  $this->data['total_count'] = $totalCount;
139  return $this;
140  }
141 
145  public function getSearchCriteria()
146  {
147  if (!isset($this->data['search_criteria'])) {
148  $this->data['search_criteria'] = $this->query->getCriteria();
149  }
150  return $this->data['search_criteria'];
151  }
152 
158  public function setSearchCriteria(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria = null)
159  {
160  return $this;
161  }
162 
166  public function createIterator()
167  {
168  return $this->resultIteratorFactory->create(
169  [
170  'searchResult' => $this,
171  'query' => $this->query,
172  ]
173  );
174  }
175 
180  public function createDataObject(array $arguments = [])
181  {
182  return $this->entityFactory->create($this->getDataInterfaceName(), $arguments);
183  }
184 
188  public function getIdFieldName()
189  {
190  return $this->query->getIdFieldName();
191  }
192 
196  public function getSize()
197  {
198  return $this->query->getSize();
199  }
200 
207  public function getItemId(\Magento\Framework\DataObject $item)
208  {
209  $field = $this->query->getIdFieldName();
210  if ($field) {
211  return $item->getData($field);
212  }
213  return $item->getId();
214  }
215 
219  protected function isLoaded()
220  {
221  return $this->isLoaded;
222  }
223 
229  protected function load()
230  {
231  if (!$this->isLoaded()) {
232  $this->beforeLoad();
233  $data = $this->query->fetchAll();
234  $this->data['items'] = [];
235  if (is_array($data)) {
236  foreach ($data as $row) {
237  $item = $this->createDataObject(['data' => $row]);
238  $this->addItem($item);
239  }
240  }
241  $this->setIsLoaded(true);
242  $this->afterLoad();
243  }
244  }
245 
252  protected function setIsLoaded($flag = true)
253  {
254  $this->isLoaded = $flag;
255  }
256 
264  protected function addItem(\Magento\Framework\DataObject $item)
265  {
266  $itemId = $this->getItemId($item);
267  if ($itemId !== null) {
268  if (isset($this->data['items'][$itemId])) {
269  throw new \Exception(
270  'Item (' . get_class($item) . ') with the same ID "' . $item->getId() . '" already exists.'
271  );
272  }
273  $this->data['items'][$itemId] = $item;
274  } else {
275  $this->data['items'][] = $item;
276  }
277  }
278 
284  protected function beforeLoad()
285  {
286  $this->eventManager->dispatch('abstract_search_result_load_before', ['collection' => $this]);
287  if ($this->eventPrefix && $this->eventObject) {
288  $this->eventManager->dispatch($this->eventPrefix . '_load_before', [$this->eventObject => $this]);
289  }
290  }
291 
297  protected function afterLoad()
298  {
299  $this->eventManager->dispatch('abstract_search_result_load_after', ['collection' => $this]);
300  if ($this->eventPrefix && $this->eventObject) {
301  $this->eventManager->dispatch($this->eventPrefix . '_load_after', [$this->eventObject => $this]);
302  }
303  }
304 
312  {
313  if (is_string($dataInterface)) {
314  $this->dataInterface = $dataInterface;
315  }
316  }
317 
323  protected function getDataInterfaceName()
324  {
325  return $this->dataInterface;
326  }
327 
331  protected function getQuery()
332  {
333  return $this->query;
334  }
335 }
getItemId(\Magento\Framework\DataObject $item)
setSearchCriteria(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria=null)
addItem(\Magento\Framework\DataObject $item)
$searchCriteria
$arguments
__construct(\Magento\Framework\DB\QueryInterface $query, \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory, \Magento\Framework\Event\ManagerInterface $eventManager, \Magento\Framework\Data\SearchResultIteratorFactory $resultIteratorFactory)
$items