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 
10 
17 {
23  protected $_groupManagement;
24 
28  protected $_customersFactory;
29 
38  public function __construct(
39  \Magento\Framework\Model\ResourceModel\Db\Context $context,
42  \Magento\Customer\Api\GroupManagementInterface $groupManagement,
43  \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory $customersFactory,
44  $connectionName = null
45  ) {
46  $this->_groupManagement = $groupManagement;
47  $this->_customersFactory = $customersFactory;
48  parent::__construct($context, $entitySnapshot, $entityRelationComposite, $connectionName);
49  }
50 
56  protected function _construct()
57  {
58  $this->_init('customer_group', 'customer_group_id');
59  }
60 
66  protected function _initUniqueFields()
67  {
68  $this->_uniqueFields = [['field' => 'customer_group_code', 'title' => __('Customer Group')]];
69 
70  return $this;
71  }
72 
80  protected function _beforeDelete(\Magento\Framework\Model\AbstractModel $group)
81  {
82  if ($group->usesAsDefault()) {
83  throw new \Magento\Framework\Exception\LocalizedException(
84  __('You can\'t delete group "%1".', $group->getCode())
85  );
86  }
87  return parent::_beforeDelete($group);
88  }
89 
96  protected function _afterDelete(\Magento\Framework\Model\AbstractModel $group)
97  {
98  $customerCollection = $this->_createCustomersCollection()->addAttributeToFilter(
99  'group_id',
100  $group->getId()
101  )->load();
102  foreach ($customerCollection as $customer) {
104  $customer->load($customer->getId());
105  $defaultGroupId = $this->_groupManagement->getDefaultGroup($customer->getStoreId())->getId();
106  $customer->setGroupId($defaultGroupId);
107  $customer->save();
108  }
109  return parent::_afterDelete($group);
110  }
111 
115  protected function _createCustomersCollection()
116  {
117  return $this->_customersFactory->create();
118  }
119 
126  protected function _beforeSave(\Magento\Framework\Model\AbstractModel $group)
127  {
129  $group->setCode(substr($group->getCode(), 0, $group::GROUP_CODE_MAX_LENGTH));
130  return parent::_beforeSave($group);
131  }
132 
136  protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
137  {
138  if ($object->getId() == \Magento\Customer\Model\Group::CUST_GROUP_ALL) {
139  $this->skipReservedId($object);
140  }
141 
142  return $this;
143  }
144 
152  private function skipReservedId(\Magento\Framework\Model\AbstractModel $object)
153  {
154  $tableFieldsWithoutIdField = $this->getTableFieldsWithoutIdField();
155  $select = $this->getConnection()->select();
156  $select->from(
157  [$this->getMainTable()],
158  $tableFieldsWithoutIdField
159  )
160  ->where('customer_group_id = ?', \Magento\Customer\Model\Group::CUST_GROUP_ALL);
161 
162  $query = $this->getConnection()->insertFromSelect(
163  $select,
164  $this->getMainTable(),
165  $tableFieldsWithoutIdField
166  );
167  $this->getConnection()->query($query);
168  $lastInsertId = $this->getConnection()->lastInsertId();
169 
170  $query = $this->getConnection()->deleteFromSelect(
171  $select,
172  $this->getMainTable()
173  );
174  $this->getConnection()->query($query);
175 
176  $object->setId($lastInsertId);
177  }
178 
184  private function getTableFieldsWithoutIdField()
185  {
186  $fields = $this->getConnection()->describeTable($this->getMainTable());
187  if (isset($fields['customer_group_id'])) {
188  unset($fields['customer_group_id']);
189  }
190 
191  return array_keys($fields);
192  }
193 }
__construct(\Magento\Framework\Model\ResourceModel\Db\Context $context, Snapshot $entitySnapshot, RelationComposite $entityRelationComposite, \Magento\Customer\Api\GroupManagementInterface $groupManagement, \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory $customersFactory, $connectionName=null)
Definition: Group.php:38
$customer
Definition: customers.php:11
$group
Definition: sections.phtml:16
$fields
Definition: details.phtml:14
__()
Definition: __.php:13
_afterSave(\Magento\Framework\Model\AbstractModel $object)
Definition: Group.php:136
_beforeDelete(\Magento\Framework\Model\AbstractModel $group)
Definition: Group.php:80
_afterDelete(\Magento\Framework\Model\AbstractModel $object)
Definition: AbstractDb.php:689
_beforeSave(\Magento\Framework\Model\AbstractModel $object)
Definition: AbstractDb.php:653
load(\Magento\Framework\Model\AbstractModel $object, $value, $field=null)
Definition: AbstractDb.php:339