0

I am developing an module in Magento 2.I have created helper,Model

Fatal error:vendorname\modulename\Model\Testclass class is not exist.I have tried each and everything to resolve this issue but not able to work it.

My code is:(This is not working.

 public function __construct(
        vendorname\modulename\Model\Testclass $testclassinject
        )

After I have do hit and try and inject this class with Factory keyword. (This is working.)

public function __construct(
    vendorname\modulename\Model\TestclassFactory $testclassinject
    )

Now this is working for me.

So my question is that is this required that each Model class will require to Factory class to inject another class?

Please help me this is creating confusing for me.

5
  • I think you have posted the question in a wrong way. vendorname/modulename/Model/Testclass.php should be \Vendorname\Modulename\Model\Testclass and vendorname/modulename/Model/TestclassFactory.php should be \vendorname\modulename\Model\TestclassFactory Apr 3, 2019 at 11:27
  • No, Factory keyword is not required.
    – fmsthird
    Apr 3, 2019 at 11:31
  • @MohitKumarArora,I have corrected but the main concern is why model class is not injecting without Factory.
    – akgola
    Apr 3, 2019 at 11:33
  • @akgola, still you have forgot to remove .php Apr 3, 2019 at 11:45

2 Answers 2

1

Finally I resolved the issue after lot of R&D.The main issue was the class which are injecting means Testclass.php ,there was an error an extra comma.Since the error was "class is not exist" hence I have not focused on this class and was focusing only class in which I am injecting.

Thanks all for helping me.

0

Your Question: each Model class will require to Factory class to inject another class?

Ans: No. It depends. If your model class represent a database entity than you have to use a Factory class to inject the model class.

Please read more https://devdocs.magento.com/guides/v2.3/extension-dev-guide/factories.html

3
  • then what could be possible region that class is not injection and throw fatal error class is not exist?
    – akgola
    Apr 3, 2019 at 11:46
  • Possible Reason 1) vendorname/modulename/Model/Testclass.php should be \Vendorname\Modulename\Model\Testclass and vendorname/modulename/Model/TestclassFactory.php should be \vendorname\modulename\Model\TestclassFactory Possible Reason 2) Is your module have any database entity (means Table attached). is yes then you have to use a factory class Apr 3, 2019 at 12:07
  • ,thanks for helping me.Issue ha been resolved.
    – akgola
    Apr 3, 2019 at 12:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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