@@ -61,36 +61,51 @@ public function getValues(
61
61
array $ attributeCodes = [],
62
62
array $ storeIds = []
63
63
): array {
64
- return $ this ->getValuesImplementation ($ entityType , $ entityId , $ attributeCodes , $ storeIds );
64
+ $ metadata = $ this ->metadataPool ->getMetadata ($ entityType );
65
+ $ connection = $ metadata ->getEntityConnection ();
66
+ $ selects = [];
67
+ $ attributeTables = $ this ->prepareAttributeTables ($ entityType , $ attributeCodes );
68
+ foreach ($ attributeTables as $ attributeTable => $ attributeIds ) {
69
+ $ select = $ connection ->select ()
70
+ ->from (
71
+ ['t ' => $ attributeTable ],
72
+ ['* ' ]
73
+ )
74
+ ->where ('attribute_id IN (?) ' , $ attributeIds );
75
+
76
+ $ select ->where ($ metadata ->getLinkField () . ' = ? ' , $ entityId );
77
+
78
+ if (!empty ($ storeIds )) {
79
+ $ select ->where (
80
+ 'store_id IN (?) ' ,
81
+ $ storeIds
82
+ );
83
+ }
84
+ $ selects [] = $ select ;
85
+ }
86
+
87
+ if (count ($ selects ) > 1 ) {
88
+ $ select = $ connection ->select ();
89
+ $ select ->from (['u ' => new UnionExpression ($ selects , Select::SQL_UNION_ALL , '( %s ) ' )]);
90
+ } else {
91
+ $ select = reset ($ selects );
92
+ }
93
+
94
+ return $ connection ->fetchAll ($ select );
65
95
}
66
96
67
97
/**
68
- * Implementation for the getValues methods
98
+ * Fill the attribute tables array
69
99
*
70
100
* @param string $entityType
71
- * @param int $entityId
72
- * @param string[] $attributeCodes
73
- * @param int[] $storeIds
74
- * @param int[] $entityIds
75
- * @param bool $isMultiple
101
+ * @param array $attributeCodes
76
102
* @return array
77
- * @throws \Exception
78
103
*/
79
- private function getValuesImplementation (
80
- string $ entityType ,
81
- int $ entityId = 0 ,
82
- array $ attributeCodes = [],
83
- array $ storeIds = [],
84
- array $ entityIds = [],
85
- bool $ isMultiple = false
86
- ): array {
87
- $ metadata = $ this ->metadataPool ->getMetadata ($ entityType );
88
- $ connection = $ metadata ->getEntityConnection ();
89
- $ selects = [];
104
+ private function prepareAttributeTables (string $ entityType , array $ attributeCodes ) : array
105
+ {
90
106
$ attributeTables = [];
91
107
$ attributes = [];
92
108
$ allAttributes = $ this ->getEntityAttributes ($ entityType );
93
- $ result = [];
94
109
if ($ attributeCodes ) {
95
110
foreach ($ attributeCodes as $ attributeCode ) {
96
111
$ attributes [$ attributeCode ] = $ allAttributes [$ attributeCode ];
@@ -104,6 +119,29 @@ private function getValuesImplementation(
104
119
$ attributeTables [$ attribute ->getBackend ()->getTable ()][] = $ attribute ->getAttributeId ();
105
120
}
106
121
}
122
+ return $ attributeTables ;
123
+ }
124
+
125
+ /**
126
+ * Bulk version of the getValues() for several entities
127
+ *
128
+ * @param string $entityType
129
+ * @param int[] $entityIds
130
+ * @param string[] $attributeCodes
131
+ * @param int[] $storeIds
132
+ * @return array
133
+ */
134
+ public function getValuesMultiple (
135
+ string $ entityType ,
136
+ array $ entityIds ,
137
+ array $ attributeCodes = [],
138
+ array $ storeIds = []
139
+ ) : array {
140
+ $ metadata = $ this ->metadataPool ->getMetadata ($ entityType );
141
+ $ connection = $ metadata ->getEntityConnection ();
142
+ $ selects = [];
143
+ $ result = [];
144
+ $ attributeTables = $ this ->prepareAttributeTables ($ entityType , $ attributeCodes );
107
145
108
146
if ($ attributeTables ) {
109
147
foreach ($ attributeTables as $ attributeTable => $ attributeIds ) {
@@ -113,17 +151,15 @@ private function getValuesImplementation(
113
151
['* ' ]
114
152
)
115
153
->where ('attribute_id IN (?) ' , $ attributeIds );
116
- if (!$ isMultiple ) {
117
- $ select ->where ($ metadata ->getLinkField () . ' = ? ' , $ entityId );
118
- } else {
119
- $ linkField = $ metadata ->getLinkField ();
120
- $ select ->joinInner (
121
- ['e_t ' => $ metadata ->getEntityTable ()],
122
- 't. ' . $ linkField . ' = e_t. ' . $ linkField ,
123
- []
124
- );
125
- $ select ->where ('e_t. ' . $ metadata ->getIdentifierField () . ' IN(?) ' , $ entityIds , \Zend_Db::INT_TYPE );
126
- }
154
+
155
+ $ linkField = $ metadata ->getLinkField ();
156
+ $ select ->joinInner (
157
+ ['e_t ' => $ metadata ->getEntityTable ()],
158
+ 't. ' . $ linkField . ' = e_t. ' . $ linkField ,
159
+ []
160
+ );
161
+ $ select ->where ('e_t. ' . $ metadata ->getIdentifierField () . ' IN(?) ' , $ entityIds , \Zend_Db::INT_TYPE );
162
+
127
163
if (!empty ($ storeIds )) {
128
164
$ select ->where (
129
165
'store_id IN (?) ' ,
@@ -140,36 +176,14 @@ private function getValuesImplementation(
140
176
$ select = reset ($ selects );
141
177
}
142
178
143
- if (!$ isMultiple ) {
144
- $ result = $ connection ->fetchAll ($ select );
145
- } else {
146
- foreach ($ connection ->fetchAll ($ select ) as $ row ) {
147
- $ result [$ row [$ metadata ->getLinkField ()]][$ row ['store_id ' ]] = $ row ['value ' ];
148
- }
179
+ foreach ($ connection ->fetchAll ($ select ) as $ row ) {
180
+ $ result [$ row [$ metadata ->getLinkField ()]][$ row ['store_id ' ]] = $ row ['value ' ];
149
181
}
150
182
}
151
183
152
184
return $ result ;
153
185
}
154
186
155
- /**
156
- * Bulk version of the getValues() for several entities
157
- *
158
- * @param string $entityType
159
- * @param int[] $entityIds
160
- * @param string[] $attributeCodes
161
- * @param int[] $storeIds
162
- * @return array
163
- */
164
- public function getValuesMultiple (
165
- string $ entityType ,
166
- array $ entityIds ,
167
- array $ attributeCodes = [],
168
- array $ storeIds = []
169
- ) : array {
170
- return $ this ->getValuesImplementation ($ entityType , 0 , $ attributeCodes , $ storeIds , $ entityIds , true );
171
- }
172
-
173
187
/**
174
188
* Delete attribute values
175
189
*
0 commit comments