I want to initialize some data on factory->create()
without passing in the create(["mydata" => 1])
.
What's the way to create MyFactoryClass
with custom create()
for MyClass
in my module?
I find not dev doc for the same or any article.
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityLet's say that your class for which you want the factory is this Vendor\Module\Model\MyClass
.
now you can create the class Vendor\Module\Model\MyClassFactory
in the same folder as your initial class.
namespace Vendor\Module\Model;
class MyClassFactory implements MyInterface
{
.....
public function create()
{
......
}
}
class MyInterfaceFactory extends MyClassFactory
{
// this class is added so that there no Type Error
// while using MyInterfaceFactory/MyClassFactory
}
Creating this means that Magento will not autogenerate the factory class for your class anymore. It will use your factory instead.
MyClass
is implemented by MyInterface
so MyInterfaceFactory
needs to be mapped in di.xml
to my custom MyClassFactory
?
Aug 31, 2020 at 13:26
Magento/Framework/Exception/RuntimeException with message 'Type Error occurred when creating object:
Aug 31, 2020 at 13:32