Store.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Store\Model\ResourceModel;
  7. /**
  8. * Store Resource Model
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Store extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  14. {
  15. /**
  16. * @var \Magento\Framework\App\Cache\Type\Config
  17. */
  18. protected $configCache;
  19. /**
  20. * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
  21. * @param \Magento\Framework\App\Cache\Type\Config $configCacheType
  22. */
  23. public function __construct(
  24. \Magento\Framework\Model\ResourceModel\Db\Context $context,
  25. \Magento\Framework\App\Cache\Type\Config $configCacheType
  26. ) {
  27. $this->configCache = $configCacheType;
  28. parent::__construct($context);
  29. }
  30. /**
  31. * Define main table and primary key
  32. *
  33. * @return void
  34. */
  35. protected function _construct()
  36. {
  37. $this->_init('store', 'store_id');
  38. }
  39. /**
  40. * Count number of all entities in the system
  41. *
  42. * By default won't count admin store
  43. *
  44. * @param bool $countAdmin
  45. * @return int
  46. */
  47. public function countAll($countAdmin = false)
  48. {
  49. $connection = $this->getConnection();
  50. $select = $connection->select()->from($this->getMainTable(), 'COUNT(*)');
  51. if (!$countAdmin) {
  52. $select->where(sprintf('%s <> %s', $connection->quoteIdentifier('code'), $connection->quote('admin')));
  53. }
  54. return (int)$connection->fetchOne($select);
  55. }
  56. /**
  57. * Initialize unique fields
  58. *
  59. * @return $this
  60. */
  61. protected function _initUniqueFields()
  62. {
  63. $this->_uniqueFields = [['field' => 'code', 'title' => __('Store with the same code')]];
  64. return $this;
  65. }
  66. /**
  67. * Update Store Group data after save store
  68. *
  69. * @param \Magento\Framework\Model\AbstractModel $object
  70. * @return $this
  71. */
  72. protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
  73. {
  74. parent::_afterSave($object);
  75. $this->_updateGroupDefaultStore($object->getGroupId(), $object->getId());
  76. $this->_changeGroup($object);
  77. return $this;
  78. }
  79. /**
  80. * Remove configuration data after delete store
  81. *
  82. * @param \Magento\Framework\Model\AbstractModel $model
  83. * @return $this
  84. */
  85. protected function _afterDelete(\Magento\Framework\Model\AbstractModel $model)
  86. {
  87. $where = [
  88. 'scope = ?' => \Magento\Store\Model\ScopeInterface::SCOPE_STORES,
  89. 'scope_id = ?' => $model->getStoreId(),
  90. ];
  91. $this->getConnection()->delete($this->getTable('core_config_data'), $where);
  92. $this->configCache->clean();
  93. return $this;
  94. }
  95. /**
  96. * Update Default store for Store Group
  97. *
  98. * @param int $groupId
  99. * @param int $storeId
  100. * @return $this
  101. */
  102. protected function _updateGroupDefaultStore($groupId, $storeId)
  103. {
  104. $connection = $this->getConnection();
  105. $bindValues = ['group_id' => (int)$groupId];
  106. $select = $connection->select()->from(
  107. $this->getMainTable(),
  108. ['count' => 'COUNT(*)']
  109. )->where(
  110. 'group_id = :group_id'
  111. );
  112. $count = $connection->fetchOne($select, $bindValues);
  113. if ($count == 1) {
  114. $bind = ['default_store_id' => (int)$storeId];
  115. $where = ['group_id = ?' => (int)$groupId];
  116. $connection->update($this->getTable('store_group'), $bind, $where);
  117. }
  118. return $this;
  119. }
  120. /**
  121. * Change store group for store
  122. *
  123. * @param \Magento\Framework\Model\AbstractModel $model
  124. * @return $this
  125. */
  126. protected function _changeGroup(\Magento\Framework\Model\AbstractModel $model)
  127. {
  128. if ($model->getOriginalGroupId() && $model->getGroupId() != $model->getOriginalGroupId()) {
  129. $connection = $this->getConnection();
  130. $select = $connection->select()->from(
  131. $this->getTable('store_group'),
  132. 'default_store_id'
  133. )->where(
  134. $connection->quoteInto('group_id=?', $model->getOriginalGroupId())
  135. );
  136. $storeId = $connection->fetchOne($select, 'default_store_id');
  137. if ($storeId == $model->getId()) {
  138. $bind = ['default_store_id' => \Magento\Store\Model\Store::DEFAULT_STORE_ID];
  139. $where = ['group_id = ?' => $model->getOriginalGroupId()];
  140. $this->getConnection()->update($this->getTable('store_group'), $bind, $where);
  141. }
  142. }
  143. return $this;
  144. }
  145. /**
  146. * Read information about all stores
  147. *
  148. * @return array
  149. * @since 100.1.3
  150. */
  151. public function readAllStores()
  152. {
  153. $select = $this->getConnection()
  154. ->select()
  155. ->from($this->getTable('store'));
  156. return $this->getConnection()->fetchAll($select);
  157. }
  158. /**
  159. * Retrieve select object for load object data
  160. *
  161. * @param string $field
  162. * @param mixed $value
  163. * @param \Magento\Framework\Model\AbstractModel $object
  164. * @return \Magento\Framework\DB\Select
  165. */
  166. protected function _getLoadSelect($field, $value, $object)
  167. {
  168. $select = parent::_getLoadSelect($field, $value, $object);
  169. $select->order('sort_order');
  170. return $select;
  171. }
  172. }