Skip to main content
107 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
30 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
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

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.7k
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,479
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

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.5k
6 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,332
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

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

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
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
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.9k
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.7k
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.7k
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

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,928
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,100
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

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

Magento2: What is the best practice to get collection

Since you are foremost asking for best practice: The best practice is to create the collection using factories, getCollection() is a shortcut that I would see as a relict from Magento 1. It's not ...
Fabian Schmengler's user avatar
3 votes

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

I don't see any reasons for the factories of related classes to be related by themselves. A factory has only one scope. Create other instances. MagicClassFactory creates instances of MagicClass and ...
Marius's user avatar
  • 199k
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
3 votes

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

Reason for error is you can not return Object type of Fudu\HelloWorld\Model\StudentsFactory In controller execute method. Return type for controller should be : “Json” result (return json type of ...
Dhaval Solanki's user avatar
3 votes
Accepted

Magento 2.2.5: Add Student Instead of Update Student

$model->setData($data); this line reset your data. If there id field present $data variable then your code working fine. Otherwise you need to set id for update. Check following code: $id = $this->...
Sohel Rana's user avatar
  • 36.1k
3 votes

Magento2 Alternate of Method getCollection() which is deprecated

In Magento2 it is always recommend using Search Criteria to get collections For example: $searchCriteriaBuilder->addFilter('zip', $saveSearchZip['zip'], 'eq') ->addFilter('country', $...
Vishwas Bhatnagar's user avatar
3 votes
Accepted

factory class duplicate insert check in magento2

Modify builder method following way: public function builder($pid,$type,$id,$date) { $log = $this->_builderFactory->create(); $log = $log->load($pid, 'p_id'); if (!$log->getId(...
Sohel Rana's user avatar
  • 36.1k
3 votes
Accepted

PostFactory does not exist in generated Interceptor in magento 2?

The Vendor\Company\Model\PostFactory Class is injected at Vendor\Company\Controller\Index\Index (in the constructor) and from there the error occurs. If you have Vendor\Company\Model\Post Class then ...
Amit Bera's user avatar
  • 77.7k

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