115 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 ...
user avatar
  • 14.4k
41 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/ | | | | ├ ...
user avatar
  • 2,239
34 votes
Accepted

Model save using setData and addData not adding to database

This is what you have done here $data = array('appid'=>'new_appid','custid'=>1,'info'=>'info'); $model = Mage::getModel('interface/data')->setData($data); $model->save(); Here I am ...
user avatar
22 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 ...
user avatar
22 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 = ...
user avatar
  • 3,834
20 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 ...
user avatar
16 votes
Accepted

Decrement value instead of setting it like `number = number - 1`. Is it possible in Magento?

Yes, it's possible, using Zend_Db_Expr: $object->setNumber(new Zend_Db_Expr('number-1')); For reference: The method Mage_Core_Model_Resource_Abstract::_prepareDataForSave() contains the ...
user avatar
13 votes

How to set a store ID on Mage_Catalog_Model_Resource_Product_Collection?

What version of Magento is this? These are my results for Magento 1.9: Flat catalog enabled: Flat catalog is indexed: Some data set in a specific store view: Code used: <?php require_once '...
user avatar
  • 1,182
12 votes
Accepted

how to join three tables on every time when model load

techguy4web, In this case,you need make one table primary table and using join function[left join,Inner join] on Resource Model and Resource Model Collection class relation rest of two classes. ...
user avatar
  • 75.4k
10 votes
Accepted

How to set a store ID on Mage_Catalog_Model_Resource_Product_Collection?

So I consider these to be two bugs in Magento. First one is the fact that you can't set store ID on catalog/product collection. And the second is that you absolutely can't get resource model as non-...
user avatar
  • 11.5k
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\...
user avatar
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::...
user avatar
  • 1,321
7 votes
Accepted

Error class AbstractDb not found

Change Magento\Framework\Model\Resource\Db\AbstractDb to Magento\Framework\Model\ResourceModel\Db\AbstractDb
user avatar
6 votes
Accepted

Magento2 Get a list of all cms static blocks

Do a dependency injection of Magento\Cms\Model\BlockFactory class in your constructor public function __construct( ... \Magento\Cms\Model\BlockFactory $blockFactory, ... ) ...
user avatar
6 votes
Accepted

Magento 2.1.0 : How to get custom table data in phtml file

You can load the Data by following below steps. Create a Block file or if you already created a block for your template use below code in the block in your module. Add below code to block public ...
user avatar
6 votes
Accepted

It is possible to addStatusHistoryComment to order without call $order->save()?

If the order already exists in the system, you can mimic the core behaviour and call save() either on the order status history collection or the status history model itself. // just some random order ...
user avatar
  • 2,913
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); ...
user avatar
5 votes
Accepted

Loading just an attribute from a model and saving it

If you already have the customer model (at least partially loaded), you can use this line to save a single attribute: $customer->getResource()->saveAttribute($customer, '...
user avatar
5 votes
Accepted

addAttribute() vs addColumn()?

Let's take it step by step. addColumn adds a column to a table. addAttribute adds a new attribute for EAV entities. So a new record will be created in the eav_attribute table. The exception: For ...
user avatar
  • 195k
5 votes

Magento collection classes confusion

Mage::getResourceModel('sales/order_item_collection') = Mage_Sales_Model_Resource_Order_Item_Collection this is the resource for a collection of items in orders, normally the database table that this ...
user avatar
5 votes
Accepted

Fatal error: Call to a member function beginTransaction() on boolean in /var/www/html/app/code/core/Mage/Core/Model/Abstract.php on line 313

The problem comes from the declaration of the resources and the name used in the models. So instead of : <xmpieformbuilder> <class>Company_XMPieFormBuilder_Model</class> &...
user avatar
5 votes
Accepted

Create a custom resource model that fetches data from 3rd-party API (non-db)

The answer is Yes! It is possible. The resource models inside Magento are responsible for the CRUD operations of a module. So, based upon that, all you have to do is leverage the save(), delete(), ...
user avatar
  • 2,020
5 votes

How to use resource model delete?

If we take a look: vendor/magento/framework/DB/Adapter/AdapterInterface.php /** * Deletes table rows based on a WHERE clause. * * @param mixed $table The table to update. ...
user avatar
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/...
user avatar
  • 5,848
4 votes
Accepted

mage::getModel('module/model')->getData returns an empty array

I think the problem is that you do not have a definition for the table prefs/prefsformelements. If you look at how the init for the resource works it takes the following: /** * Standard resource ...
user avatar
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() { ...
user avatar
4 votes

Use of $installer v $this in install scripts

One thing I like about using $installer that I really want to add is that it makes it easy to replace it with something else or to run it outside of the class scope. 1. Replacing it: $installer = $...
user avatar
  • 7,342
4 votes
Accepted

Get Model Properties ( DB column )

It is possible to use the describeTable() method. So your resource model, you can create a function as the following one, this will return an array of fields for the main table: public function ...
user avatar
  • 6,845
4 votes
Accepted

Always update on duplicate when using save() in custom model

The method that I came up which seems to work fine is to add this method to my model: /** * @param $sku * @return $this|Mage_Core_Model_Abstract */ public function loadBySku($sku) { $matches = ...
user avatar
  • 2,957
4 votes

Product stock is not saved when creating a product programmatically

This is a working example wich i use: $product = Mage::getModel('catalog/product'); $product->setWebsiteIds(array(1)); $product->setTypeId('simple'); ...
user avatar
  • 131

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