0

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 )

  • notice that protected function _constructor() { in the modelname.php should be protected function _construct() – ThatOneGuyThatDontGetIt Nov 10 '18 at 14:59
0

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.

  • Thank you for your answer, silly me didn't notice it at all. since you are such a good help do you mind if I ask you another question. Now I get the error Exception #0 (Magento\Framework\Exception\LocalizedException): Model collection resource name is not defined. Do you see any other mistakes I have made in de code in the question – ThatOneGuyThatDontGetIt Nov 10 '18 at 14:41
  • in which line you got this error – Ramkishan Suthar Nov 10 '18 at 15:14
  • I found the error by myself and posted in a comment under the question but still thanks! – ThatOneGuyThatDontGetIt Nov 10 '18 at 16:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.