Magento – Fatal error: Uncaught Error: Cannot instantiate abstract class Magento\Framework\Model\ResourceModel\AbstractResource

magento2

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();
}



}

Best Answer

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

Related Topic