Skip to main content
119 votes
Accepted

How to implement service contract for a custom module in Magento 2?

I would like to give a bit more detail in addition to the excellent answer of @ryanF. I would like to sum up the reasons to add a repository for custom entities, give examples how to do so, and also ...
Vinai's user avatar
  • 14k
46 votes
Accepted

How to Get country name from country code in Magento 2?

Create Block file, public function __construct( \Magento\Directory\Model\CountryFactory $countryFactory ) { $this->_countryFactory = $countryFactory; } ...
Rakesh Jesadiya's user avatar
43 votes

How to implement service contract for a custom module in Magento 2?

@Raphael at Digital Pianism: Please refer to the following sample module structure: app/ ├ code/ | ├ Namespace/ | | ├ Custom/ | | | ├ Api/ | | | | ├ ...
ryanF's user avatar
  • 2,279
38 votes
Accepted

Deprecated save and load methods in Abstract Model

You should use Module Service Contract. For example for product you should use ProductRepositoryInterface <?php /** * * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for ...
KAndy's user avatar
  • 20.9k
25 votes

Deprecated save and load methods in Abstract Model

From what I understood, what is going to happen is Magento is going to switch to hydrators with extract() and hydrate() methods. This link used to work but it seems like Magento team rolled it back: ...
Raphael at Digital Pianism's user avatar
24 votes

How to get Category Collection per Store in Magento 2?

Use directly objectmanager is not best/Recommended way to do in magento use Block with consturct and fetch method in your phtml file. $categoryFactory = $objectManager->create('Magento\Catalog\...
Rakesh Jesadiya's user avatar
24 votes
Accepted

Best way to update product's attribute value

I have used method updateAttributes of class \Magento\Catalog\Model\ResourceModel\Product\Action. Method can update multiple products and attribute values, but if you want to update just one product ...
Bartosz Kubicki's user avatar
24 votes
Accepted

Truncate table using resource model in Magento 2

You can truncate table using DB Adapter and table name. /** @var \Magento\Framework\DB\Adapter\AdapterInterface $connection */ $connection = $model->getResource()->getConnection(); $tableName = ...
Max's user avatar
  • 4,054
22 votes
Accepted

What are the differences between a Model and a Resource model?

Models : Models are where your main business logic should be handled and is a single instance of an object. The model will use the resource model to talk to the database and get/set data for it on ...
Suresh Chikani's user avatar
20 votes
Accepted

Best Way to Load a Custom Model in Magento 2

Best practice: via the service contract The best practice is always to use the service contract whenever it's possible. You can find the list of reasons here: Magento 2: what are the benefits of ...
Raphael at Digital Pianism's user avatar
18 votes
Accepted

Insert multiple rows without calling model save inside loop

Use insertMultiple function of class Magento\Framework\App\ResourceConnection protected $connection; protected $resource; public function __construct( ... \Magento\Framework\App\...
Prince Patel's user avatar
  • 22.8k
17 votes
Accepted

Magento2 How to get region name by id?

You still can do basically the same as load(), save(), and delete() actions still exists in abstract model classes. They are however marked as deprecated as magento moves CRUD methods to separate ...
Zefiryn's user avatar
  • 5,908
15 votes

How should I save new or updated entities of models?

Let us first see, what happens if you use the save() method directly on a product model like /** * @var Magento\Catalog\Model\Product $product */ $product->save(); The model class itself is ...
Marjan's user avatar
  • 1,149
15 votes

Disabling a UI component field upon condition in Magento 2

I tried Konstantin's solution but for some reason, it didn't work for me. I managed to make it work by using the following syntax: <field name="name"> <argument> ... </...
Alessandro Ronchi's user avatar
14 votes

What and why is the proper way to load a model

Well, the first step you should check for the model in question is: Is there a Repository Service Contract? If so, use that, because Service Contracts are bound to semantic versioning and will ...
Giel Berkers's user avatar
  • 12.3k
13 votes
Accepted

How to save data to custom table using custom model in magento2

The most simple solution for your own model is to get factory using di and then create 1 copy (or load it), fill it in with data and save. Here is the example with the model called Module: class Rent ...
Mageworx's user avatar
  • 3,566
13 votes
Accepted

Disabling a UI component field upon condition in Magento 2

First set: <item name="disabled" xsi:type="string">${ $.provider }:data.do_we_hide_it</item> Suppose Vendor\Extension\Model\Notification\DataProvider is your data provider for UI: <...
Konstantin Gerasimov's user avatar
12 votes

Get Option Value by Id of Attribute in Magento

Simply put - use the getAttributeText method. $product->getAttributeText('brand')
PromInc's user avatar
  • 460
12 votes
Accepted

Magento 2: How to Insert Multiple Records to custom table?

We forgot to unset data model after saving. $productModel->save(); $productModel->unsetData(); [EDIT] Using model Factory can solve our issue. For example, inject Company\Module\Model\...
Khoa TruongDinh's user avatar
12 votes
Accepted

Product grid says "The resource isn't set" when adding __construct to model class

I wasn't in accord with the parent class' constructor. The surprising thing is magento setup:di:compile yielded no error. Anyway, here's the solution. public function __construct( \Magento\...
Joseph Hovik's user avatar
11 votes

Magento 2: how to get Model in Block

Override constructor in your block class and add your class factory as dependency. public function __construct( \Magento\Backend\Block\Template\Context $context, \Vendor\Exple\Model\...
KAndy's user avatar
  • 20.9k
11 votes

How to get Category Collection per Store in Magento 2?

Create Block and add below code to your block. namespace <vendor>\<module>\Block; class FeaturedCategories extends \Magento\Framework\View\Element\Template{ protected $...
Savoo's user avatar
  • 487
11 votes

Disabling a UI component field upon condition in Magento 2

You can override getMeta() function in your DataProvider class public function getMeta() { $meta = parent::getMeta(); /** @var Magento\Framework\App\RequestInterface $this->...
Smita Kagwade's user avatar
11 votes
Accepted

Get quote item collection by using quote item id in Magento 2

private $quoteItemFactory; private $itemResourceModel; public function __construct( ..... \Magento\Quote\Model\Quote\ItemFactory $quoteItemFactory, \Magento\Quote\Model\ResourceModel\Quote\Item $...
Marius's user avatar
  • 198k
11 votes

Magento 2 How to get only one field data in collection

Magento has an inbuilt function on Magento\Framework\Data\Collection which provide a specific field value from the collection. You have to use getColumnValues($colName) for getting the specific field ...
Amit Bera's user avatar
  • 77.6k
10 votes

How to Get country name from country code in Magento 2?

We can use Magento\Directory\Api\CountryInformationAcquirerInterface to get the country info: /** @var \Magento\Directory\Api\CountryInformationAcquirerInterface $country */ /** @var \Magento\...
Khoa TruongDinh's user avatar
10 votes
Accepted

Proper way to call a model constant in Magento 1

echo Company_Module_Model_Somemodel::SOME_CONST; This one is way to go because Magento uses an autoloader and your class will be autoloaded by name For further detail please check the reference link ...
Muhammad Hasham's user avatar
10 votes
Accepted

What is difference between these models and design patterns used in Magento?

Repository is the Model that is supposed to be used to expose the data for a given entity. In practice, they may optimise how the data is loaded. For instance, product repository uses a caching ...
Herve Tribouilloy's user avatar
9 votes

Magento 2 Get product collection in a custom template block

Instead of using the Object manager u can try: public function __construct( \Magento\Backend\Block\Template\Context $context, \Magento\Catalog\Model\ResourceModel\Product\...
Sushivam's user avatar
  • 2,639
9 votes
Accepted

Override model function Magento 2

Depending on what kind of data you want to add, you might want to use an observer instead of rewriting the model: etc/events.xml <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/...
Raphael at Digital Pianism's user avatar

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