Skip to content

Commit 195ad8f

Browse files
committed
Fix graphql get posts from child categories when filtering by category
1 parent dfa9341 commit 195ad8f

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

Model/ResourceModel/Post/Collection.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\Ab
3939
* @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
4040
* @param \Magento\Framework\Event\ManagerInterface $eventManager
4141
* @param \Magento\Framework\Stdlib\DateTime\DateTime $date
42-
* @param Magento\Store\Model\StoreManagerInterface $storeManager
42+
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
4343
* @param null|\Zend_Db_Adapter_Abstract $connection
4444
* @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
45+
* @param \Magefan\Blog\Model\CategoryFactory|null $category
4546
*/
4647
public function __construct(
4748
\Magento\Framework\Data\Collection\EntityFactory $entityFactory,
@@ -51,11 +52,17 @@ public function __construct(
5152
\Magento\Framework\Stdlib\DateTime\DateTime $date,
5253
\Magento\Store\Model\StoreManagerInterface $storeManager,
5354
$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
5557
) {
5658
parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
5759
$this->_date = $date;
5860
$this->_storeManager = $storeManager;
61+
62+
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
63+
$this->category = $category ?: $objectManager->create(
64+
\Magefan\Blog\Model\Category::class
65+
);
5966
}
6067

6168
/**
@@ -228,7 +235,6 @@ public function addCategoryFilter($category)
228235
$categories[$k] = $id;
229236
}
230237
}
231-
232238
} else {
233239
$select = $connection->select()
234240
->from(['t' => $tableName], 'category_id')
@@ -237,11 +243,19 @@ public function addCategoryFilter($category)
237243
. ' OR ' .
238244
$connection->prepareSqlCondition('t.category_id', $categories)
239245
);
240-
246+
241247
$categories = [];
242248
foreach ($connection->fetchAll($select) as $item) {
243249
$categories[] = $item['category_id'];
244250
}
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+
}
245259
}
246260

247261
$this->addFilter('category', ['in' => $categories], 'public');
@@ -313,7 +327,7 @@ public function addSearchFilter($term)
313327
$this->addExpressionFieldToSelect(
314328
'search_rate',
315329
'(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)
317331
+ IF(main_table.post_id IN (' . implode(',', $tagPostIds) . '), "1", "0"))',
318332
[
319333
'term' => $this->getConnection()->quote($term)
@@ -367,7 +381,7 @@ public function addTagFilter($tag)
367381

368382
$connection = $this->getConnection();
369383
$tableName = $this->getTable('magefan_blog_tag');
370-
384+
371385
if (is_numeric(key($tag))) {
372386
foreach ($tag as $k => $id) {
373387
if (!is_numeric($id)) {
@@ -391,7 +405,7 @@ public function addTagFilter($tag)
391405
. ' OR ' .
392406
$connection->prepareSqlCondition('t.tag_id', $tag)
393407
);
394-
408+
395409
$tag = [];
396410
foreach ($connection->fetchAll($select) as $item) {
397411
$tag[] = $item['tag_id'];

0 commit comments

Comments
 (0)