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 
15 {
21  protected $_map = ['fields' => ['website_id' => 'main_table.website_id']];
22 
28  protected function _construct()
29  {
30  $this->setFlag('load_default_website', false);
31  $this->_init(\Magento\Store\Model\Website::class, \Magento\Store\Model\ResourceModel\Website::class);
32  }
33 
39  protected function _renderFiltersBefore()
40  {
41  if (!$this->getLoadDefault()) {
42  $this->getSelect()->where('main_table.website_id > ?', 0);
43  }
44  parent::_renderFiltersBefore();
45  }
46 
53  public function setLoadDefault($loadDefault)
54  {
55  $this->setFlag('load_default_website', (bool)$loadDefault);
56  return $this;
57  }
58 
65  public function getLoadDefault()
66  {
67  return $this->getFlag('load_default_website');
68  }
69 
75  public function toOptionArray()
76  {
77  return $this->_toOptionArray('website_id', 'name');
78  }
79 
85  public function toOptionHash()
86  {
87  return $this->_toOptionHash('website_id', 'name');
88  }
89 
96  public function addIdFilter($ids)
97  {
98  if (is_array($ids)) {
99  if (empty($ids)) {
100  $this->addFieldToFilter('website_id', null);
101  } else {
102  $this->addFieldToFilter('website_id', ['in' => $ids]);
103  }
104  } else {
105  $this->addFieldToFilter('website_id', $ids);
106  }
107  return $this;
108  }
109 
117  public function load($printQuery = false, $logQuery = false)
118  {
119  $this->unshiftOrder('main_table.name', \Magento\Framework\DB\Select::SQL_ASC) // website name SECOND
120  ->unshiftOrder('main_table.sort_order', \Magento\Framework\DB\Select::SQL_ASC); // website sort order FIRST
121 
122  return parent::load($printQuery, $logQuery);
123  }
124 
134  public function joinGroupAndStore()
135  {
136  if (!$this->getFlag('groups_and_stores_joined')) {
137  $this->_idFieldName = 'website_group_store';
138  $this->getSelect()->joinLeft(
139  ['group_table' => $this->getTable('store_group')],
140  'main_table.website_id = group_table.website_id',
141  ['group_id' => 'group_id', 'group_title' => 'name', 'group_code' => 'code']
142  )->joinLeft(
143  ['store_table' => $this->getTable('store')],
144  'group_table.group_id = store_table.group_id',
145  ['store_id' => 'store_id', 'store_title' => 'name', 'store_code' => 'code']
146  );
147  $this->addOrder('group_table.name', \Magento\Framework\DB\Select::SQL_ASC) // store name
148  ->addOrder(
149  'CASE WHEN store_table.store_id = 0 THEN 0 ELSE 1 END',
150  \Magento\Framework\DB\Select::SQL_ASC
151  ) // view is admin
152  ->addOrder('store_table.sort_order', \Magento\Framework\DB\Select::SQL_ASC) // view sort order
153  ->addOrder('store_table.name', \Magento\Framework\DB\Select::SQL_ASC) // view name
154  ;
155  }
156  return $this;
157  }
158 
166  public function addFilterByGroupIds($groupIds)
167  {
168  if ($this->getFlag('groups_and_stores_joined')) {
169  $this->addFieldToFilter('group_table.group_id', $groupIds);
170  }
171  return $this;
172  }
173 }
_toOptionArray($valueField=null, $labelField='name', $additional=[])
Definition: AbstractDb.php:641
unshiftOrder($field, $direction=self::SORT_ORDER_DESC)
Definition: AbstractDb.php:298
load($printQuery=false, $logQuery=false)
Definition: Collection.php:117
addOrder($field, $direction=self::SORT_ORDER_DESC)
Definition: AbstractDb.php:286
_toOptionHash($valueField=null, $labelField='name')
Definition: AbstractDb.php:656
const SQL_ASC
Definition: Select.php:81