Skip to main content
Share Your Experience: Take the 2024 Developer Survey
105 votes
Accepted

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

If there is a repository and it does what you need well, always prefer the repository. Repositories are part of the Service Contracts (they are implementations of interfaces in Api), this means they ...
Fabian Schmengler's user avatar
29 votes

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

Good question. Even if both Repositories and Factories let us access an Entity I think we should focus on their responsibility. From Magento documentation: "Factories are service classes that ...
Alessandro Ronchi's user avatar
17 votes
Accepted

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

Benifits of using service contracts, (as per Magento 2 understanding) Service contracts have a number of important functions for Magento 2, such as: Upgrading modules becomes easy. Simplify ...
Rakesh Jesadiya's user avatar
11 votes
Accepted

Get Region Id given Region Code and country id

This has to work $region = $this->_regionFactory->create(); $regionId = $region->loadByCode($code, $country_id)->getId();
Nikolas's user avatar
  • 2,291
10 votes
Accepted

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

Use code like $this->flagFactory->create(['data' => ['flag_code' => 'something']]);
KAndy's user avatar
  • 20.9k
10 votes
Accepted

Magento2: Difference between Factory and Repository

Different between factory and repository Factory definition: Factories are service classes that instantiate non-injectable classes, that is, models that represent a database entity. They create a ...
Amit Bera's user avatar
  • 77.5k
9 votes
Accepted

What is the use of pagefactory in Magento 2?

Pagefactory is used to initialize the layout. Its working in magento2 same like as in magento1 was doing with below code $this->_view->loadLayout(); $this->_view->renderLayout();
Ansar Husain's user avatar
  • 3,429
7 votes

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

I would say the way forward it's to start using repositories as they allow code separation between data reading/writing and business logic. There is a very detailed article written by Alan Storm ...
Marina Gociu's user avatar
  • 1,261
7 votes
Accepted

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

The easiest way to deal with that is to run compilation before running tests: bin/magento setup:di:compile The other way is to explicitly define methods for the factory mock eg. instead of doing ...
Wojtek Naruniec's user avatar
7 votes

What is Factory in Magento2 and Why use?

In Magento 2, the Magento 2 Factory Object will be used to instantiate an object. The Factory class name is the name of Model class and append with the Factory word. It also dependent with ...
Rohan Hapani's user avatar
  • 17.4k
6 votes

magento2 what exactly purpose of Factory class?

In general you use factories to instantiate non-injectable objects, like the cms page object. Some objects require user input to instantiate so you cannot inject them via the constructor. \Magento\...
Claudiu Creanga's user avatar
5 votes
Accepted

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

Replace class Createdat extends Magento\Sales\Model\ResourceModel\Report\Order\Createdat { With class Createdat extends \Magento\Sales\Model\ResourceModel\Report\Order\Createdat { You have forgot ...
Rakesh Jesadiya's user avatar
5 votes

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

Also you may use something like this private function getMockupFactory($instanceName) { /** Magento\Framework\TestFramework\Unit\Helper\ObjectManager */ $objectManager = $this->...
Eduard Melnyk's user avatar
5 votes

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

The problem originates from PHPUnit mocking library, as it cannot autoload the needed class. If you take a look into Magento dev repo, it setups Autoloader catcher, that generates a class when it is ...
Ivan Chepurnyi's user avatar
5 votes

Magento 2: Invalid customer attribute in collection filtering

is_active is not an attribute which available to add to select, or filter. You are still able to have a workaround for this problem by: private function getCustomerDetails() { $collection = $this-...
Tuyen Nguyen's user avatar
  • 1,312
5 votes

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

I think the greatest benefit is that modules can determine which functionality can be used by other modules. In Magento 1 you had helpers that where often kind of misused for this purpose (but that's ...
Giel Berkers's user avatar
  • 12.3k
5 votes

Get Region Id given Region Code and country id

Try below code: <?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $region = $objectManager->create('Magento\Directory\Model\Region') ->...
Abhishek Panchal's user avatar
5 votes
Accepted

Difference between Session and SessionFactory in Magento 2

There are two ways of creating object: 1) Model \Magento\Customer\Model\Session 2) Factory \Magento\Customer\Model\SessionFactory If you use Model class, it will always create shared objects it ...
Prince Patel's user avatar
  • 22.7k
5 votes

What is the use of pagefactory in Magento 2?

https://www.mageplaza.com/magento-2-module-development/factory-object-magento-2.html In Magento 2 all controller actions must return something, opposed to M1, where a controller action would just ...
Shubham Singh's user avatar
5 votes
Accepted

Magento2 Alternate of Method getCollection() which is deprecated

Shoaib Munir ,you are wrong as $this->_myCollectionFactory instance of a collection faCTORY CLASS . If you have using Collection factory class then getting collection data, you don't need to call ...
Amit Bera's user avatar
  • 77.5k
5 votes

Magento 2: When should we use a ProductRepository, ProductFactory, Product model?

First, check below links for understanding Repository pattern https://inchoo.net/magento-2/repository-pattern-in-magento-2/ https://devdocs.magento.com/guides/v2.3/extension-dev-guide/service-...
Amit Bera's user avatar
  • 77.5k
5 votes
Accepted

Magento 2 - How can I write query in Magento

You can try this.. $this->ayakilOrganizationModel ->getCollection() ->getSelect() ->join( ['ot' => 'ayakil_organization_type'], 'main_table.type_id = ot....
Kishan Savaliya's user avatar
4 votes
Accepted

What are the ObjectManager Factories differences?

And condition is here: \Magento\Framework\App\EnvironmentFactory::createEnvironment It does check if file with compiled content is exist for current area: "/var/di/global.ser", "/var/di/frontend.ser" ...
user40988's user avatar
  • 324
4 votes
Accepted

Magento2 factory object inject parameters

you need to add 'myProperty' to the constructor aslo public function __construct( ... $myProperty = null; //because this is not a class instance make it default to null otherwise the object ...
Marius's user avatar
  • 198k
4 votes
Accepted

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

Adding to Marius answer when you look at those auto-generated factories code it is always only call of \Magento\Framework\ObjectManagerInterface::create(). There is no more to it. Making it extend ...
Zefiryn's user avatar
  • 5,898
4 votes
Accepted

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

You can use ResourceModel's load() instead, which extends Magento\Framework\EntityManager\EntityManager's load() method. We could see a example in: Magento\Cms\Model\BlockRepository public function ...
Yogesh Agarwal's user avatar
4 votes
Accepted

difference between collection and collectionFactory

I understand this follows the Factory pattern https://en.wikipedia.org/wiki/Factory_(object-oriented_programming)#PHP So, create() method returns the requested object, in this case: \Magento\User\...
Raul Sanchez's user avatar
  • 3,115
4 votes

Magento2 Alternate of Method getCollection() which is deprecated

Use a CollectionFactoryas follows. For example, for Magento\Catalog\Model\Product : public function __construct( \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $...
Adarsh M's user avatar
  • 594
4 votes
Accepted

What PHP class executes the SQL to select models and collections from the database?

In most circumstances, entity models inherit from AbstractModel, resource models, which serves as an adapter between an entity model and ORM, inherit from AbstractDb, and collections inherit from ...
nburr's user avatar
  • 191
3 votes
Accepted

Object instantiations in compiled mode

The details of those arguments can be found in the Magento\Setup\Module\Di\Compiler\ArgumentsResolver : /** * Shared instance argument pattern used for configuration * * @var array */ private $...
Raphael at Digital Pianism's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible