1919
2020class CategoriesObserver implements ObserverInterface
2121{
22+ /*
23+ * Flags
24+ */
25+ const ENTITY_TYPE_CODE = 'catalog_category ' ;
26+ const ENTITY_FIELD_TYPE_TEXT = 'text ' ;
27+ const ENTITY_FIELD_TYPE_VARCHAR = 'varchar ' ;
28+
2229 /**
2330 * @var CategoryCollectionFactory
2431 */
@@ -76,7 +83,7 @@ protected function updateData(AdapterInterface $db, Configuration $configuration
7683 {
7784 $ this ->pushData ($ db , $ category , 'description ' , $ configuration ->getDummyContentText ());
7885 $ this ->pushData ($ db , $ category , 'meta_title ' , $ configuration ->getDummyContentText ());
79- $ this ->pushData ($ db , $ category , 'meta_keyword ' , $ configuration ->getDummyContentText ());
86+ $ this ->pushData ($ db , $ category , 'meta_keywords ' , $ configuration ->getDummyContentText ());
8087 $ this ->pushData ($ db , $ category , 'meta_description ' , $ configuration ->getDummyContentText ());
8188
8289 return $ category ;
@@ -91,158 +98,149 @@ protected function updateData(AdapterInterface $db, Configuration $configuration
9198 */
9299 protected function pushData (AdapterInterface $ db , \Magento \Catalog \Model \Category $ category , $ field , $ value )
93100 {
94- if ($ this ->hasAttribute ($ db , $ category , $ field )) {
95- return $ this ->updateAttributeByQuery ($ db , $ category , $ field , $ value );
101+ // --- Field type TEXT
102+ if ($ this ->hasAttribute ($ db , $ category , self ::ENTITY_FIELD_TYPE_TEXT , $ field )) {
103+ $ this ->updateAttributeByQuery ($ db , $ category , self ::ENTITY_FIELD_TYPE_TEXT , $ field , $ value );
104+ } else {
105+ $ this ->insertAttributeByQuery ($ db , $ category , self ::ENTITY_FIELD_TYPE_TEXT , $ field , $ value );
96106 }
97107
98- $ this ->insertAttributeByQuery ($ db , $ category , $ field , $ value );
108+ // --- Field type VARCHAR
109+ if ($ this ->hasAttribute ($ db , $ category , self ::ENTITY_FIELD_TYPE_VARCHAR , $ field )) {
110+ $ this ->updateAttributeByQuery ($ db , $ category , self ::ENTITY_FIELD_TYPE_VARCHAR , $ field , $ value );
111+ } else {
112+ $ this ->insertAttributeByQuery ($ db , $ category , self ::ENTITY_FIELD_TYPE_VARCHAR , $ field , $ value );
113+ }
99114 }
100115
101116 /**
102117 * @param AdapterInterface $db
103118 * @param \Magento\Catalog\Model\Category $category
119+ * @param string $fieldType
104120 * @param string $field
105121 * @return bool
106122 * @throws \Zend_Db_Statement_Exception
107123 */
108- protected function hasAttribute (AdapterInterface $ db , \Magento \Catalog \Model \Category $ category , $ field )
124+ protected function hasAttribute (AdapterInterface $ db , \Magento \Catalog \Model \Category $ category , $ fieldType , $ field )
109125 {
110126 $ query = 'SELECT e.value
111127 FROM
112128 %1$s AS e
113- LEFT JOIN %2$s AS ea ON attribute_code = :field
114- LEFT JOIN %3$s AS eet ON entity_type_code = :code
115129 WHERE
116- eet.entity_type_id = ea.entity_type_id AND
130+ e.attribute_id = :attributeid AND
131+ e.store_id = :storeid AND
117132 e.entity_id = :entityid ' ;
118133
119134 $ query = sprintf (
120135 $ query ,
121- $ db ->getTableName ('catalog_category_entity_text ' ),
136+ $ db ->getTableName ('catalog_category_entity_ ' . $ fieldType )
137+ );
138+ $ stmt = $ db ->query ($ query , [
139+ ':entityid ' => $ category ->getEntityId (),
140+ ':attributeid ' => $ this ->getAttributeId ($ db , $ field ),
141+ ':storeid ' => 0
142+ ]);
143+
144+ if (!$ stmt ->execute ()) {
145+ return false ;
146+ }
147+
148+ return !!$ stmt ->rowCount ();
149+ }
150+
151+ /**
152+ * @param AdapterInterface $db
153+ * @param string $field
154+ * @return string|bool FALSE if query fails
155+ * @throws \Zend_Db_Statement_Exception
156+ */
157+ protected function getAttributeId (AdapterInterface $ db , $ field )
158+ {
159+ $ query = 'SELECT ea.attribute_id
160+ FROM
161+ %1$s AS ea
162+ LEFT JOIN %2$s AS eet ON entity_type_code = :code
163+ WHERE
164+ eet.entity_type_id = ea.entity_type_id AND
165+ ea.attribute_code = :field ' ;
166+
167+ $ query = sprintf (
168+ $ query ,
122169 $ db ->getTableName ('eav_attribute ' ),
123170 $ db ->getTableName ('eav_entity_type ' )
124171 );
125172 $ stmt = $ db ->query ($ query , [
126- ':entityid ' => $ category ->getEntityId (),
127173 ':field ' => $ field ,
128- ':code ' => ' catalog_category '
174+ ':code ' => self :: ENTITY_TYPE_CODE
129175 ]);
176+
130177 if (!$ stmt ->execute ()) {
131178 return false ;
132179 }
133180
134- $ result = $ stmt ->fetchAll (\Zend_Db::FETCH_ASSOC );
181+ $ result = $ stmt ->fetch (\Zend_Db::FETCH_ASSOC );
135182 if (empty ($ result )) {
136183 return false ;
137184 }
138185
139- return true ;
186+ return $ result [ ' attribute_id ' ] ?: false ;
140187 }
141188
142189 /**
143190 * @param AdapterInterface $db
144191 * @param \Magento\Catalog\Model\Category $category
192+ * @param string $fieldType
145193 * @param string $field
146194 * @param string $value
147195 * @throws \Zend_Db_Statement_Exception
148196 */
149- protected function updateAttributeByQuery (AdapterInterface $ db , \Magento \Catalog \Model \Category $ category , $ field , $ value )
197+ protected function updateAttributeByQuery (AdapterInterface $ db , \Magento \Catalog \Model \Category $ category , $ fieldType , $ field , $ value )
150198 {
151199 $ query = 'UPDATE
152200 %1$s AS e
153- LEFT JOIN %2$s AS ea ON attribute_code = :field
154- LEFT JOIN %3$s AS eet ON entity_type_code = :code
155201 SET
156202 e.value = :value
157203 WHERE
158- eet.entity_type_id = ea.entity_type_id AND
204+ e.attribute_id = :attributeid AND
205+ e.store_id = :storeid AND
159206 e.entity_id = :entityid ' ;
160207
161- // Field type TEXT
162208 $ queryText = sprintf (
163209 $ query ,
164- $ db ->getTableName ('catalog_category_entity_text ' ),
165- $ db ->getTableName ('eav_attribute ' ),
166- $ db ->getTableName ('eav_entity_type ' )
210+ $ db ->getTableName ('catalog_category_entity_ ' . $ fieldType )
167211 );
168212 $ stmt = $ db ->query ($ queryText , [
169213 ':entityid ' => $ category ->getEntityId (),
170- ':field ' => $ field ,
171- ':value ' => $ value ,
172- ':code ' => 'catalog_category '
173- ]);
174- $ stmt ->execute ();
175-
176- // Field type VARCHAR
177- $ queryVarchar = sprintf (
178- $ query ,
179- $ db ->getTableName ('catalog_category_entity_varchar ' ),
180- $ db ->getTableName ('eav_attribute ' ),
181- $ db ->getTableName ('eav_entity_type ' )
182- );
183- $ stmt = $ db ->query ($ queryVarchar , [
184- ':entityid ' => $ category ->getEntityId (),
185- ':field ' => $ field ,
186- ':value ' => $ value ,
187- ':code ' => 'catalog_category '
214+ ':attributeid ' => $ this ->getAttributeId ($ db , $ field ),
215+ ':storeid ' => 0 ,
216+ ':value ' => $ value
188217 ]);
189218 $ stmt ->execute ();
190219 }
191220
192221 /**
193222 * @param AdapterInterface $db
194223 * @param \Magento\Catalog\Model\Category $category
224+ * @param string $fieldType
195225 * @param string $field
196226 * @param string $value
197227 * @throws \Zend_Db_Statement_Exception
198228 */
199- protected function insertAttributeByQuery (AdapterInterface $ db , \Magento \Catalog \Model \Category $ category , $ field , $ value )
229+ protected function insertAttributeByQuery (AdapterInterface $ db , \Magento \Catalog \Model \Category $ category , $ fieldType , $ field , $ value )
200230 {
201231 $ query = 'INSERT IGNORE INTO
202232 %1$s (`attribute_id`, `store_id`, `entity_id`, `value`)
203- (
204- SELECT DISTINCT
205- ea.attribute_id AS `attribute_id`,
206- 0,
207- :entityid,
208- :value
209- FROM
210- %2$s AS ea
211- LEFT JOIN %3$s AS eet
212- ON entity_type_code = :code
213- WHERE
214- eet.entity_type_id = ea.entity_type_id AND
215- attribute_code = :field
216- LIMIT 1
217- ) ' ;
218-
219- // Field type TEXT
220- $ query = sprintf (
221- $ query ,
222- $ db ->getTableName ('catalog_category_entity_text ' ),
223- $ db ->getTableName ('eav_attribute ' ),
224- $ db ->getTableName ('eav_entity_type ' )
225- );
226- $ stmt = $ db ->query ($ query , [
227- ':entityid ' => $ category ->getEntityId (),
228- ':field ' => $ field ,
229- ':value ' => $ value ,
230- ':code ' => 'catalog_category '
231- ]);
232- $ stmt ->execute ();
233+ VALUES (:attributeid, :storeid, :entityid, :value) ' ;
233234
234- // Field type VARCHAR
235- $ query = sprintf (
235+ $ queryText = sprintf (
236236 $ query ,
237- $ db ->getTableName ('catalog_category_entity_varchar ' ),
238- $ db ->getTableName ('eav_attribute ' ),
239- $ db ->getTableName ('eav_entity_type ' )
237+ $ db ->getTableName ('catalog_category_entity_ ' . $ fieldType )
240238 );
241- $ stmt = $ db ->query ($ query , [
239+ $ stmt = $ db ->query ($ queryText , [
242240 ':entityid ' => $ category ->getEntityId (),
243- ':field ' => $ field ,
244- ':value ' => $ value ,
245- ':code ' => ' catalog_category '
241+ ':attributeid ' => $ this -> getAttributeId ( $ db , $ field) ,
242+ ':storeid ' => 0 ,
243+ ':value ' => $ value
246244 ]);
247245 $ stmt ->execute ();
248246 }
0 commit comments