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 ...
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/
| | | | ├ ...
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 ...
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 ...
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 = ...
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 ...
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 ...
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 '...
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.
...
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-...
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
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::...
7
votes
Accepted
Error class AbstractDb not found
Change Magento\Framework\Model\Resource\Db\AbstractDb
to Magento\Framework\Model\ResourceModel\Db\AbstractDb
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,
...
) ...
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 ...
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 ...
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
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, '...
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 ...
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 ...
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>
&...
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(), ...
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.
...
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
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 ...
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
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 = $...
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 ...
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 = ...
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');
...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
resource-model × 204model × 83
magento2 × 79
collection × 38
database × 27
magento-1.7 × 15
module × 13
overrides × 13
magento-1.9 × 12
magento-2.1 × 12
magento2.3 × 7
grid × 7
controllers × 7
factory × 7
attributes × 6
sql × 6
eav × 6
php × 5
magento-1.8 × 5
magento2.2 × 5
magento-1 × 5
configuration × 5
repository × 5
crud × 5
product × 4