magefan / module-blog

Magento 2 Blog Extension is a solution that helps you to create a blog on Magento and manage posts,categories, tags, authors, and comments.
https://magefan.com/magento2-blog-extension
Other
265 stars 136 forks source link

"Call to a member function getItemById() on array" when viewing frontend posts #577

Closed Larke12 closed 6 months ago

Larke12 commented 6 months ago

Describe the bug After updating the composer package to 2.11.3 (from 2.11.0.1) the frontend blogs stopped loading, throwing the following error:

Error: Call to a member function getItemById() on array in .../vendor/magefan/module-blog/Model/UrlResolver.php:190

This appears to be due to the function \Magefan\Blog\Model\Post::getParentCategories no longer returning a collection, but an array. Reverting this function via an override to use the old code resolves this issue.

Preconditions:

To Reproduce Steps to reproduce the behavior:

  1. Go to blog post list on frontend
  2. Click on any blog post
  3. See error

Expected behavior The blog post loads without issue.

Actual behavior The blog post does not load at all and throws only an exception with stack trace.

lohoy commented 6 months ago

Hello!

I fix the issue with this patch :

Index: Model/Post.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/Model/Post.php b/Model/Post.php
--- a/Model/Post.php
+++ b/Model/Post.php    (date 1714034007690)
@@ -668,7 +668,7 @@
                     try {
                         $category = $this->categoryRepository->getById($categoryId);
                         if ($category->getId() && $category->isVisibleOnStore($this->getStoreId())) {
-                            $this->_parentCategories[] = $category;
+                            $this->_parentCategories[$categoryId] = $category;
                         }
                     } catch (NoSuchEntityException $e) { }
                 }

Index: Model/UrlResolver.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/Model/UrlResolver.php b/Model/UrlResolver.php
--- a/Model/UrlResolver.php
+++ b/Model/UrlResolver.php (date 1714034007682)
@@ -187,7 +187,11 @@
                             $factory = Url::CONTROLLER_POST . 'Factory';
                             $model = $this->$factory->create()->load($postId);

-                            if (!$model->getCategoriesCount() || !$model->getParentCategories()->getItemById($categoryId)) {
+                            if (
+                                !$model->getCategoriesCount()
+                                || (array_key_exists($categoryId, $model->getParentCategories())
+                                    && !$model->getParentCategories()[$categoryId])
+                            ) {
                                 return null;
                             }
                         }
magefan commented 6 months ago

Thank you for your report. To fix this issue, please use this fix https://github.com/magefan/module-blog/commit/195e9261cbc4eaf6efc1bbd26a07950992636573 or update the extension to the newer version 2.11.3.1 (https://github.com/magefan/module-blog/releases/tag/2.11.3.1)