1
1
<?php
2
2
namespace Ometria \Api \Model \ResourceModel ;
3
3
4
+ use Magento \Catalog \Api \Data \ProductInterface ;
5
+ use Magento \Framework \EntityManager \MetadataPool ;
4
6
use Magento \Framework \Model \ResourceModel \Db \AbstractDb ;
7
+ use Magento \Framework \Model \ResourceModel \Db \Context ;
5
8
6
9
class Product extends AbstractDb
7
10
{
11
+ /** @var MetadataPool */
12
+ private $ metadataPool ;
13
+
14
+ /**
15
+ * @param Context $context
16
+ * @param MetadataPool $metadataPool
17
+ * @param null $connectionName
18
+ */
19
+ public function __construct (
20
+ Context $ context ,
21
+ MetadataPool $ metadataPool ,
22
+ $ connectionName = null
23
+ ) {
24
+ $ this ->metadataPool = $ metadataPool ;
25
+
26
+ parent ::__construct ($ context , $ connectionName );
27
+ }
28
+
8
29
protected function _construct ()
9
30
{
10
31
}
@@ -21,22 +42,26 @@ public function getConfigurableProductParentIds(array $childIds)
21
42
$ childToParentIds = [];
22
43
23
44
$ connection = $ this ->getConnection ();
24
-
45
+ $ metadata = $ this -> metadataPool -> getMetadata (ProductInterface::class);
25
46
$ select = $ connection ->select ()
26
47
->from (
27
- $ this ->getConnection ()->getTableName ('catalog_product_super_link ' ),
28
- ['product_id ' , 'parent_id ' ]
29
- )
30
- ->where (
31
- 'product_id IN (?) ' ,
48
+ ['link_table ' => $ connection ->getTableName ('catalog_product_super_link ' )],
49
+ ['link_id ' , 'product_id ' ]
50
+ )->join (
51
+ ['e ' => $ this ->metadataPool ->getMetadata (ProductInterface::class)->getEntityTable ()],
52
+ 'e. ' . $ metadata ->getLinkField () . ' = link_table.parent_id ' ,
53
+ ['e.entity_id ' ]
54
+ )->where (
55
+ 'link_table.product_id IN(?) ' ,
32
56
$ childIds
33
57
)
34
- // order by the oldest links first so the iterator will end with the most recent link
35
- ->order ('link_id ASC ' );
58
+ ->order (
59
+ 'link_id ASC '
60
+ );
36
61
37
62
$ result = $ connection ->fetchAll ($ select );
38
63
foreach ($ result as $ _row ) {
39
- $ childToParentIds [$ _row ['product_id ' ]] = $ _row ['parent_id ' ];
64
+ $ childToParentIds [$ _row ['product_id ' ]] = $ _row ['entity_id ' ];
40
65
}
41
66
42
67
return $ childToParentIds ;
@@ -54,22 +79,25 @@ public function getBundleProductParentIds(array $childIds)
54
79
$ childToParentIds = [];
55
80
56
81
$ connection = $ this ->getConnection ();
57
-
82
+ $ metadata = $ this -> metadataPool -> getMetadata (ProductInterface::class);
58
83
$ select = $ connection ->select ()
59
84
->from (
60
- $ this ->getConnection ()->getTableName ('catalog_product_bundle_selection ' ),
61
- ['parent_product_id ' , 'product_id ' ]
62
- )
63
- ->where (
64
- 'product_id IN (?) ' ,
85
+ ['link_table ' => $ connection ->getTableName ('catalog_product_bundle_selection ' )],
86
+ ['selection_id ' , 'product_id ' ]
87
+ )->join (
88
+ ['e ' => $ this ->metadataPool ->getMetadata (ProductInterface::class)->getEntityTable ()],
89
+ 'e. ' . $ metadata ->getLinkField () . ' = link_table.parent_product_id ' ,
90
+ ['e.entity_id ' ]
91
+ )->where (
92
+ 'link_table.product_id IN(?) ' ,
65
93
$ childIds
66
- )
67
- // order by the oldest selections first so the iterator will end with the most recent link
68
- -> order ( ' selection_id ASC ' );
94
+ )-> order (
95
+ ' selection_id ASC '
96
+ );
69
97
70
98
$ result = $ connection ->fetchAll ($ select );
71
99
foreach ($ result as $ _row ) {
72
- $ childToParentIds [$ _row ['product_id ' ]] = $ _row ['parent_product_id ' ];
100
+ $ childToParentIds [$ _row ['product_id ' ]] = $ _row ['entity_id ' ];
73
101
}
74
102
75
103
return $ childToParentIds ;
@@ -87,26 +115,30 @@ public function getGroupedProductParentIds(array $childIds)
87
115
$ childToParentIds = [];
88
116
89
117
$ connection = $ this ->getConnection ();
90
-
118
+ $ metadata = $ this -> metadataPool -> getMetadata (ProductInterface::class);
91
119
$ select = $ connection ->select ()
92
120
->from (
93
- $ this ->getConnection ()->getTableName ('catalog_product_link ' ),
94
- ['product_id ' , 'linked_product_id ' ]
95
- )
96
- ->where (
97
- 'linked_product_id IN (?) ' ,
98
- $ childIds
99
- )
100
- ->where (
121
+ ['link_table ' => $ connection ->getTableName ('catalog_product_link ' )],
122
+ ['link_id ' , 'linked_product_id ' ]
123
+ )->join (
124
+ ['e ' => $ this ->metadataPool ->getMetadata (ProductInterface::class)->getEntityTable ()],
125
+ 'e. ' . $ metadata ->getLinkField () . ' = link_table.product_id ' ,
126
+ ['e.entity_id ' ]
127
+ )->where (
101
128
'link_type_id = ? ' ,
102
129
\Magento \GroupedProduct \Model \ResourceModel \Product \Link::LINK_TYPE_GROUPED
103
130
)
104
- // order by the oldest links first so the iterator will end with the most recent link
105
- ->order ('link_id ASC ' );
131
+ ->where (
132
+ 'link_table.linked_product_id IN(?) ' ,
133
+ $ childIds
134
+ )->order (
135
+ 'link_id ASC '
136
+ );
106
137
107
138
$ result = $ connection ->fetchAll ($ select );
139
+
108
140
foreach ($ result as $ _row ) {
109
- $ childToParentIds [$ _row ['linked_product_id ' ]] = $ _row ['product_id ' ];
141
+ $ childToParentIds [$ _row ['linked_product_id ' ]] = $ _row ['entity_id ' ];
110
142
}
111
143
112
144
return $ childToParentIds ;
0 commit comments