Questions tagged [model]

Models contain most of the business logic in Magento modules

Filter by
Sorted by
Tagged with
46
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 ...
58
votes
4answers
16k 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 ...
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 ...
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. ...
10
votes
6answers
18k 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 $...
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');
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
25k 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' =...
6
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 ...
8
votes
2answers
580 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 ...
3
votes
3answers
4k views

Override model function Magento 2

I want to override one of the function in Magento core model which is located in vendor\magento\module-sales\Model\Order\Email\Sender\OrderSender.php <?php /** * Copyright © 2016 Magento. All ...
15
votes
2answers
12k 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 ...
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 ...
5
votes
3answers
26k views

Model save using setData and addData not adding to database

I have a model and I was trying to add a model to the database. When I execute the php script to add everything works fine and no exception are thrown. But the data is not added to the database, which ...
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 ...
6
votes
4answers
39k 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 ...
5
votes
4answers
16k views

Magento 2: how to get Model in Block

I want to use an instance of a Model into a Block. in Magento 1 they uses: $exple = Mage::getModel('exple/standard'); How can I do this with Magento 2?
4
votes
2answers
13k views

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

I have a custom table and I have written a custom model for it too, but I am not able to understand how can I perform something like Mage::getModel('')->setData(). I have followed the required ...
1
vote
2answers
22k views

getting customers names

I am trying to display all my users first and last names and email addresses to practice pulling values from the database and joining tables. I have been trying to dig through the code in app/code/...
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 ...
4
votes
1answer
3k views

add attribute to admin user

I need to add a field that I did to the customer, but also to the admin user. I have the customer one working, $installer->addAttribute('customer', 'ldap_user', array( 'type' => '...
4
votes
2answers
4k views

Truncate table using resource model in Magento 2

I have a custom module and i want to add remove every data including the increment id, using resource model in magento 2, i've read the solution in here but this is for magento 1, i'm not sure how to ...
3
votes
1answer
3k views

Filtering error when using custom Varien object collection in admin grid

I have created a custom Varien object collection for my admin module (because the data is not from a db) and have got it successfully to show in an admin grid. I built the collection by using the ...
1
vote
1answer
213 views

Magento 2.3.2: How to override a Inventory Deduction API model?

I want to override below Magento core file vendor/magento/module-inventory-source-deduction-api/Model/SourceDeductionService.php to add code $qty=0; inline 83. I tried by my self but getting an ...
1
vote
5answers
10k views

load by attribute sku not working some times

For Loading product by sku i'm using following function. For some skus its working fine where as for some sku's not working.And these sku products existed in back end. $_product = Mage::getModel('...
28
votes
2answers
321 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 (...
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....
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 ...
6
votes
3answers
470 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> ...
5
votes
1answer
10k views

Retrive the Shipping method and shipping rates programmatically [closed]

I had overwriten the shipping Model , I need a carrier rates from the carrier codes for the further calculation in model class. Thanks in advanced.
7
votes
2answers
6k 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 ...
6
votes
1answer
398 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 ...
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 ...
5
votes
2answers
5k views

Get a Magento product collection without sub-products of configurable products

I want to get a list of all products available in my Magento installation. Currently I am using the following call to filter out enabled products and limit the result set to those products which are ...
4
votes
3answers
4k views

Print INSERT Query in saveAction [duplicate]

I have a custom module where my few DB fields are not being saved properly. I can see the values selected in $model->getData(); but it does not reflects in my Database. try { echo '<...
2
votes
2answers
9k views

Fetch Single Record from Custom Module Table

I have a custom module with a custom DB. The DB consist of customer_id field and inserted customer_id into it. How can I get a single record based on customer_id passed, whereby customer_id is the ...
2
votes
1answer
3k views

Is it possible to save attribute value per store in custom eav based model?

Let's say I have custom EAV entity type with it's tables that have store field (just like catalog/product tables have). Can default EAV resource model save data per store? From what I have seen, in ...
1
vote
2answers
426 views

custom model magento looks in Mage/…?

I try to follow this tutorial: http://devdocs.magento.com/guides/m1x/magefordev/mage-for-dev-5.html Problem is it doesn't work at all as Magento always tries to load the model from the wrong folder ...
1
vote
2answers
2k views

Can we call model methods in Magento setup scripts?

Can we call model methods in Magento setup scripts? I tried this, setup script is executed (new entry appears in the database table core_resource) but model methods are not executed (I added Mage:log('...
7
votes
1answer
808 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
32k 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 = $...
5
votes
2answers
214 views

Where’s The V in Magento's MVC? And is there better name?

When you look at the Magento directory structure, the C for ‘controller’ and the M for Model are easily identified as files and classes. But where are the Views and what are they?
5
votes
4answers
7k views

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

I get the quote_item_id like this: $orderItems = $order->getAllItems(); foreach($orderItems as $orderItem){ $qouteItemId = $orderItem->getQuoteItemId(); } I need to load quote item ...
4
votes
2answers
7k views

Admin Create New Order Observer

I am trying to locate the correct observer to capture the data when the admin creates a new order. I do not want this observer to be triggered when frontend orders are placed. (Only admin backend ...
3
votes
1answer
2k views

Cancel save() in custom model if condition is met in _beforeSave() [duplicate]

I have a custom model and I want to add a validation in the _beforeSave() method so that if it's a new object, and no customer_id has been set, it shouldn't save the record at all. I did the ...
2
votes
1answer
855 views

Mysql query to Model, Resource Model, Collection - Magento 2

I can easily get or set value using ORM. I felt difficult to convert mysql query logic to Model, Resource Model, Collection(ORM). for example I am getting last one hour data using the below query ...
1
vote
1answer
2k views

Magento 2 - Collection on custom table problem

I created my custom table on magento, with the InstallSchema and InstallData and It works fine, then I created my Model class, initially it seemed to work, because I used the collection in one Block ...
0
votes
2answers
353 views

Need Magento 2 equivalent for Model Filter

I need a help to porting below code to Magento 2. $groups=Mage::getModel('mppartnergroup/mppartnergroup') ->addFieldToFilter('status',array('eq'=>1))
9
votes
1answer
2k 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 ...