Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractResource.php
Go to the documentation of this file.
1 <?php
8 
16 {
37  protected $_associatedEntitiesMap = [];
38 
45  public function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
46  {
47  $this->resolveDate($object, 'from_date');
48  $this->resolveDate($object, 'to_date');
49  parent::_beforeSave($object);
50  return $this;
51  }
52 
58  private function resolveDate(\Magento\Framework\Model\AbstractModel $object, $dateIdentifier)
59  {
60  $date = $object->getData($dateIdentifier);
61  if ($date instanceof \DateTimeInterface) {
62  $object->setData($dateIdentifier, $date->format('Y-m-d H:i:s'));
63  } elseif (!is_string($date) || empty($date)) {
64  $object->setData($dateIdentifier, null);
65  }
66  }
67 
77  public function bindRuleToEntity($ruleIds, $entityIds, $entityType)
78  {
79  $this->getConnection()->beginTransaction();
80 
81  try {
82  $this->_multiplyBunchInsert($ruleIds, $entityIds, $entityType);
83  } catch (\Exception $e) {
84  $this->getConnection()->rollBack();
85  throw $e;
86  }
87 
88  $this->getConnection()->commit();
89 
90  return $this;
91  }
92 
101  protected function _multiplyBunchInsert($ruleIds, $entityIds, $entityType)
102  {
103  if (empty($ruleIds) || empty($entityIds)) {
104  return $this;
105  }
106  if (!is_array($ruleIds)) {
107  $ruleIds = [(int)$ruleIds];
108  }
109  if (!is_array($entityIds)) {
110  $entityIds = [(int)$entityIds];
111  }
112  $data = [];
113  $count = 0;
114  $entityInfo = $this->_getAssociatedEntityInfo($entityType);
115  foreach ($ruleIds as $ruleId) {
116  foreach ($entityIds as $entityId) {
117  $data[] = [
118  $entityInfo['entity_id_field'] => $entityId,
119  $entityInfo['rule_id_field'] => $ruleId,
120  ];
121  $count++;
122  if ($count % 1000 == 0) {
123  $this->getConnection()->insertOnDuplicate(
124  $this->getTable($entityInfo['associations_table']),
125  $data,
126  [$entityInfo['rule_id_field']]
127  );
128  $data = [];
129  }
130  }
131  }
132  if (!empty($data)) {
133  $this->getConnection()->insertOnDuplicate(
134  $this->getTable($entityInfo['associations_table']),
135  $data,
136  [$entityInfo['rule_id_field']]
137  );
138  }
139 
140  $this->getConnection()->delete(
141  $this->getTable($entityInfo['associations_table']),
142  $this->getConnection()->quoteInto(
143  $entityInfo['rule_id_field'] . ' IN (?) AND ',
144  $ruleIds
145  ) . $this->getConnection()->quoteInto(
146  $entityInfo['entity_id_field'] . ' NOT IN (?)',
147  $entityIds
148  )
149  );
150  return $this;
151  }
152 
161  public function unbindRuleFromEntity($ruleIds, $entityIds, $entityType)
162  {
163  $connection = $this->getConnection();
164  $entityInfo = $this->_getAssociatedEntityInfo($entityType);
165 
166  if (!is_array($entityIds)) {
167  $entityIds = [(int)$entityIds];
168  }
169  if (!is_array($ruleIds)) {
170  $ruleIds = [(int)$ruleIds];
171  }
172 
173  $where = [];
174  if (!empty($ruleIds)) {
175  $where[] = $connection->quoteInto($entityInfo['rule_id_field'] . ' IN (?)', $ruleIds);
176  }
177  if (!empty($entityIds)) {
178  $where[] = $connection->quoteInto($entityInfo['entity_id_field'] . ' IN (?)', $entityIds);
179  }
180 
181  $connection->delete($this->getTable($entityInfo['associations_table']), implode(' AND ', $where));
182 
183  return $this;
184  }
185 
194  {
195  $entityInfo = $this->_getAssociatedEntityInfo($entityType);
196 
197  $select = $this->getConnection()->select()->from(
198  $this->getTable($entityInfo['associations_table']),
199  [$entityInfo['entity_id_field']]
200  )->where(
201  $entityInfo['rule_id_field'] . ' = ?',
202  $ruleId
203  );
204 
205  return $this->getConnection()->fetchCol($select);
206  }
207 
214  public function getWebsiteIds($ruleId)
215  {
216  return $this->getAssociatedEntityIds($ruleId, 'website');
217  }
218 
225  public function getCustomerGroupIds($ruleId)
226  {
227  return $this->getAssociatedEntityIds($ruleId, 'customer_group');
228  }
229 
239  {
240  if (isset($this->_associatedEntitiesMap[$entityType])) {
241  return $this->_associatedEntitiesMap[$entityType];
242  }
243 
244  throw new \Magento\Framework\Exception\LocalizedException(
245  __('There is no information about associated entity type "%1".', $entityType)
246  );
247  }
248 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$count
Definition: recent.phtml:13
_beforeSave(\Magento\Framework\Model\AbstractModel $object)
unbindRuleFromEntity($ruleIds, $entityIds, $entityType)
__()
Definition: __.php:13
_multiplyBunchInsert($ruleIds, $entityIds, $entityType)
bindRuleToEntity($ruleIds, $entityIds, $entityType)
$connection
Definition: bulk.php:13