Questions tagged [factory]

Denotes questions related to Magento 2 factories.

Filter by
Sorted by
Tagged with
3
votes
2answers
101 views

Magento 2.2.5: Add Student Instead of Update Student

So instead of update the student, this code add a new student. I'm looking for a way that not using object manager, because the objectmanager is recommended not to use. So when i searching for how to ...
1
vote
3answers
5k views

Magento 2.2.5: Exception #0 (InvalidArgumentException): Invalid return type

i'm doing update "students" on magento 2.2.5 and i'm stuck in how to get student data by id the error say: 1 exception(s): Exception #0 (InvalidArgumentException): Invalid return type here is my ...
0
votes
1answer
75 views

Magento 2.2.5: How to get something (students,teachers,products) by id

i'm doing update "students" on magento 2.2.5 and i'm stuck in how to get student data by id i saw they use repository to load by id, but when i come in this file C:\xampp\htdocs\magento\vendor\...
-2
votes
2answers
77 views

Class Factory doesn't exists

I am after this course https://www.mage-world.com/blog/grid-and-form-in-magento-2-admin-panel-part-1.html but my magento version is 2.2.5 so its a little bit different. i create NewsFactory auto by ...
5
votes
2answers
2k views

Difference between Session and SessionFactory in Magento 2

While working i found there are two methods of getting customer data using two session classes 1.) \Magento\Customer\Model\SessionFactory $customerSession 2.) \Magento\Customer\Model\Session $...
1
vote
2answers
613 views

Magento 2.1 - Memory Leak in my getCollection function

So I'm creating a really simple module to read from a custom table. Nothing fancy, table has two rows: +-----------+--------------+ + status_id + status_label + +-----------+--------------+ + 1 ...
1
vote
1answer
353 views

MEQP Gives a Warning : Data load getFirstItem() method detected in loop

I want to get the First item from the collection so I have used following code. But it gives some warning on the usage of getFirstItem() protected $_sampleFactory; public function __construct( \...
1
vote
1answer
1k views

Why Magento 2.2.3 returns empty collection?

I don't have any prior experience in this. I want to fetch the products from Database. I am trying from past few hours.But not working. I am using following code. protected $...
1
vote
2answers
942 views

How to create mock for factory classes?

In unit testing I want to create mock object for factory classes e.g. $this->getMockBuilder(\Magento\Framework\Controller\Result\RawFactory::class)->disableOriginalConstructor()->getMock(); but I ...
0
votes
2answers
25 views

How do I add products to the page.phtml?

I have an array with the product ID in my block, how do I get the product list on the page like this:
5
votes
1answer
6k views

difference between collection and collectionFactory

I need the list of all the admin users. And, both the below set of codes, provide the same result. Code 1 public function __construct( \Magento\User\Model\ResourceModel\User\Collection $...
3
votes
1answer
681 views

Magento 2 - Empty arguments in adapter __construct()

I am creating an adapter for a third party module by following the Magento_Braintree structure. In the Braintree module, they have a factory BraintreeAdapterFactory that creates an adapter ...
0
votes
1answer
2k views

In Repository, how to replace deprecated load() function?

I know we should use service contracts(Repository) over $model->load(), but in Repository itself, how to replace the deprecated load() function? For example, in Magento\Cms\Model\PageRepository, ...
4
votes
3answers
13k views

Get Region Id given Region Code and country id

I need to get the region_id given the region_code and the country_id, but I cannot manage to achieve it. I've tried using the countryFactory and the regionFactory, but i do not understand what i'm ...
1
vote
2answers
783 views

How does getInvoiceCollection() work with orderCollectionFactory in Magento 2?

The following code works but how can we identify what methods can be called on the order collection result, such as getInvoiceCollection? $collection = $this->orderCollectionFactory->create() ...
0
votes
1answer
692 views

Factory create model is not empty Magento 2

I use a custom model with it's own Factory. In my helper in the construct public function __construct( \Company\Module\Model\ResourceModel\Communication\CollectionFactory $communicationFactory ){ $...
1
vote
1answer
218 views

Fetch a specific model in magento 2

I have 2 ways to fetch a model object: $orderId = 18; /** @var \Magento\Sales\Model\Order $firstOrder */ $firstOrder = $this->_orderFactory->create()->load( $orderId); /** @var \Magento\...
1
vote
1answer
42 views

In Magento 2, what classes should be instantiated with a factory and what not?

Magento 2 has a big 'depend on abstractions, not concretions' philosophy. One example of this is the use of Factories. Automatically generated factories. But I'm wondering: from a software ...
3
votes
1answer
1k views

Magento2: What is the best practice to get collection

I know it is not a good practice to use objectManager to get collection. However, I don't know what is the advantage of using ModelFactory ($this->mymodelFactory->create()->getCollection()) ...
4
votes
1answer
6k views

Magento2: Difference between Factory and Repository

What is the difference between these two objects in magento2? (Factory and Repository) I have seen that they have almost the same methods. What proper use does one have?
1
vote
1answer
6k views

Model class not found in .../magento/framework/ObjectManager/Factory/AbstractFactory.php:93

When I try to load collection via getCollection method in my custom Block, I get this error in apache2 log. The complete error is: [Mon May 08 09:53:53.485661 2017] [proxy_fcgi:error] [pid 1996:...
2
votes
1answer
169 views

How to override UpgradeData.php with Factory?

I need to override the UpgradeData Class from Catalog\Setup\UpgradeData But the constructor has factories in his arguments like CategorySetupFactory When I run setup:di:compile I got an error that ...
1
vote
1answer
36 views

Magento 2 - Prohibits depends directly the ObjectManager, but all object can use factory object

As Magento prohibits depending on and directly using the ObjectManager, whether all object can use factory object to instantiate?
0
votes
0answers
61 views

Creating a category programmatically using category factory, category data gets created in DB, but category is not visible in admin

Pretty much self-explanatory. I create a category model using category factory and then use the ->save method to save the category. I can see some data created, along with catalog_category_entity, ...
1
vote
0answers
219 views

Magento2 performance killer -factory pattern

Magento using factory pattern injecting non-injectable objects such as products,categories.so that it will create new object every time when you use create method on factory object. Is there any other ...
6
votes
2answers
471 views

Why auto generated Factories do not extends or implements any classes (interfaces)

This is more abstract question then specific problem. Why auto-generated factories in magento do not implements any common interface? And what bothers me maybe more. Why auto-generated factories ...
0
votes
1answer
68 views

Object instantiations in compiled mode

I was poking around for academic reasons on how the objects are instantiated. I figured out most of the things and the differences between factories (dev, compiled, production) but something scared me....
97
votes
6answers
36k views

When Should We Use a Repository and Factory in Magento 2?

I have gone through a couple of tutorials in Magento 2, and this confuses me a little. I can see there are basically two ways by which we can read/write business entities: Retrieve Data Using A ...
2
votes
1answer
293 views

Registering a shared service with a custom factory

I'm wondering how (if I can) register a shared service with a custom factory. Consider the following code: class MyApiFactory { private $config; public function ___construct(Config $config) ...
2
votes
0answers
429 views

How to load facotry model load best practice?

class Product extends \Magento\Framework\View\Element\Template { protected $_productloader; public function __construct( \Magento\Framework\View\Element\Template\Context $context, ...
1
vote
1answer
213 views

How to have a reference to an extra factory

I'm trying to have a reference to a model factory to load an object on desire. I just added a parameter to the constructor, but it generates an error: (The parent constructor doesn't have the ...
2
votes
2answers
4k views

Magento 2 : override resource model that is called by factory method

I am trying to override \Magento\Sales\Model\ResourceModel\Report\Order\Createdat by using <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:...
6
votes
3answers
2k views

"Invalid Factory for nonexistent class" (setup:di:compile error)

I get the following error when running bin/magento setup:di:compile: Errors during configuration scanning: IntegerNet\SolrSuggest\Implementor\Factory\AppFactory Invalid ...
2
votes
1answer
8k views

Magento 2 How can i create model factory for custom module?

Hi i am trying to collect some data from the database but the create method of the injected factory fails and no errors are showed. Here are my classes. magento\app\code\Cosmo\Basemodule\Model\...
0
votes
1answer
1k views

Magento 2 . Model factory create fails

These are my php files. magento\app\code\Cosmo\Basemodule\Model\DatiInterface.php <?php namespace Cosmo\Basemodule\Model; interface DatiInterface { } magento\app\code\Cosmo\Basemodule\Model\Dati....
1
vote
1answer
260 views

Factory methods, should I manually create them?

So when I develop extensions I usually do this outside of Magento and just pull in the packages I need. Once I have developed the extension I integrate it at the end. Because of this I manually ...
3
votes
2answers
2k views

Magento 2: Invalid customer attribute in collection filtering

I'm getting a strange error after filtering through customerFactory. Exception #0 (Magento\Framework\Exception\LocalizedException): Invalid attribute name: is_active. private function ...
2
votes
2answers
4k views

Magento 2: How to override Magento\Catalog\Block\Product\View constructor to inject \Magento\Eav\Model\Entity\Attribute\SetFactory

I am attempting to override the __construct method in the Magento\Catalog\block\Product\View block so that I can access the Magento\Eav\Model\Entity\Attribute\SetFactory to get the AttributeSetName of ...
5
votes
1answer
921 views

magento2 what exactly purpose of Factory class?

From this documentation Factories are special objects that have only one purpose: to create an instance of one non-injectable class or interface non-injectable object are Objects that cannot be ...
1
vote
1answer
91 views

setup:upgrade can't find Factory

my Magento 2 runs in developer mode and I'm trying to install my first selfwritten extension by CLI. The install itself works nice, but when running the setup:upgrade command, it can't find the ...
9
votes
1answer
621 views

What are the ObjectManager Factories differences?

I see that Magento 2 has 3 factory classes (4 if you count the abstract one). \Magento\Framework\ObjectManager\Factory\Dynamic\Developer \Magento\Framework\ObjectManager\Factory\Dynamic\Production \...
0
votes
1answer
646 views

How to create a front controller that uses an empty layout

I want my front controller to serve an xml type and for that reason i need that controller to use a layout that doesn't include any html tags. On my controller class i have the following code: <?...
0
votes
1answer
45 views

PageFactory Error with Hello World, Interceptor.php

Working on the Hello World example here: alanstorm.com/magento_2_mvvm_mvc I'm on the "Passing off to the View" step but I get an error with the World.php file when I refresh the page as instructed: ...
0
votes
1answer
54 views

Module Wide Factory Object

In Magento 2, would it be wrong to create a Factory model that allows me to get a factory for any of the models in my module? The reason I ask is that injecting model factories into my blocks/...
1
vote
1answer
895 views

Extending a block in Magento 2

I'm trying to extend Magento\Catalog\Block\Product\View to add some functionalities. I have let PHPStorm generate the constructor method for me and that worked fine. public function __construct( ...
9
votes
3answers
3k views

Generate factory or proxy in unit tests: "ReflectionException: Class ...Factory does not exist"

As far as I understand, Factory and Proxy classes are generated on the fly by the autoloader if they do not exist yet in var/generation (see: What Triggers the Generation of a Factory in Magento 2) ...
15
votes
3answers
3k views

Magento 2: what are the benefits of using service contracts?

So as some of you may know, Magento 2 recommended way to deal with models/collections for CRUD actions is to use service contracts. But still according to the Magento SE Q&A it seems like most ...
1
vote
1answer
274 views

Magento2 factory object inject parameters

I have created a Model with a Resource model to it and a collection. In MyModel I have defined the property public $myProperty; However, when I try to do $myModel = $this->MyModuleFactory->...
5
votes
1answer
3k views

How can data parameters be passed to a factory create( ) in Magento 2?

I have this snippet: // In __constructor arguments: \Magento\Framework\FlagFactory $flagFactory ... // This is not called in __constructor: $flag = $this->flagFactory->create(['flag_code' => ...
3
votes
1answer
3k views

Difference between Factory vs ObjectManager while instance object

I have some confusion while use Factory pattern vs new instance object. What difference between them? $object = objectManager->create() vs objectManager->create('ObjectFactory') what benefits ...