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 ...
25
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 ...
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 = ...
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 ...
8
votes
Accepted
Magento 2: What is a Resource Model and how can I use it?
You can get product,category and order collection by Proper way,
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Catalog\Model\ResourceModel\...
7
votes
Accepted
Error class AbstractDb not found
Change Magento\Framework\Model\Resource\Db\AbstractDb
to Magento\Framework\Model\ResourceModel\Db\AbstractDb
7
votes
How to implement service contract for a custom module in Magento 2?
complete files of using service contracts
Custom/Module/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::...
6
votes
Accepted
Custom Table Data not saving in Magento 2
Replace below code:
$this->contactUS->setFullname($full_name);
$this->contactUS->setCompany($company);
$this->contactUS->setAddress($address);
...
5
votes
Accepted
Resource Model vs Repository in Magento 2
Resource models are the way models were handled in Magento 1 and are still available in Magento 2. Repositories are the way we are supposed to handle model storage and retrieval in Magento 2, but ...
5
votes
Magento 2 Get last and first item from model collections
This is Tested Solution feel free to implement
$firstItem = $collections->getFirstItem();
$lastItem = $collections->getLastItem();
echo 'This First Record in Collection <br> ';
print_r(...
5
votes
Custom Table Data not saving in Magento 2
try it like this:
<?php
namespace Learning\ContactUS\Controller\Index;
class Save extends \Magento\Framework\App\Action\Action
{
/**
* @var \Magento\Framework\View\Result\PageFactory
...
5
votes
magento 2 : model in custom module
You need to add another resource modal connection (CRUD)
Step : 1 Create Model
app/code/NameSpace/ModuleName/Model/Post.php
Step : 2 Create Resource Model
app/code/NameSpace/ModuleName/Model/...
4
votes
Serializable Fields in Magento Collections
Or more simply, in your resource model collection:-
class Collection
extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
protected function _afterLoad()
{
...
4
votes
Magento 2.2 error: Unable to unserialize value
The problem is in /vendor/magento/framework/Serialize/Serializer/Json.php there is a function unserialize($string) which gives you a syntax error if the string is already serialized.
There is a ...
3
votes
Magento 2 Get last and first item from model collections
If you don't want to use a for-each loop then you should use the below code to get First and Last Item.
For First Item, you can use an array of 0. e.g $collection[0]. This will give you the first ...
3
votes
Truncate table using resource model in Magento 2
Create a function in your ResourceModel
app\code\Module\Namespace\Model\ResourceModel
Inventory.php
<?php
namespace Module\Namespace\Model\ResourceModel;
class Inventory extends \Magento\...
3
votes
Accepted
How to use resource model delete?
Here is what worked for me:
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
$myTable = $resource->getTableName('mytable'...
3
votes
Accepted
Correct way to save entries to database
Make your class look like this:
class ReservedItemRepository implements ReservedItemRepositoryInterface{
protected $collectionFactory;
protected $reservedItemFactory;
protected $...
3
votes
Accepted
How to stop saving resource model and throw error messages inside _beforeSave function?
Just throw exception.
protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
{
if(!$object->getData('identifier')):
throw new \Magento\Framework\...
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
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
Grid not displaying in admin - custom Resource Model
I have Analysed the code you have shared there is no typo error.
The Reason for Admin Grid Page not showing/blank is because magento is not able to read your adminhtml layout file. As per your code ...
3
votes
Accepted
Magento 2 Save and Load functions
So, first of all, you always should use Repository to load and save objects.
If there is a reason not to do this, everything is in phpDoc of load and save methods:
/**
* Save object data
*
* @...
2
votes
Accepted
Get Categories of Products on Special Offer
So you already have the products inside the $collection object.
You can do this.
$categoryIds = array();
foreach ($collection as $product) {
$categoryIds = array_merge($categoryIds, $product->...
2
votes
Get Categories of Products on Special Offer
As you said you got product collection let assume product collection is $collection.
$cat = array();
foreach ($collection as $col) {
foreach ($col->getCategoryIds() as $catId) {
...
2
votes
Accepted
Magento2 - Creating Purchase Order Grid based on Sales Order Grid
The problem is that you have the di.xml with your collection handle in the etc/adminhtml directory while Magento puts these declarations in the base area, /etc/di.xml. This is resulting in the handle ...
2
votes
Magento 2: Delete data in custom module
If you want avoid do a foreach, you can use a collection walk.
$modelcollection->walk('delete');
2
votes
Accepted
Magento 2: Delete data in custom module
Use \Magento\Ui\Component\MassAction\Filter class for massDelete
protected $zipcodeFactory;
public function __construct(
\Magento\Ui\Component\MassAction\Filter $filter,
\Vendor\Module\Model\...
2
votes
Accepted
Magento2 Override Resource Model does not Work
It appears to me that the only problem with your code is capitalization.
The class you are writing preference for is actually: ...\Backend\Tierprice not ...\Backend\TierPrice
(note the 'P')
I have ...
2
votes
Magento 2: What is a Resource Model and how can I use it?
Note: As per ECGM2 coding standard you should not use object manager direct in template files
By ObjectManager
//get product collection
$objectManager = \Magento\Framework\App\ObjectManager::...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
resource-model × 208model × 83
magento2 × 81
collection × 37
database × 27
magento-1.7 × 15
overrides × 14
magento-1.9 × 12
magento-2.1 × 12
module × 12
magento-1 × 8
magento2.3 × 7
controllers × 7
factory × 7
php × 6
attributes × 6
grid × 6
sql × 6
eav × 6
repository × 6
crud × 6
magento-1.8 × 5
magento2.2 × 5
uicomponent × 5
configuration × 5