Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Store.php
Go to the documentation of this file.
1 <?php
7 
15 {
19  protected $configCache;
20 
25  public function __construct(
26  \Magento\Framework\Model\ResourceModel\Db\Context $context,
27  \Magento\Framework\App\Cache\Type\Config $configCacheType
28  ) {
29  $this->configCache = $configCacheType;
30  parent::__construct($context);
31  }
32 
38  protected function _construct()
39  {
40  $this->_init('store', 'store_id');
41  }
42 
51  public function countAll($countAdmin = false)
52  {
53  $connection = $this->getConnection();
54  $select = $connection->select()->from($this->getMainTable(), 'COUNT(*)');
55  if (!$countAdmin) {
56  $select->where(sprintf('%s <> %s', $connection->quoteIdentifier('code'), $connection->quote('admin')));
57  }
58  return (int)$connection->fetchOne($select);
59  }
60 
66  protected function _initUniqueFields()
67  {
68  $this->_uniqueFields = [['field' => 'code', 'title' => __('Store with the same code')]];
69  return $this;
70  }
71 
78  protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
79  {
80  parent::_afterSave($object);
81  $this->_updateGroupDefaultStore($object->getGroupId(), $object->getId());
82  $this->_changeGroup($object);
83 
84  return $this;
85  }
86 
93  protected function _afterDelete(\Magento\Framework\Model\AbstractModel $model)
94  {
95  $where = [
97  'scope_id = ?' => $model->getStoreId(),
98  ];
99 
100  $this->getConnection()->delete($this->getTable('core_config_data'), $where);
101  $this->configCache->clean();
102  return $this;
103  }
104 
113  {
114  $connection = $this->getConnection();
115 
116  $bindValues = ['group_id' => (int)$groupId];
117  $select = $connection->select()->from(
118  $this->getMainTable(),
119  ['count' => 'COUNT(*)']
120  )->where(
121  'group_id = :group_id'
122  );
123  $count = $connection->fetchOne($select, $bindValues);
124 
125  if ($count == 1) {
126  $bind = ['default_store_id' => (int)$storeId];
127  $where = ['group_id = ?' => (int)$groupId];
128  $connection->update($this->getTable('store_group'), $bind, $where);
129  }
130 
131  return $this;
132  }
133 
140  protected function _changeGroup(\Magento\Framework\Model\AbstractModel $model)
141  {
142  if ($model->getOriginalGroupId() && $model->getGroupId() != $model->getOriginalGroupId()) {
143  $connection = $this->getConnection();
144  $select = $connection->select()->from(
145  $this->getTable('store_group'),
146  'default_store_id'
147  )->where(
148  $connection->quoteInto('group_id=?', $model->getOriginalGroupId())
149  );
150  $storeId = $connection->fetchOne($select, 'default_store_id');
151 
152  if ($storeId == $model->getId()) {
153  $bind = ['default_store_id' => \Magento\Store\Model\Store::DEFAULT_STORE_ID];
154  $where = ['group_id = ?' => $model->getOriginalGroupId()];
155  $this->getConnection()->update($this->getTable('store_group'), $bind, $where);
156  }
157  }
158  return $this;
159  }
160 
167  public function readAllStores()
168  {
169  $select = $this->getConnection()
170  ->select()
171  ->from($this->getTable('store'));
172 
173  return $this->getConnection()->fetchAll($select);
174  }
175 
184  protected function _getLoadSelect($field, $value, $object)
185  {
186  $select = parent::_getLoadSelect($field, $value, $object);
187  $select->order('sort_order');
188  return $select;
189  }
190 }
_updateGroupDefaultStore($groupId, $storeId)
Definition: Store.php:112
_afterDelete(\Magento\Framework\Model\AbstractModel $model)
Definition: Store.php:93
$count
Definition: recent.phtml:13
_changeGroup(\Magento\Framework\Model\AbstractModel $model)
Definition: Store.php:140
__construct(\Magento\Framework\Model\ResourceModel\Db\Context $context, \Magento\Framework\App\Cache\Type\Config $configCacheType)
Definition: Store.php:25
__()
Definition: __.php:13
_getLoadSelect($field, $value, $object)
Definition: Store.php:184
$value
Definition: gender.phtml:16
_afterSave(\Magento\Framework\Model\AbstractModel $object)
Definition: Store.php:78
$connection
Definition: bulk.php:13