Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Website.php
Go to the documentation of this file.
1 <?php
8 
16 {
22  protected function _construct()
23  {
24  $this->_init('store_website', 'website_id');
25  }
26 
32  protected function _initUniqueFields()
33  {
34  $this->_uniqueFields = [['field' => 'code', 'title' => __('Website with the same code')]];
35  return $this;
36  }
37 
47  public function readAllWebsites()
48  {
49  $websites = [];
50  $select = $this->getConnection()
51  ->select()
52  ->from($this->getTable('store_website'));
53 
54  foreach ($this->getConnection()->fetchAll($select) as $websiteData) {
55  $websites[$websiteData['code']] = $websiteData;
56  }
57 
58  return $websites;
59  }
60 
68  protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
69  {
70  if (!preg_match('/^[a-z]+[a-z0-9_]*$/i', $object->getCode())) {
71  throw new \Magento\Framework\Exception\LocalizedException(
72  __(
73  'Website code may only contain letters (a-z), numbers (0-9) or underscore (_),'
74  . ' and the first character must be a letter.'
75  )
76  );
77  }
78 
79  return parent::_beforeSave($object);
80  }
81 
88  protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
89  {
90  if ($object->getIsDefault()) {
91  $this->getConnection()->update($this->getMainTable(), ['is_default' => 0]);
92  $where = ['website_id = ?' => $object->getId()];
93  $this->getConnection()->update($this->getMainTable(), ['is_default' => 1], $where);
94  }
95  return parent::_afterSave($object);
96  }
97 
104  protected function _afterDelete(\Magento\Framework\Model\AbstractModel $model)
105  {
106  $where = [
108  'scope_id = ?' => $model->getWebsiteId(),
109  ];
110 
111  $this->getConnection()->delete($this->getTable('core_config_data'), $where);
112 
113  return $this;
114  }
115 
123  public function getDefaultStoresSelect($includeDefault = false)
124  {
125  $ifNull = $this->getConnection()->getCheckSql(
126  'store_group_table.default_store_id IS NULL',
127  '0',
128  'store_group_table.default_store_id'
129  );
130  $select = $this->getConnection()->select()->from(
131  ['website_table' => $this->getTable('store_website')],
132  ['website_id']
133  )->joinLeft(
134  ['store_group_table' => $this->getTable('store_group')],
135  'website_table.website_id=store_group_table.website_id' .
136  ' AND website_table.default_group_id = store_group_table.group_id',
137  ['store_id' => $ifNull]
138  );
139  if (!$includeDefault) {
140  $select->where('website_table.website_id <> ?', 0);
141  }
142  return $select;
143  }
144 
151  public function countAll($includeDefault = false)
152  {
153  $connection = $this->getConnection();
154  $select = $connection->select()->from($this->getMainTable(), 'COUNT(*)');
155  if (!$includeDefault) {
156  $select->where('website_id <> ?', 0);
157  }
158  return (int)$connection->fetchOne($select);
159  }
160 }
_afterSave(\Magento\Framework\Model\AbstractModel $object)
Definition: Website.php:88
_afterDelete(\Magento\Framework\Model\AbstractModel $model)
Definition: Website.php:104
_beforeSave(\Magento\Framework\Model\AbstractModel $object)
Definition: Website.php:68
__()
Definition: __.php:13
getDefaultStoresSelect($includeDefault=false)
Definition: Website.php:123
$connection
Definition: bulk.php:13