5
5
*/
6
6
namespace Magento \SalesRule \Model \ResourceModel ;
7
7
8
+ use \Magento \SalesRule \Model \Rule as SalesRule ;
8
9
use Magento \Framework \Model \AbstractModel ;
10
+ use Magento \Framework \DB \Select ;
11
+ use Magento \Rule \Model \ResourceModel \AbstractResource ;
12
+ use Magento \Framework \Model \EntityManager ;
13
+ use Magento \SalesRule \Api \Data \RuleInterface ;
9
14
10
15
/**
11
16
* Sales Rule resource model
12
17
*/
13
- class Rule extends \ Magento \ Rule \ Model \ ResourceModel \ AbstractResource
18
+ class Rule extends AbstractResource
14
19
{
15
20
/**
16
21
* Store associated with rule entities information map
17
22
*
18
23
* @var array
19
24
*/
20
- protected $ _associatedEntitiesMap = [
21
- 'website ' => [
22
- 'associations_table ' => 'salesrule_website ' ,
23
- 'rule_id_field ' => 'rule_id ' ,
24
- 'entity_id_field ' => 'website_id ' ,
25
- ],
26
- 'customer_group ' => [
27
- 'associations_table ' => 'salesrule_customer_group ' ,
28
- 'rule_id_field ' => 'rule_id ' ,
29
- 'entity_id_field ' => 'customer_group_id ' ,
30
- ],
31
- ];
25
+ protected $ _associatedEntitiesMap = [];
32
26
33
27
/**
34
28
* @var array
@@ -52,20 +46,31 @@ class Rule extends \Magento\Rule\Model\ResourceModel\AbstractResource
52
46
*/
53
47
protected $ _resourceCoupon ;
54
48
49
+ /**
50
+ * @var EntityManager
51
+ */
52
+ protected $ entityManager ;
53
+
55
54
/**
56
55
* @param \Magento\Framework\Model\ResourceModel\Db\Context $context
57
56
* @param \Magento\Framework\Stdlib\StringUtils $string
58
57
* @param \Magento\SalesRule\Model\ResourceModel\Coupon $resourceCoupon
58
+ * @param EntityManager $entityManager
59
+ * @param array $associatedEntitiesMap
59
60
* @param string $connectionName
60
61
*/
61
62
public function __construct (
62
63
\Magento \Framework \Model \ResourceModel \Db \Context $ context ,
63
64
\Magento \Framework \Stdlib \StringUtils $ string ,
64
65
\Magento \SalesRule \Model \ResourceModel \Coupon $ resourceCoupon ,
66
+ EntityManager $ entityManager ,
67
+ array $ associatedEntitiesMap = [],
65
68
$ connectionName = null
66
69
) {
67
70
$ this ->string = $ string ;
68
71
$ this ->_resourceCoupon = $ resourceCoupon ;
72
+ $ this ->entityManager = $ entityManager ;
73
+ $ this ->_associatedEntitiesMap = $ associatedEntitiesMap ;
69
74
parent ::__construct ($ context , $ connectionName );
70
75
}
71
76
@@ -79,24 +84,10 @@ protected function _construct()
79
84
$ this ->_init ('salesrule ' , 'rule_id ' );
80
85
}
81
86
82
- /**
83
- * Add customer group ids and website ids to rule data after load
84
- *
85
- * @param AbstractModel $object
86
- * @return $this
87
- */
88
- protected function _afterLoad (AbstractModel $ object )
89
- {
90
- $ this ->loadCustomerGroupIds ($ object );
91
- $ this ->loadWebsiteIds ($ object );
92
-
93
- parent ::_afterLoad ($ object );
94
- return $ this ;
95
- }
96
-
97
87
/**
98
88
* @param AbstractModel $object
99
89
* @return void
90
+ * @deprecated
100
91
*/
101
92
public function loadCustomerGroupIds (AbstractModel $ object )
102
93
{
@@ -109,6 +100,7 @@ public function loadCustomerGroupIds(AbstractModel $object)
109
100
/**
110
101
* @param AbstractModel $object
111
102
* @return void
103
+ * @deprecated
112
104
*/
113
105
public function loadWebsiteIds (AbstractModel $ object )
114
106
{
@@ -135,6 +127,23 @@ public function _beforeSave(AbstractModel $object)
135
127
return $ this ;
136
128
}
137
129
130
+ /**
131
+ * Load an object
132
+ *
133
+ * @param SalesRule|AbstractModel $object
134
+ * @param mixed $value
135
+ * @param string $field field to load by (defaults to model id)
136
+ * @return $this
137
+ * @SuppressWarnings(PHPMD.UnusedFormalParameter)
138
+ */
139
+ public function load (AbstractModel $ object , $ value , $ field = null )
140
+ {
141
+ $ this ->entityManager ->load (RuleInterface::class, $ object , $ value );
142
+ $ this ->unserializeFields ($ object );
143
+ $ this ->_afterLoad ($ object );
144
+ return $ this ;
145
+ }
146
+
138
147
/**
139
148
* Bind sales rule to customer group(s) and website(s).
140
149
* Save rule's associated store labels.
@@ -149,22 +158,6 @@ protected function _afterSave(AbstractModel $object)
149
158
$ this ->saveStoreLabels ($ object ->getId (), $ object ->getStoreLabels ());
150
159
}
151
160
152
- if ($ object ->hasWebsiteIds ()) {
153
- $ websiteIds = $ object ->getWebsiteIds ();
154
- if (!is_array ($ websiteIds )) {
155
- $ websiteIds = explode (', ' , (string )$ websiteIds );
156
- }
157
- $ this ->bindRuleToEntity ($ object ->getId (), $ websiteIds , 'website ' );
158
- }
159
-
160
- if ($ object ->hasCustomerGroupIds ()) {
161
- $ customerGroupIds = $ object ->getCustomerGroupIds ();
162
- if (!is_array ($ customerGroupIds )) {
163
- $ customerGroupIds = explode (', ' , (string )$ customerGroupIds );
164
- }
165
- $ this ->bindRuleToEntity ($ object ->getId (), $ customerGroupIds , 'customer_group ' );
166
- }
167
-
168
161
// Save product attributes used in rule
169
162
$ ruleProductAttributes = array_merge (
170
163
$ this ->getProductAttributes (serialize ($ object ->getConditions ()->asArray ())),
@@ -369,4 +362,45 @@ public function getProductAttributes($serializedString)
369
362
370
363
return $ result ;
371
364
}
365
+
366
+ /**
367
+ * @param \Magento\Framework\Model\AbstractModel $object
368
+ * @return $this
369
+ * @throws \Exception
370
+ */
371
+ public function save (\Magento \Framework \Model \AbstractModel $ object )
372
+ {
373
+ if ($ object ->isDeleted ()) {
374
+ return $ this ->delete ($ object );
375
+ }
376
+
377
+ $ this ->beginTransaction ();
378
+
379
+ try {
380
+ if (!$ this ->isModified ($ object )) {
381
+ $ this ->processNotModifiedSave ($ object );
382
+ $ this ->commit ();
383
+ $ object ->setHasDataChanges (false );
384
+ return $ this ;
385
+ }
386
+ $ object ->validateBeforeSave ();
387
+ $ object ->beforeSave ();
388
+ if ($ object ->isSaveAllowed ()) {
389
+ $ this ->_serializeFields ($ object );
390
+ $ this ->_beforeSave ($ object );
391
+ $ this ->_checkUnique ($ object );
392
+ $ this ->objectRelationProcessor ->validateDataIntegrity ($ this ->getMainTable (), $ object ->getData ());
393
+ $ this ->entityManager ->save (RuleInterface::class, $ object );
394
+ $ this ->unserializeFields ($ object );
395
+ $ this ->processAfterSaves ($ object );
396
+ }
397
+ $ this ->addCommitCallback ([$ object , 'afterCommitCallback ' ])->commit ();
398
+ $ object ->setHasDataChanges (false );
399
+ } catch (\Exception $ e ) {
400
+ $ this ->rollBack ();
401
+ $ object ->setHasDataChanges (true );
402
+ throw $ e ;
403
+ }
404
+ return $ this ;
405
+ }
372
406
}
0 commit comments