Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Group.php
Go to the documentation of this file.
1 <?php
7 
15 {
21  protected function _construct()
22  {
23  $this->_init('store_group', 'group_id');
24  }
25 
32  protected function _afterSave(\Magento\Framework\Model\AbstractModel $model)
33  {
34  $this->_updateStoreWebsite($model->getId(), $model->getWebsiteId());
35  $this->_updateWebsiteDefaultGroup($model->getWebsiteId(), $model->getId());
36  $this->_changeWebsite($model);
37 
38  return $this;
39  }
40 
47  protected function _initUniqueFields()
48  {
49  $this->_uniqueFields = [['field' => 'code', 'title' => __('Group with the same code')]];
50  return $this;
51  }
52 
61  {
62  $select = $this->getConnection()->select()->from(
63  $this->getMainTable(),
64  'COUNT(*)'
65  )->where(
66  'website_id = :website'
67  );
68  $count = $this->getConnection()->fetchOne($select, ['website' => $websiteId]);
69 
70  if ($count == 1) {
71  $bind = ['default_group_id' => $groupId];
72  $where = ['website_id = ?' => $websiteId];
73  $this->getConnection()->update($this->getTable('store_website'), $bind, $where);
74  }
75  return $this;
76  }
77 
84  protected function _changeWebsite(\Magento\Framework\Model\AbstractModel $model)
85  {
86  if ($model->getOriginalWebsiteId() && $model->getWebsiteId() != $model->getOriginalWebsiteId()) {
87  $select = $this->getConnection()->select()->from(
88  $this->getTable('store_website'),
89  'default_group_id'
90  )->where(
91  'website_id = :website_id'
92  );
93  $groupId = $this->getConnection()->fetchOne(
94  $select,
95  ['website_id' => $model->getOriginalWebsiteId()]
96  );
97 
98  if ($groupId == $model->getId()) {
99  $bind = ['default_group_id' => 0];
100  $where = ['website_id = ?' => $model->getOriginalWebsiteId()];
101  $this->getConnection()->update($this->getTable('store_website'), $bind, $where);
102  }
103  }
104  return $this;
105  }
106 
115  {
116  $bind = ['website_id' => $websiteId];
117  $where = ['group_id = ?' => $groupId];
118  $this->getConnection()->update($this->getTable('store'), $bind, $where);
119  return $this;
120  }
121 
129  protected function _saveDefaultStore($groupId, $storeId)
130  {
131  $bind = ['default_store_id' => $storeId];
132  $where = ['group_id = ?' => $groupId];
133  $this->getConnection()->update($this->getMainTable(), $bind, $where);
134 
135  return $this;
136  }
137 
146  public function countAll($countAdmin = false)
147  {
148  $connection = $this->getConnection();
149  $select = $connection->select()->from(['main' => $this->getMainTable()], 'COUNT(*)');
150  if (!$countAdmin) {
151  $select->joinLeft(
152  ['store_website' => $this->getTable('store_website')],
153  'store_website.website_id = main.website_id',
154  null
155  )->where(
156  sprintf('%s <> %s', $connection->quoteIdentifier('code'), $connection->quote('admin'))
157  );
158  }
159  return (int)$connection->fetchOne($select);
160  }
161 }
$count
Definition: recent.phtml:13
__()
Definition: __.php:13
_updateStoreWebsite($groupId, $websiteId)
Definition: Group.php:114
_updateWebsiteDefaultGroup($websiteId, $groupId)
Definition: Group.php:60
_saveDefaultStore($groupId, $storeId)
Definition: Group.php:129
_afterSave(\Magento\Framework\Model\AbstractModel $model)
Definition: Group.php:32
$connection
Definition: bulk.php:13
_changeWebsite(\Magento\Framework\Model\AbstractModel $model)
Definition: Group.php:84