Question

Playing around with Magento 2 but run into a problem I can not wrap my head around. The object manager tries to instantiate Abstract class, this gives an Fatal Error. Does someone know what might cause this or point me in the right direction?

The Error

Fatal error: Cannot instantiate abstract class Magento\Framework\Model\Resource\AbstractResource in /lib/internal/Magento/Framework/ObjectManager/Factory/Dynamic/Developer.php on line 75

The Problem

My module has a model that extends the \Magento\Quote\Model\Quote. The constructor of this model has the following line injecting this:

\Magento\Framework\Model\Resource\AbstractResource $resource = null,.

The controller that is instantiating the model extends the \Magento\Backend\App\Action and uses $this->_objectManager->create() to instantiate the model.

The thing I can not wrap my head around is if I instantiate the Magento model it just works but if I instantiate my model it tries to instantiate this abstract class throwing the error above. What tells the object manager to do this and how can I solve my problem?

My first thought was Magento uses the di.xml to replace this, execute this or something, this was not the case.

Thanks for taking the time to read this. Any help or input on this issue is appreciated.

Was it helpful?

Solution

This can happen if your sub-class added new dependencies after the existing optional dependencies of the parent class.

Snippet from the parent

    \Magento\Quote\Model\Cart\CurrencyFactory $currencyFactory,  // required
    JoinProcessorInterface $extensionAttributesJoinProcessor,    // required
    \Magento\Framework\Model\Resource\AbstractResource $resource = null,       //optional
    \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,  //optional
    array $data = [] //optional
)}

How code might alter what is optional

    \Magento\Quote\Model\Cart\CurrencyFactory $currencyFactory,  // required
    JoinProcessorInterface $extensionAttributesJoinProcessor,    // required
    \Magento\Framework\Model\Resource\AbstractResource $resource = null,       // required (even though it has a default value, because it is followed by parameters that don't have default values)
    int $x, // required
    \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,  // optional (still optional because there are no required parameters following it)
    array $data = [] // optional
)}

The Magento 2 Object Manager will try to inject any required parameters. So this could happen if you added a required parameter at the end of the constructor.

You can fix this by moving any new required parameter up above the optional ones.

OTHER TIPS

Sometimes some module was updated and the references under /generated were not updated yet.

Just do a bin/magento setup:di:compile and try again.

(Worked for me when updating a payment module and /checkout didn't load)

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top