2

Fatal error: Uncaught Error: Cannot instantiate abstract class Magento\Framework\Model\ResourceModel\AbstractResource in /home/dukaania/public_html/testing2/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php:50 Stack trace: #0

Here's my code:

/home1/dukaania/public_html/testing2/app/code/Sugarcode/Test/Model/Total/Fee.php

<?php
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Sugarcode\Test\Model\Total;
class Fee extends \Magento\Framework\Model\AbstractModel implements \Magento\Framework\DataObject\IdentityInterface {
 const CACHE_TAG = 'mr_dukaaniatest';
protected $_cacheTag = 'mr_dukaaniatest';
protected $_eventPrefix = 'mr_dukaaniatest';



protected function _construct()
{
    $this->_init('Sugarcode\Test\Model\ResourceModel\Fee.php');
}

/**
 * Collect grand total address amount
 *
 * @param \Magento\Quote\Model\Quote $quote
 * @param \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment
 * @param \Magento\Quote\Model\Quote\Address\Total $total
 * @return $this
 */

   protected $_feeFactory;



public function __construct(
        \Magento\Framework\Model\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\Model\ResourceModel\AbstractResource $resource= null,
        \Magento\Framework\Data\Collection\AbstractDb $resourceCollection= null,
            \Sugarcode\Test\Model\Total\FeeFactory $feeFactory,

        array $data = array()
    ) {
        parent::__construct($context, $registry, $resource, $resourceCollection, $data);
            $this->_feeFactory = $feeFactory;

    }

public function collect(

) {
    parent::collect($quote, $shippingAssignment, $total);



}






public function getIdentities()
{
    $identities = [
        self::CACHE_TAG . '_' . $this->getId(),
    ];
    if ($this->hasDataChanges() || $this->isDeleted()) {
        $identities[] = Product::CACHE_PRODUCT_CATEGORY_TAG . '_' . $this->getId();
    }
    return $identities;
}



 public function fetch() {      
    $fee = $this->_feeFactory->create();
    $collection = $fee->getCollection();
    foreach($collection as $item){
        \Zend_Debug::dump($item->getData());
    }
    exit();
}

  public function execute()
{
    $fee = $this->_feeFactory->create();
    $collection = $fee->getCollection();
    foreach($collection as $item){
        \Zend_Debug::dump($item->getData());
    }
    exit();
}

/**
 * Get Subtotal label
 *
 * @return \Magento\Framework\Phrase
 */
public function getLabel() {
    return __('Fee');
}
}

/home1/dukaania/public_html/testing2/app/code/Sugarcode/Test/Model/ResourceModel/Fee.php

<?php
namespace Sugarcode\Test\Model\ResourceModel;


class Fee extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{

    public function __construct(
        \Magento\Framework\Model\ResourceModel\Db\Context $context
    )
    {
        parent::__construct($context);
    }

    protected function _construct()
    {
        $this->_init('mr_dukaaniatest', 'dukaaniatest_id');
    }

}

/home1/dukaania/public_html/testing2/app/code/Sugarcode/Test/Model/ResourceModel/Fee/Collection.php

<?php
namespace Sugarcode\Test\Model\ResourceModel\Fee;

class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
    protected $_idFieldName = 'dukaaniatest_id';
    protected $_eventPrefix = 'mr_dukaaniatest_collection';
    protected $_eventObject = 'fee_collection';

    /**
     * Define resource model
     *
     * @return void
     */
    protected function _construct()
    {
        $this->_init('Sugarcode\Test\Model\Total\Fee', 'Sugarcode\Test\Model\ResourceModel\Fee');
    }

}

/home1/dukaania/public_html/testing2/app/code/Sugarcode/Test/Block/Sales/Order/Fee.php

<?php
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

/**
 * Tax totals modification block. Can be used just as subblock of \Magento\Sales\Block\Order\Totals
 */
namespace Sugarcode\Test\Block\Sales\Order;



class Fee extends \Magento\Framework\View\Element\Template
{
    /**
     * Tax configuration model
     *
     * @var \Magento\Tax\Model\Config
     */
    protected $_config;

    /**
     * @var Order
     */
    protected $_order;

    /**
     * @var \Magento\Framework\DataObject
     */
    protected $_source;

    /**
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param \Magento\Tax\Model\Config $taxConfig
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Tax\Model\Config $taxConfig,
        array $data = []
    ) {
        $this->_config = $taxConfig;
        parent::__construct($context, $data);
    }

    /**
     * Check if we nedd display full tax total info
     *
     * @return bool
     */
    public function displayFullSummary()
    {
        return true;
    }

    /**
     * Get data (totals) source model
     *
     * @return \Magento\Framework\DataObject
     */
    public function getSource()
    {
        return $this->_source;
    } 
    public function getStore()
    {
        return $this->_order->getStore();
    }

      /**
     * @return Order
     */
    public function getOrder()
    {
        return $this->_order;
    }

    /**
     * @return array
     */
    public function getLabelProperties()
    {
        return $this->getParentBlock()->getLabelProperties();
    }

    /**
     * @return array
     */
    public function getValueProperties()
    {
        return $this->getParentBlock()->getValueProperties();
    }

    /**
     * Initialize all order totals relates with tax
     *
     * @return \Magento\Tax\Block\Sales\Order\Tax
     */
     public function initTotals()
    {

        $parent = $this->getParentBlock();
        $this->_order = $parent->getOrder();
        $this->_source = $parent->getSource();

        $store = $this->getStore();

        $fee = new \Magento\Framework\DataObject(
                [
                    'code' => 'fee',
                    'strong' => false,
                    'value' => 120,
                    //'value' => $this->_source->getFee(),
                    'label' => __('Fee'),
                ]
            );

            $parent->addTotal($fee, 'fee');
           // $this->_addTax('grand_total');


            return $this;



    }


public function getIdentities()
{
    return $this->getCurrentCategory()->getIdentities();
}



}
5
  • which magento varsion you used ? Commented Oct 3, 2018 at 9:55
  • magento 2.0 version Commented Oct 3, 2018 at 9:56
  • Check constructor of this file "\Magento\Framework\Model\AbstractModel" and set that constructor here. Commented Oct 3, 2018 at 9:57
  • please remove constructor from your resource model fee.php Commented Oct 3, 2018 at 10:07
  • i removed constructor but nothing happen same issue Commented Oct 3, 2018 at 10:24

4 Answers 4

1

For anyone seeing this error recurring and having to di:compile and rm -rf generated their way out of it repeatedly, check your layout XML files for any blocks using an Abstract class.

In my case, I had used:

class="Magento\Catalog\Block\Product\View\AbstractView"

When it should have just been:

class="Magento\Catalog\Block\Product\View"

Surprising that it ever worked, to be honest, but making the above change solved it entirely for me.

1

This issue is particularly related to the optional parameter that you are setting in the public function __construct.

In this particular case the original constructor parameters are like this (which is incorrect and not allowed anymore):

public function __construct(
        \Magento\Framework\Model\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\Model\ResourceModel\AbstractResource $resource= null,
        \Magento\Framework\Data\Collection\AbstractDb $resourceCollection= null,
        \Sugarcode\Test\Model\Total\FeeFactory $feeFactory,
        array $data = array()
    ) {
        parent::__construct($context, $registry, $resource, $resourceCollection, $data);
            $this->_feeFactory = $feeFactory;
    }

The correct way is to include all the optional parameters in the last:

public function __construct(
        \Magento\Framework\Model\Context $context,
        \Magento\Framework\Registry $registry,
        \Sugarcode\Test\Model\Total\FeeFactory $feeFactory,
        \Magento\Framework\Model\ResourceModel\AbstractResource $resource= null,
        \Magento\Framework\Data\Collection\AbstractDb $resourceCollection= null,
        array $data = array()
    ) {
        parent::__construct($context, $registry, $resource, $resourceCollection, $data);
            $this->_feeFactory = $feeFactory;
    }

Let me know if it helps.

0

please replace your consturct code in below file /home1/dukaania/public_html/testing2/app/code/Sugarcode/Test/Model/Total/Fee.php

       public function __construct(
    \Magento\Framework\Model\Context $context,
    \Magento\Framework\Registry $registry,
     \Sugarcode\Test\Model\Total\FeeFactory $feeFactory,
    \Magento\Framework\Model\ResourceModel\AbstractResource $resource= null,
    \Magento\Framework\Data\Collection\AbstractDb $resourceCollection= null,
      array $data = array()
) {
    parent::__construct($context, $registry, $resource, $resourceCollection, 
      $data);
        $this->_feeFactory = $feeFactory;

}

after replacing run command : rm -rf generated/* and clear cache and check

4
  • still m getting this issue Exception #0 (ReflectionException): Class Sugarcode\Test\Model\ResourceModel\Fee.php does not exist Commented Oct 3, 2018 at 10:03
  • please make sure your Fee.php in ResourceModel folder not in /Sugarcode/Test/Model/ResourceModel/Total/Fee.php Commented Oct 3, 2018 at 10:25
  • /home1/dukaania/public_html/testing2/app/code/Sugarcode/Test/Model/ResourceModel/Fee.php /home1/dukaania/public_html/testing2/app/code/Sugarcode/Test/Model/ResourceModel/Fee/Collection.php /home1/dukaania/public_html/testing2/app/code/Sugarcode/Test/Model/ResourceModel/Fee.php /home1/dukaania/public_html/testing2/app/code/Sugarcode/Test/Model/Total/Fee.php Commented Oct 3, 2018 at 10:32
  • @jhonjhondoe what you have asked in question is perfectly answered over here.if you have another issue you can asked another question. Commented Oct 4, 2018 at 4:21
0

Please replace your this "Sugarcode/Test/Model/Total/Fee.php" model file function _construct.

protected function _construct()
{
    $this->_init('Sugarcode\Test\Model\ResourceModel\Fee');
}

and check after that.

7
  • after adding this or replace this code admin panel not login redirect to same admin panel login screen Commented Oct 3, 2018 at 10:55
  • try to run command and clearing cache and then check it becuase your code issue has been fixed if you put above code Commented Oct 3, 2018 at 10:56
  • i run all commands but still same issue my admin panel not login redirect to same login screen after login Commented Oct 3, 2018 at 11:02
  • also product still not added in cart Commented Oct 3, 2018 at 11:02
  • can we discuss in chat please Commented Oct 3, 2018 at 11:03

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.