Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Collection.php
Go to the documentation of this file.
1 <?php
7 
9 
15 {
21  protected $_columnGroupBy = null;
22 
28  protected $_resourceCollection = null;
29 
37  public function setColumnGroupBy($column)
38  {
39  $this->_columnGroupBy = (string)$column;
40  return $this;
41  }
42 
50  public function load($printQuery = false, $logQuery = false)
51  {
52  if ($this->isLoaded()) {
53  return $this;
54  }
55 
56  parent::load($printQuery, $logQuery);
57  $this->_setIsLoaded();
58 
59  if ($this->_columnGroupBy !== null) {
60  $this->_mergeWithEmptyData();
61  $this->_groupResourceData();
62  }
63 
64  return $this;
65  }
66 
75  {
76  $this->_resourceCollection = $collection;
77  return $this;
78  }
79 
85  protected function _mergeWithEmptyData()
86  {
87  if (count($this->_items) == 0) {
88  return $this;
89  }
90 
91  foreach ($this->_items as $key => $item) {
92  foreach ($this->_resourceCollection as $dataItem) {
93  if ($item->getData($this->_columnGroupBy) == $dataItem->getData($this->_columnGroupBy)) {
94  if ($this->_items[$key]->getIsEmpty()) {
95  $this->_items[$key] = $dataItem;
96  } else {
97  $this->_items[$key]->addChild($dataItem);
98  }
99  }
100  }
101  }
102 
103  return $this;
104  }
105 
111  protected function _groupResourceData()
112  {
113  if (count($this->_items) == 0) {
114  foreach ($this->_resourceCollection as $item) {
115  if (isset($this->_items[$item->getData($this->_columnGroupBy)])) {
116  $this->_items[$item->getData($this->_columnGroupBy)]->addChild($item->setIsEmpty(false));
117  } else {
118  $this->_items[$item->getData($this->_columnGroupBy)] = $item->setIsEmpty(false);
119  }
120  }
121  }
122 
123  return $this;
124  }
125 }
load($printQuery=false, $logQuery=false)
Definition: Collection.php:50