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.
3 Answers 3
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.
-
My issue was that when using auto-complete in phpStorm it failed to also bring with the = null part :/OZZIE– OZZIE2017年09月26日 12:37:48 +00:00Commented Sep 26, 2017 at 12:37
-
surprising i have same issue in one of the server but same code is working fine in other server. not sure what is the issue ? why same code is working one of the server not in otherPradeep Kumar– Pradeep Kumar2020年06月23日 11:46:44 +00:00Commented Jun 23, 2020 at 11:46
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)
I encountered the similar issue while Magento upgrade. I had several custom modules and 3rd party modules. The class Magento\Framework\Model\Resource\AbstractResource were used in many places. I checked all the places(files) and moved the required parameters to the top of the constructor(next to parent constructor parameters). Kept optional parameters at the bottom of the constructor. Then it solved the issue.
For example:
It was
public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Framework\App\Config\ScopeConfigInterface $config,
\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
\Magento\Catalog\Model\ResourceModel\Category $categoryResource,
\Magento\Catalog\Model\CategoryRepository $categoryRepository,
array $data = []
)
Now, with the fixes
public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Framework\App\Config\ScopeConfigInterface $config,
\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
\Magento\Catalog\Model\ResourceModel\Category $categoryResource,
\Magento\Catalog\Model\CategoryRepository $categoryRepository,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = []
)
Explore related questions
See similar questions with these tags.
__constructmethod of your model.