6
6
7
7
namespace Magento \CatalogSearch \Model \Search ;
8
8
9
+ use Magento \Catalog \Model \Product ;
9
10
use Magento \Catalog \Model \ResourceModel \Product \Attribute \CollectionFactory ;
11
+ use Magento \Eav \Model \Config as EavConfig ;
10
12
use Magento \Eav \Model \Entity \Attribute \AbstractAttribute ;
13
+ use Magento \Framework \App \ObjectManager ;
11
14
use Magento \Framework \App \ResourceConnection as AppResource ;
12
15
use Magento \Framework \DB \Select ;
13
16
use Magento \Framework \Search \Request \FilterInterface ;
@@ -34,41 +37,64 @@ class TableMapper
34
37
35
38
/**
36
39
* @var \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection
40
+ * @deprecated
37
41
*/
38
42
private $ attributeCollection ;
39
43
44
+ /**
45
+ * @var EavConfig
46
+ */
47
+ private $ eavConfig ;
48
+
40
49
/**
41
50
* @param AppResource $resource
42
51
* @param StoreManagerInterface $storeManager
43
52
* @param CollectionFactory $attributeCollectionFactory
53
+ * @param EavConfig $eavConfig
44
54
*/
45
55
public function __construct (
46
56
AppResource $ resource ,
47
57
StoreManagerInterface $ storeManager ,
48
- CollectionFactory $ attributeCollectionFactory
58
+ CollectionFactory $ attributeCollectionFactory ,
59
+ EavConfig $ eavConfig = null
49
60
) {
50
61
$ this ->resource = $ resource ;
51
62
$ this ->storeManager = $ storeManager ;
52
63
$ this ->attributeCollection = $ attributeCollectionFactory ->create ();
64
+ $ this ->eavConfig = $ eavConfig !== null ? $ eavConfig : ObjectManager::getInstance ()->get (EavConfig::class);
53
65
}
54
66
55
67
/**
56
68
* @param Select $select
57
69
* @param RequestInterface $request
58
70
* @return Select
71
+ * @throws \LogicException
59
72
*/
60
73
public function addTables (Select $ select , RequestInterface $ request )
61
74
{
62
75
$ mappedTables = [];
63
76
$ filters = $ this ->getFilters ($ request ->getQuery ());
64
77
foreach ($ filters as $ filter ) {
65
- list ($ alias , $ table , $ mapOn , $ mappedFields ) = $ this ->getMappingData ($ filter );
78
+ list ($ alias , $ table , $ mapOn , $ mappedFields, $ joinType ) = $ this ->getMappingData ($ filter );
66
79
if (!array_key_exists ($ alias , $ mappedTables )) {
67
- $ select ->joinLeft (
68
- [$ alias => $ table ],
69
- $ mapOn ,
70
- $ mappedFields
71
- );
80
+ switch ($ joinType ) {
81
+ case \Magento \Framework \DB \Select::INNER_JOIN :
82
+ $ select ->joinInner (
83
+ [$ alias => $ table ],
84
+ $ mapOn ,
85
+ $ mappedFields
86
+ );
87
+ break ;
88
+ case \Magento \Framework \DB \Select::LEFT_JOIN :
89
+ $ select ->joinLeft (
90
+ [$ alias => $ table ],
91
+ $ mapOn ,
92
+ $ mappedFields
93
+ );
94
+ break ;
95
+ default :
96
+ throw new \LogicException (__ ('Unsupported join type: %1 ' , $ joinType ));
97
+ }
72
98
$ mappedTables [$ alias ] = $ table ;
73
99
}
74
100
}
@@ -102,6 +128,7 @@ private function getMappingData(FilterInterface $filter)
102
128
$ mapOn = null ;
103
129
$ mappedFields = null ;
104
130
$ field = $ filter ->getField ();
131
+ $ joinType = \Magento \Framework \DB \Select::INNER_JOIN ;
105
132
$ fieldToTableMap = $ this ->getFieldToTableMap ($ field );
106
133
if ($ fieldToTableMap ) {
107
134
list ($ alias , $ table , $ mapOn , $ mappedFields ) = $ fieldToTableMap ;
@@ -110,6 +137,7 @@ private function getMappingData(FilterInterface $filter)
110
137
if ($ filter ->getType () === FilterInterface::TYPE_TERM
111
138
&& in_array ($ attribute ->getFrontendInput (), ['select ' , 'multiselect ' ], true )
112
139
) {
140
+ $ joinType = \Magento \Framework \DB \Select::LEFT_JOIN ;
113
141
$ table = $ this ->resource ->getTableName ('catalog_product_index_eav ' );
114
142
$ alias = $ field . RequestGenerator::FILTER_SUFFIX ;
115
143
$ mapOn = sprintf (
@@ -127,7 +155,7 @@ private function getMappingData(FilterInterface $filter)
127
155
}
128
156
}
129
157
130
- return [$ alias , $ table , $ mapOn , $ mappedFields ];
158
+ return [$ alias , $ table , $ mapOn , $ mappedFields, $ joinType ];
131
159
}
132
160
133
161
/**
@@ -242,10 +270,11 @@ private function getFieldToTableMap($field)
242
270
/**
243
271
* @param string $field
244
272
* @return \Magento\Catalog\Model\ResourceModel\Eav\Attribute
273
+ * @throws \Magento\Framework\Exception\LocalizedException
245
274
*/
246
275
private function getAttributeByCode ($ field )
247
276
{
248
- $ attribute = $ this ->attributeCollection -> getItemByColumnValue ( ' attribute_code ' , $ field );
277
+ $ attribute = $ this ->eavConfig -> getAttribute (Product:: ENTITY , $ field );
249
278
return $ attribute ;
250
279
}
251
280
}
0 commit comments