Questions tagged [model]

Models contain most of the business logic in Magento modules

Filter by
Sorted by
Tagged with
58
votes
4answers
17k views

Deprecated save and load methods in Abstract Model

I see that in the develop branch of the Magento 2 repo the methods load and save from Magento\Framework\Model\AbstractModel class are deprecated. But there are a gazillion classes in the core that ...
48
votes
3answers
17k views

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

As seen in this post : Deprecated save and load methods in Abstract Model the save and load methods are deprecated in the Magento 2 develop branch. Thus, the good practice is now to be implement ...
29
votes
1answer
10k views

Conflicts in where clause with ambiguous column names

A bit of context for this. I want to extend the sales order export function (via the grid) to have more columns. I have created a module which adds a new grid for exporting and also a new collection ...
28
votes
2answers
322 views

What parts of the model layer can be bypassed in the interest of performance optimization

I'm currently seeing that for a database table with a very simple schema (about 5 fields), it's inserting new records at a rate of just under ~50 inserts / second, in my local development environment (...
24
votes
3answers
4k views

Is there ever a reason to prefer $model->load() over service contracts?

I understand that the preferred way to work in between modules in Magento 2 is using the service contracts. So if I want to load a product, I use the product repository: $product = $...
20
votes
3answers
3k views

What is IdentityInterface in Magento2

I see that a lot of the models in Magento 2 implement Magento\Framework\DataObject\IdentityInterface. This interface has a single method called getIdentities The implementations of the method usually ...
16
votes
2answers
13k views

Best Way to Load a Custom Model in Magento 2

Because it was hard for me to find the right way, below you could find the best practice I made mine. Enjoy, correct my English if needed and say me I'm wrong if I am. :) Edit : ... and I found out I ...
15
votes
4answers
23k views

Magento - custom (non eav) model, load by multiple fields

I have a custom model and resource model. I want to load a single instance of the model using more than 1 field. The model has the following fields: id tag_name custom_name group_name I want to ...
15
votes
2answers
6k views

Why magento use resource and mysql4 model collection?

What is the purpose of using these two models collection. Resource Mysql4 why can't we use resource model for all collections. Why we are using MySql4 collection for simple module Some one explain....
15
votes
3answers
2k views

Efficient Collection Calling, Filtering, and Loading

Right now im reusing a lot of collections that are nested within foreach loops. Is it possible to move these things up a few levels? Currently Im forced to reload collections that have 51k + entities ...
15
votes
1answer
2k views

SQL injections vulnerabilities when using SQL models of the Zend Framework

When joining tables, I use SQL models of the Zend Framework. As example I modified my actual code, but I think you will get the point: $this->getSelect()->join( array('...
15
votes
1answer
3k views

What is the best practice way in Magento 2 for creating Many To Many relationships?

I've looked around the core and seen a few examples of many to many relationships between models, but I can't see a definitive answer on this. As an example, say we create a new model and we want to ...
14
votes
3answers
8k views

How to instantiate a model in magento2?

Is there a way of doing something simple as in magento2? call function like this in magento 2, $model = Mage::getModel('cms/page');
14
votes
4answers
2k views

Is it good to instantiate a getModel class on phtml templates?

This is a question regarding a good programming practices in Magento. I need to show (in the category product list) the product with its related products in thumbnails. So I edited mypackage/mytheme/...
14
votes
1answer
2k views

Using Magento's ORM to Insert a Specific ID Field

Is there a way to use Magento's simple ORM (Mage_Core_Model_Abstract and Mage_Core_Model_Resource_Abstract) to insert model rows with a specific primary key? For example, if I ran the following ...
12
votes
3answers
47k views

Get Option Value by Id of Attribute in Magento

How can Magento find an attribute value by a given attribute label or a given attribute id?
11
votes
1answer
2k views

Full Class Names in Magento's Factory Methods

In Magento 1, if I use the full Magento class name in a factory method, I'm able to instantiate an object //trying full class name instead of catalog/product $object = Mage::getModel('...
10
votes
6answers
19k views

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

i want to get country name from country code, i got the country code from the data order like this : $data = $order->getShippingAddress()->getData(); $countryCode = $data['country_id']; echo $...
10
votes
1answer
3k views

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

What are the differences between a Model and a Resource model? Also what is a model and a resource model?
10
votes
2answers
3k views

What is the difference between getOrigData(); and getData();? [duplicate]

What is the difference between $model->getOrigData(); and $model->getData();
10
votes
2answers
6k views

How should I save new or updated entities of models?

In Magento 2 we have repositories classes. Classic method save() used heavily in Magento 1.9 are deprecated, if I am correct, from 2.04 or 2.05. I was using factories to create new object and after ...
9
votes
1answer
3k views

Tables with non auto increment primary key

I have set-up a table in Magento that has two fields, id and date. The date is simply the set to now but the id is actually a foreign key attached to the order id. My problem is that Magento does not ...
9
votes
2answers
4k views

How to Load Model data joining 2 tables?

Suppose I have a simple Model A that target table A now I have to add some extra info so I decide to create a Model B and save those extra info in a Table B with a foreign key pointing to Table A ...
9
votes
2answers
3k views

What and why is the proper way to load a model

I have quite a lot of experience with Magento but I realized that I don't understand which way of loading a model is the correct one and why. I've read everything that I could, about the topic but ...
8
votes
3answers
10k views

New column added to table is not saving

I have added a new column to the newsletter_subscriber table, 'artists_followed'. in my overridden Mage_Newsletter_Model_Subscriber model class, I have added the following function: public function ...
8
votes
3answers
44k views

Error on Mage::getModel('catalog/product')->load($sku, 'sku');

when trying to load a product, $foundProduct = Mage::getModel('catalog/product')->load($sku, 'sku'); we get the following error: Fatal error: Uncaught exception 'Exception' with message '...
8
votes
2answers
4k views

What is the difference between $model->getOrigData(); and $model->getData();?

There are two methods which retrieve model data. $model->getData(); $model->getOrigData(); Can anyone explain what is the difference between them ? Any help will be appreciated.
8
votes
2answers
26k views

How to create a custom grid filter

I have added a custom grid column using the field URL in a custom database: $this->addColumn('url', array( 'header' => Mage::helper('companymodule')->__('Has Website'), 'align' =...
8
votes
4answers
8k views

How prevent a model data save using _save_before event

I have create a model which has its own database table. For a customization I need to trigger save_before event of this model. If one field value is not matched, then the data should not be saved. ...
8
votes
2answers
604 views

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

I need to decrement a value with an atomic database operation, is it possible using Magento models? setNumber($number) works like number = $number, but I need it to be decremented in SQL query. Is ...
8
votes
1answer
261 views

For a cms/block entity, why are the subordinate data deleted from cms_page_store explicitly?

cms/block entities have a join table cms_block_store, the records in which link a given block to one or more stores. Prior to CE 1.6, the resource model Mage_Cms_Model_Mysql4_Block (link) does not ...
7
votes
4answers
34k views

How to get Category Collection per Store in Magento 2?

I want to get all categories names from a specific store. I'm trying: $categoryFactory = $objectManager->create('Magento\Catalog\Model\ResourceModel\Category\CollectionFactory'); $categories = $...
7
votes
1answer
44k views

Custom Sort Order For Collection

I need to sort a collection based on "sort_order" field instead of "id" as shown in the image. $collection=Mage::getResourceModel('banners/manage_collection'); $collection->getSelect()->order('...
7
votes
2answers
6k views

Disabling a UI component field upon condition in Magento 2

I have a UI component defining some fields. How do I disable the field upon a certain condition? In other words, use <item name="disabled" xsi:type="boolean">true</item> only when a ...
7
votes
1answer
7k views

Usage of getsingleton() and getModel()

Is there a set of guidelines that I can go by to decide when I should use mage::getModel() and mage::getSingleton() From what I can tell it seems as though getSingleton either gets an existing ...
7
votes
2answers
7k views

What is the difference between getData('name') and getName()

I am using magento 1.8 And I am facing problem when I use below methods $product->getData('name'); $product->getName(); Is they are same, I sure that they return same value[answer]. When I ...
7
votes
5answers
3k views

Magento customer save memory leak

I'm trying to edit customer data in bulk (a couple of thousand records) and Magento keeps running out of available memory to run the script. After some testing with memory_get_usage() the culprit ...
7
votes
1answer
7k views

Magento2 How to get region name by id?

Like in magento1 is is there any method to retrive region name by id. $regionModel = Mage::getModel('directory/region')->load('region_id'); $region = $regionModel->getName(); I checked ...
7
votes
1answer
4k views

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

I have created a new table in Magento2: my_custom_table with 4 fields: id: primary key, product_id: int(11), customer_id: int(11) status:int(11) I want to save multiple records at a time. Here is ...
7
votes
2answers
11k views

Get subcategories of parent category when FLAT categories enabled

I override a block class Mage_Catalog_Block_Navigation (link to signature) to create a custom menu which displays only subcategories of current category. Inside method renderCategoriesMenuHtml (link ...
7
votes
1answer
852 views

Magento 2: How to set VaultProvider?

I'm working on custom payment system in which I am using Magento2 default vault functionality. Everything is working fine when if I replace below code in file Magento\Vault\Model\Method\Vault into ...
7
votes
3answers
2k views

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

I need this because I don't want to dispatch $order->save() events. I want to do something like $order->_getResource()->saveAttribute($order, 'statusHistoryComment'); I will be glad to any ...
7
votes
1answer
4k views

Struggling with Observer rewrite (not conflicts that I can find)

I am trying to rewrite an observer in the extensions Xtento_GridActions. The observer is established this way: <core_block_abstract_prepare_layout_after> <observers> ...
7
votes
1answer
2k views

I want to add my custom entity for export Magento 2

I want to export my custom entity from the admin like products, customers, etc... I have already created the code for the import for my custom entity and it is working fine. But now I want to add the ...
7
votes
4answers
1k views

Get Categories of Products on Special Offer

How can I get the list of categories of all the products on special offers. It's a bit tricky. I'm able to get the list of products using: $collection ->addAttributeToFilter( ...
6
votes
4answers
40k views

Magento 2 Get product collection in a custom template block

I have defined a featured product template in a cms static page as below: {{block class="Magento\Catalog\Block\Product\ListProduct" template="Magento_Catalog::product/featured.phtml"}} Now I want to ...
6
votes
3answers
477 views

Magento cannot override Mage_Payment_Model_Method_Cc.php file

I am trying to override Payment/Model/Method/Cc.php file but it's not able rewrite. can you please any one check my script. config.xml: <?xml version="1.0"?> <config> <modules> ...
6
votes
1answer
6k views

What is the definition of a frontend model and backend model

I see methods like getBackend* and getFrontend, could some one be able to define what the a backend/ frontend model is and its responsibilities? Just really to give a definition in my head of what it ...
6
votes
2answers
285 views

Proper way to call a model constant in Magento 1

I have the class below: class Company_Module_Model_Somemodel extends Mage_Core_Model_Abstract { const SOME_CONST = 'constant_text'; } What is the proper and correct way to call the constant ...
6
votes
1answer
402 views

what is the need of extra layer(service contracts) in magento2?

With out service contracts: For example I can extend directly class AccountManagement model. I can override any function.At the same time if core developers add anything in class AccountManagement ...

1
2 3 4 5
15