Questions tagged [collection]

Questions related to Magento collections

Filter by
Sorted by
Tagged with
83
votes
6answers
82k views

Difference between getSize() and count() on collection

I have heard many times that they both are the same. But I am facing a weird issue, in the product collection of CatalogSearch module, count() is returning correct product count while getSize() is ...
45
votes
5answers
118k views

How to get all items in cart currently?

I want to get a collection of all the items in cart currently. How can I do this ? Any suggestions will be appreciated.
38
votes
3answers
32k views

Most efficient way to get all ID's from a collection

In the past to get all ID's of a product collection, I have always used getAllIds on the collection, believing this was a method that prevented the full collection load with data etc. But, I actually ...
29
votes
1answer
28k views

About Collection Model and Resource Model

I am bit confused about using resource model and collection model. Sometimes when I see examples for loading products using specific attributes, in some examples, they use collection model and ...
27
votes
2answers
75k views

Left join a table on a collection query

I am doing the following to get some orders out of the system for export: $orders = Mage::getModel('sales/order')->getCollection() ->addFieldToFilter('status', $statusToExport) ->...
25
votes
1answer
865 views

Are collections history in Magento 2?

I know that a lot of code that is currently in Magento 2 (2.1.2) is more or less ported from Magento 1 and that a lot of code will be substituted by an equivalent in the future. In this aspect, I'm ...
23
votes
3answers
11k views

How to truncate magento table using collection?

I have a custom table. I want to truncate the table using Magento collection without SQL query. Hope someone will provide some useful information.
21
votes
2answers
11k views

Is it possible to iterate over Magento collections with pagination natively?

What I mean by that is - is there a way to do: $collection = $model->getCollection(); foreach ($collection as $item) { $item->doStuff(); } In such a way that even if the collection had ...
20
votes
5answers
5k views

Issue using “having” in Magento collection

I am trying to build a custom collection for a grid in Magento admin module. I have created a new collection method called "addAttributeHaving" which just does the following: public function ...
19
votes
2answers
66k views

addFilter vs addFieldToFilter

Magento collection has two methods for filtering: 1. Varien_Data_Collection_Db::addFieldToFilter 2. Varien_Data_Collection::addFilter Seems that both methods add where condition to Zend_Db_Select. ...
19
votes
4answers
26k views

How can I fix “Item (Mage_Catalog_Model_Product) with the same id ”xxx“ already exist”?

I have been getting this error when trying to filter a products collection Item (Mage_Catalog_Model_Product) with the same id "6058" already exist and wanted to ask what could ne causing the error ...
18
votes
2answers
74k views

How to get attribute “option label/attribute text” having “attribute value” (option_id)?

Suppose I have an attribute that is a collection of option (dropdown/multiselect). I can retrieve the attribute value for a given product: $store_id = [something]; $productId = [something]; // this ...
18
votes
7answers
103k views

Get product collection from a category id

I am trying to get a collection of products from a category id. A few things I have tried are in the block : $category = Mage::getModel('catalog/category')->load(123) ->...
17
votes
6answers
59k views

How to print collection mysql query in magento 2?

There is getSelect()->__toString(); is available in Magento 1 for print query of collection. like below example $products = Mage::getModel(‘catalog/product’) ->addAttributeToFilter(‘status’, ...
17
votes
3answers
35k views

Filtering results using LIKE

Consider these three "haystack" strings: a) foo bar b) welcome to foo bar industries c) foo barer And now my "needle": foo bar (Heh) I would like my filter to match my needle with haystack ...
17
votes
1answer
25k views

how to add pagination on custom collection in Magento 2

I am working on custom module. How can i use default magento pagination with my custom collection and also set custom limits on that?
16
votes
2answers
66k views

Magento 2 get Parameters in the URL [duplicate]

I have created an admin form with phtml content and a ui component grid. Phtml section working fine. Now I want to filter the grid collection based on the url param. I have used the below code $this-&...
16
votes
2answers
28k views

Difference Between setData() and addData()

What's the difference between setData() and addData()? Is there a connection if I update a product or set a new one?
16
votes
4answers
19k views

Add Column to a grid (observer) - Column ‘store_id’ in where clause is ambiguous issue

I'm adding a column to the order grid using observer approach: On the event -> sales_order_grid_collection_load_before I'm adding a join to the collection On the event -> ...
15
votes
3answers
39k views

Magento2 Order by clause in collection

I have a custom collection to which I add filters. For example, I have this: $giftColletion = $this->_giftFactory->getCollection(); $giftColletion->addFieldToFilter('store_id', 1); What ...
15
votes
4answers
24k 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 ...
14
votes
5answers
14k views

Filter product collection by non-flat attribute

I am doing the following: $productCollection = Mage::getModel('catalog/product') ->getCollection(); $productCollection ->addAttributeToFilter('my_attribute', 1); my_attribute is not ...
13
votes
5answers
61k views

Magento - Unable to set order of collection

This doesn't seem to be ordered correctly, anything I'm doing wrong? Suggestions? $componentQuantityCollection = Mage::getModel('catalog/product')->getCollection(); $componentQuantityCollection-&...
13
votes
1answer
29k views

How to use group by in magento collection with join query

In module's admin grid I am using this code to get collection and group them by customer id $collection = Mage::getModel('referafriend/statistics')->getCollection(); $collection->getSelect()-&...
13
votes
5answers
20k views

To add default filter to grid in Magento2?

I have created one custom module with title & status field attribute in admin grid. Grid collections in module_test_grid_block.xml <arguments> <argument name="id" ...
13
votes
3answers
17k views

Layered navigation for custom collection on custom page - magento2

I am working on fetching layered navigation in magento2 for a custom product collection. I am getting custom collection already on the custom page need to show layered navigation. Tried to adapt this ...
12
votes
4answers
16k views

How to get product collections by set of ids in magento 2?

I want something like the "IN CLAUSE" in MYSQL via magento2 ORM, for example: select * from product where id in (...) but by using getCollection in magento 2 to get something like this: $...
12
votes
1answer
19k views

How to specify custom sort order for product collection?

I am trying to create a product collection based on array of products ids, and also sort the collection based on the ids array. $productIds = array(318,310,311); $collection = Mage::getModel('catalog/...
12
votes
2answers
8k views

How to clone the collection in Magento?

I have one collection in a method where I want to perform two different operations on this collection. So, I want two separate copies of same collection, and then assign one of the two collections to ...
11
votes
4answers
23k views

Best practice to join product attributes

I have a custom table with a product reference product_id. Now I would like to show product information (sku, name) in my backend grid, but I am unsure what is the best practice to do this? My best ...
11
votes
4answers
29k views

Magento 2: How to Filter a Product Collection By Store ID

Using a product factory object, I'm able to create a product, grab a product collection, and fetch the first item of that collection /* var $productFactory \Magento\Catalog\Model\ProductFactory */ $...
10
votes
4answers
30k views

Magento 2 how to get order collection

how to get order collection in custom module ? like My Orders collection in customers dashboard (frontend). this collection how to get?
10
votes
3answers
21k views

Magento2: Load Model Data By Another Field

I have below table named "messages". id | posts_id | message_description | created_at ----------------------------------------------------------------------------- 1 1 test1 ...
10
votes
3answers
9k views

Reload collection

Is there any chance to reload collection in Magento 2? For example: foreach($items as $singleItems){ $collection = $this->_myCollection->addFieldToFilter('filed', $singleItems->something); ... ...
10
votes
6answers
8k views

magento 2: How to use the order by and limit clause in sql query

I am using following code to filter result and i want to limit the result. $giftColletion = $this->_giftFactory->getCollection(); $giftColletion->addFieldToFilter('store_id', 1); $...
10
votes
2answers
11k views

Magento 2: Filter Product Collection by Multiple Categories (Magento 2.1)

I'm using Magento 2.1.0 and I'm currently having difficulties in filtering product collection with multiple categories. I've used more than a couple of ways to make it work but it won't. Assuming: $...
10
votes
2answers
8k views

Whats the best way to check if a custom attribute exists in Magento2

In being careful, I want to check if my custom attribute exists before I attempt to utilize it in any way. I need to check against all attributes collection, not just the product collection. My ...
10
votes
3answers
2k views

Magento 1: Performance optimizations to delete entities

I'm currently trying to improve a couple of modules regarding performance. Some of you may know the usage of the walk() method on collection which is very useful to avoid looping through products ...
9
votes
2answers
10k views

Magento 2: Walk through collection and delete

I was wondering if there was a way of walking through a collection on Magento 2 and deleting entries from the database. In Magento 1, this could be done by doing something like the following: Mage::...
9
votes
5answers
17k views

How to log SQL query of the collection?

I want to log the SQL query for a custom collection. I tried the following code. But did not work. Any suggestions will be appreciated. Mage::log($collection->getSelect(),null,'test.log',true);
9
votes
1answer
19k views

How to filter collection by date range on Magento 2?

I want to filter collection according : start date < today end date > today I try this but my getSlides() function return empty results <?php namespace Vendor\Module\Block; use Magento\...
9
votes
2answers
5k views

Grid pagination not working when use group clause in collection

I m working on product grid but its pagination or product count not working(as it display wrong count). as my block _preparecollection function is as below.i have added category filter code in ...
9
votes
1answer
25k views

How to list all users

As part of an exercise I am trying to list all users email from my database on one page. So far the closest i've got is $user = mage::getModel('customer/customer')->getCollection()->getData(); ...
9
votes
3answers
2k views

Force products collection to use EAV instead of flat table

In Magento 2, how can I temporarily disable the flat catalog? I have a product collection associated with a frontend store and want it to be loaded via EAV tables. I looked how the collections ...
9
votes
3answers
22k views

Magento Filter collection by create times (today, yesterday, week, hour etc)

I have a custom collection that I wish to filter by created date and het entries created "yesterday" Collection Entries //dates are set in controller using setCreatedTime(Mage::getModel('core/date')...
9
votes
2answers
10k views

magento2 search_tmp join leads to empty results in category listing

I just imported products into fresh magento2 instance and encountered issue that category product listing block are empty, although the products successfully appear in database . I have debugged list....

1
2 3 4 5
33