@@ -39,9 +39,10 @@ class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\Ab
39
39
* @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
40
40
* @param \Magento\Framework\Event\ManagerInterface $eventManager
41
41
* @param \Magento\Framework\Stdlib\DateTime\DateTime $date
42
- * @param Magento\Store\Model\StoreManagerInterface $storeManager
42
+ * @param \ Magento\Store\Model\StoreManagerInterface $storeManager
43
43
* @param null|\Zend_Db_Adapter_Abstract $connection
44
44
* @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
45
+ * @param \Magefan\Blog\Model\CategoryFactory|null $category
45
46
*/
46
47
public function __construct (
47
48
\Magento \Framework \Data \Collection \EntityFactory $ entityFactory ,
@@ -51,11 +52,17 @@ public function __construct(
51
52
\Magento \Framework \Stdlib \DateTime \DateTime $ date ,
52
53
\Magento \Store \Model \StoreManagerInterface $ storeManager ,
53
54
$ connection = null ,
54
- \Magento \Framework \Model \ResourceModel \Db \AbstractDb $ resource = null
55
+ \Magento \Framework \Model \ResourceModel \Db \AbstractDb $ resource = null ,
56
+ \Magefan \Blog \Model \CategoryFactory $ category = null
55
57
) {
56
58
parent ::__construct ($ entityFactory , $ logger , $ fetchStrategy , $ eventManager , $ connection , $ resource );
57
59
$ this ->_date = $ date ;
58
60
$ this ->_storeManager = $ storeManager ;
61
+
62
+ $ objectManager = \Magento \Framework \App \ObjectManager::getInstance ();
63
+ $ this ->category = $ category ?: $ objectManager ->create (
64
+ \Magefan \Blog \Model \Category::class
65
+ );
59
66
}
60
67
61
68
/**
@@ -228,7 +235,6 @@ public function addCategoryFilter($category)
228
235
$ categories [$ k ] = $ id ;
229
236
}
230
237
}
231
-
232
238
} else {
233
239
$ select = $ connection ->select ()
234
240
->from (['t ' => $ tableName ], 'category_id ' )
@@ -237,11 +243,19 @@ public function addCategoryFilter($category)
237
243
. ' OR ' .
238
244
$ connection ->prepareSqlCondition ('t.category_id ' , $ categories )
239
245
);
240
-
246
+
241
247
$ categories = [];
242
248
foreach ($ connection ->fetchAll ($ select ) as $ item ) {
243
249
$ categories [] = $ item ['category_id ' ];
244
250
}
251
+
252
+ if (1 === count ($ categories )) {
253
+ /* Fix for graphQL to get posts from child categories when filtering by category */
254
+ $ this ->category ->load ($ categories [0 ]);
255
+ if ($ this ->category ->getId ()) {
256
+ return $ this ->addCategoryFilter ($ this ->category );
257
+ }
258
+ }
245
259
}
246
260
247
261
$ this ->addFilter ('category ' , ['in ' => $ categories ], 'public ' );
@@ -313,7 +327,7 @@ public function addSearchFilter($term)
313
327
$ this ->addExpressionFieldToSelect (
314
328
'search_rate ' ,
315
329
'(0
316
- + FORMAT(MATCH (title, meta_keywords, meta_description, identifier, content) AGAINST ("{{term}}"), 4)
330
+ + FORMAT(MATCH (title, meta_keywords, meta_description, identifier, content) AGAINST ("{{term}}"), 4)
317
331
+ IF(main_table.post_id IN ( ' . implode (', ' , $ tagPostIds ) . '), "1", "0")) ' ,
318
332
[
319
333
'term ' => $ this ->getConnection ()->quote ($ term )
@@ -367,7 +381,7 @@ public function addTagFilter($tag)
367
381
368
382
$ connection = $ this ->getConnection ();
369
383
$ tableName = $ this ->getTable ('magefan_blog_tag ' );
370
-
384
+
371
385
if (is_numeric (key ($ tag ))) {
372
386
foreach ($ tag as $ k => $ id ) {
373
387
if (!is_numeric ($ id )) {
@@ -391,7 +405,7 @@ public function addTagFilter($tag)
391
405
. ' OR ' .
392
406
$ connection ->prepareSqlCondition ('t.tag_id ' , $ tag )
393
407
);
394
-
408
+
395
409
$ tag = [];
396
410
foreach ($ connection ->fetchAll ($ select ) as $ item ) {
397
411
$ tag [] = $ item ['tag_id ' ];
0 commit comments