invalid create method
-
06-02-2021 - |
Question
I have a model, I call it in a block and when I use the create method I get an error saying:
Exception #0 (Magento\Framework\Exception\LocalizedException): Invalid method Vendor\Namespace\Model\Modelname::create
The Block (/app/code/Vendor/Namespace/Block/Blockname.php
) look like this:
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Vendor\Namespace\Model\Modelname $modelFactory,
array $data = []
)
{
$this->_modelFactory = $modelFactory;
parent::__construct($context, $data);
}
public function getAllJohns(){
$modelFactory = $this->_modelFactory->create()->getCollection();
}
The Model (/app/code/Vendor/Namespace/Model/Modelname.php
) looks like this:
const CACHE_TAG = 'namespace_Modelname';
protected $_cacheTag = 'vendor_namespace_modelname';
protected $_eventPrefix = 'vendor_namespace_modelname';
protected function _constructor() {
$this->_init('Vendor\Namespace\Model\ResourceModel\Modelname');
}
//lots of getters and setters down here
of course it extends: \Magento\Framework\Model\AbstractModel
. And implements 2 interfaces 1 custom and the other one is Magento\Framework\DataObject\IdentityInterface
.
Then we have the resourcemodel (/app/code/Vendor/Namespace/Model/ResourceModel/Modelname.php
):
public function __construct(
\Magento\Framework\Model\ResourceModel\Db\Context $context
) {
parent::__construct($context);
}
protected function _construct() {
$this->_init('vendor_namespace_model', 'id');
}
And it extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
and last but not least the collection (/app/code/Vendor/Namespace/Model/ResourceModel/Modelname/Collection.php
):
protected $_idFieldName = 'id';
protected $_eventPrefix = 'vendor_namespace_modelname';
protected $_eventObject = 'modelname_collection';
protected function _construct()
{
$this->_init('Vendor\Namespace\Model\Modelname',
'Vendor\Namespace\Model\ResourceModel\Modelname');
}
and yes it extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
The rest should be good because if i change the function getAllJohns to return a function from the model that returns a string there is no error/conflict and I can see the string:
public function getAllJohns(){
$modelFactory = $this->_modelFactory->sayHello(); // returns hello
}
So far i have tried to rebuild the module, follow multiple tutorials, look at other models in the app, cache:clean, cache:flush, setup:upgrade, setup:di:compile. ( error.log is empty )
Solution
To use class as a factory you need to suffix your model class with Factory like below.
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Vendor\Namespace\Model\ModelnameFactory $modelFactory,
array $data = []
)
{
$this->_modelFactory = $modelFactory;
parent::__construct($context, $data);
}
Then defined your function like below
public function getAllJohns(){
$modelFactory = $this->_modelFactory->create()->sayHello(); // returns hello
}
Let me know if any further explanation needed.