1

I am getting this error on my homepage after upgrading my Magento version from 2.4.0 to 2.4.6.

Error: Cannot instantiate abstract class Magento\Framework\Data\Collection\AbstractResource in /home/websites/hollywoo/dev/public_html/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php:121 Stack trace: #0 /home/websites/hollywoo/dev/public_html/vendor/magento/framework/ObjectManager/Factory/Compiled.php(108): Magento\Framework\ObjectManager\Factory\AbstractFactory->createObject() #1 /home/websites/hollywoo/dev/public_html/vendor/magento/framework/ObjectManager/Factory/Compiled.php(150): Magento\Framework\ObjectManager\Factory\Compiled->create() #2 /home/websites/hollywoo/dev/public_html/vendor/magento/framework/ObjectManager/Factory/Compiled.php(79): Magento\Framework\ObjectManager\Factory\Compiled->get() #3 /home/websites/hollywoo/dev/public_html/vendor/magento/framework/ObjectManager/ObjectManager.php(56): Magento\Framework\ObjectManager\Factory\Compiled->create() #4 /home/websites/hollywoo/dev/public_html/generated/code/Jes/BannerProductSlider/Model/SliderFactory.php(43): Magento\Framework\ObjectManager\ObjectManager->create() #5 /home/websites/hollywoo/dev/public_html/app/code/Jes/BannerProductSlider/Helper/Data.php(193): Jes\BannerProductSlider\Model\SliderFactory->create() #6 /home/websites/hollywoo/dev/public_html/app/code/Jes/DynamicPages/Block/Pages/HomePage.php(110): Jes\BannerProductSlider\Helper\Data->getActiveSliders() #7 /home/websites/hollywoo/dev/public_html/app/code/Jes/DynamicPages/view/frontend/templates/bannerslider.phtml(27): Jes\DynamicPages\Block\Pages\HomePage->getActiveSliders()

I know it is because of the model code. Providing model code of Jes extension

<?php
namespace Jes\BannerProductSlider\Model;

use Magento\Framework\Data\Collection\AbstractDb;
use Magento\Framework\Model\AbstractModel;
use Magento\Framework\Model\Context;
use Magento\Framework\Model\ResourceModel\AbstractResource;
use Magento\Framework\Registry;
use Jes\BannerProductSlider\Model\ResourceModel\Banner\Collection;
use Jes\BannerProductSlider\Model\ResourceModel\Banner\CollectionFactory;
use Magento\Framework\App\ResourceConnection;
class Slider extends AbstractModel
{
    /**
     * Cache tag
     *
     * @var string
     */
    const CACHE_TAG = 'jes_bannerproductslider_slider';

    /**
     * Cache tag
     *
     * @var string
     */
    protected $_cacheTag = 'jes_bannerproductslider_slider';

    /**
     * Event prefix
     *
     * @var string
     */
    protected $_eventPrefix = 'jes_bannerproductslider_slider';

    /**
     * Banner Collection
     *
     * @var Collection
     */
    protected $bannerCollection;

    /**
     * Banner Collection Factory
     *
     * @var CollectionFactory
     */
    protected $bannerCollectionFactory;

     protected $_resourceConnection;

    /**
     * constructor
     *
     * @param CollectionFactory $bannerCollectionFactory
     * @param Context $context
     * @param Registry $registry
     * @param AbstractResource $resource
     * @param AbstractDb $resourceCollection
     * @param array $data
     */
    public function __construct(
        CollectionFactory $bannerCollectionFactory,
        Context $context,
        Registry $registry,
        AbstractResource $resource = null,
        AbstractDb $resourceCollection = null,
        ResourceConnection $resourceConnection,
        array $data = []
    ) {
        $this->bannerCollectionFactory = $bannerCollectionFactory;
        $this->_resourceConnection =$resourceConnection; 
        parent::__construct($context, $registry, $resource, $resourceCollection, $data);
    }

    /**
     * Initialize resource model
     *
     * @return void
     */
    protected function _construct()
    {
        $this->_init('Jes\BannerProductSlider\Model\ResourceModel\Slider');
    }

    /**
     * Get identities
     *
     * @return array
     */
    public function getIdentities()
    {
        return [self::CACHE_TAG . '_' . $this->getId()];
    }

    /**
     * get entity default values
     *
     * @return array
     */
    public function getDefaultValues()
    {
        $values = [];
        $values['status'] = '1';

        return $values;
    }

    /**
     * @return array|mixed
     */
    public function getBannersPosition()
    {
        if (!$this->getId()) {
            return [];
        }

        $array = $this->getData('banners_position');
        if ($array === null) {
            $array = $this->getResource()->getBannersPosition($this);
            $this->setData('banners_position', $array);
        }

        return $array;
    }

    /**
     * @return Collection
     */
    public function getSelectedBannersCollection()
    {
        if ($this->bannerCollection === null) {
            $collection = $this->bannerCollectionFactory->create();
            $collection->getSelect()->join(
                ['banner_slider' => $this->getResource()->getTable('jes_bannerproductslider_banner_slider')],
                'main_table.banner_id=banner_slider.banner_id AND banner_slider.slider_id=' . $this->getId(),
                ['position']
            );
            $this->bannerCollection = $collection;
        }

        return $this->bannerCollection;
    }
    public function deleteBanners($sliderId)
    {
        $this->_resourceConnection->getConnection()->delete('jes_bannerproductslider_banner_slider',["slider_id ", $sliderId]);
    }
}

1 Answer 1

0

Optional parameter is provided before required.

AbstractDb $resourceCollection = null,
ResourceConnection $resourceConnection

Therefore, if ResourceConnection $resourceConnection is a required parameter, it should come before the parameter with the optional value AbstractDb $resourceCollection = null. Based on this, your constructor should look like the following:

public function __construct(
    CollectionFactory $bannerCollectionFactory,
    Context $context,
    Registry $registry,
    ResourceConnection $resourceConnection,
    AbstractResource $resource = null,
    AbstractDb $resourceCollection = null,
    array $data = []
) {
    $this->bannerCollectionFactory = $bannerCollectionFactory;
    $this->_resourceConnection =$resourceConnection; 
    parent::__construct($context, $registry, $resource, $resourceCollection, $data);
}
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.