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 ...
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 ...
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();
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 ...
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();
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 ...
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 ...
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-...
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')
->...
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->...
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 ...
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 ...
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 ...
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 ...
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 ...
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-...
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....
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 ...
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\...
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 ...
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 $...
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 ...
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 ...
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 ...
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 $...
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 ...
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->...
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', $...
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(...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
factory × 110magento2 × 87
collection × 19
dependency-injection × 10
model × 9
resource-model × 7
object-manager × 6
code-generation × 6
magento-2.1 × 5
magento2.3 × 5
blocks × 5
repository × 5
database × 4
layout × 4
customer × 4
controllers × 4
di × 4
module × 3
magento2.2 × 3
best-practice × 3
class × 3
unit-tests × 3
interface × 3
php × 2
orders × 2