Magento product collection

Magento data collection is very important factor in magento development, we are planning to write more data collection articles on this blog but today we are going to cover only product data collection. We are going to cover quite few examples to help you understand how the product data collection works in Magento.

What are data collections? In short the data collection are like select queries to get the data from the database.

Lets start with very basic product model data collection.

To get all products from the database you can run the following piece of code -:

$collection = Mage::getResourceModel('catalog/product_collection');

or

$collection = Mage::getModel('catalog/product')->getCollection();

The above should give you the basic attributes -:

    • Entity Id
    • Attribute set
    • Type Id
    • Visibility
    • Is Saleable

And if you want to get more information or columns in your collection then you can get more data using addAttributeToSelect($attribute, $joinType=false) function. Let’s have a look at few examples here -:

$collection = Mage::getResourceModel('catalog/product_collection') ->addAttributeToSelect('name') ->addAttributeToSelect('sku') ->addAttributeToSelect('description') ->addAttributeToSelect('price');

The above collection will get basic information plus name, sku, description and price. This should be used when you want to get specific attributes in the collection instead of all or basic information.

If you want to get all the product attributes in the collection then you can do the following -:

$collection = Mage::getResourceModel('catalog/product_collection') ->addAttributeToSelect('*');

Now we have learnt how to run the select queries using collection, lets have a look how can we add filter(s) or where clause(s) to our select queries using collection.

To add filter to collection, we have addAttributeToFilter($attribute, $condition=null, $joinType=’inner’) or addFieldToFilter($attribute, $condition=null) functions. Let’s have a look how can we use them with our collections.

In the following example, we are going to get all the products where product type is simple -:

$collection = Mage::getResourceModel('catalog/product_collection') ->addAttributeToFilter('type_id', Mage_Catalog_Model_Product_Type::DEFAULT_TYPE);

is equivalent to

where type_id='simple'

In the following example, we are going to get all the products where product type is simple and visibility is catalogue search -:

$collection = Mage::getResourceModel('catalog/product_collection') ->addAttributeToFilter('type_id', Mage_Catalog_Model_Product_Type::DEFAULT_TYPE) ->addAttributeToFilter('visibility', Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);

is equivalent to

where type_id='simple' and visibility=4

In the following example, we are going to get all the products where product type is simple or configurable -:

$collection = Mage::getResourceModel('catalog/product_collection') ->addAttributeToFilter('type_id', array( Mage_Catalog_Model_Product_Type::DEFAULT_TYPE, Mage_Catalog_Model_Product_Type_Configurable::TYPE_CODE ));

is equivalent to

where type_id='simple' or type_id='configurable'

Here is the list of other filters available to use in the addAttributeToFilter function -:

  • eq (=)
  • neq (!=)
  • like (like)
  • nlike (not like)
  • in (in)
  • nin (not in)
  • finset (find_in_set)
  • is (is)
  • notnull (is not null)
  • null (is null)
  • moreq (>=)
  • gt (>)
  • lt (<)
  • gteq (>=)
  • lteq(<=)
  • from (>=) – only available for dates
  • to (<=) – only available for dates

We already talked about select queries and filters. Now we are going to discuss about how to sort our result set. Don’t worry we have function for that as well which is called addAttributeToSort($attribute, $dir=’asc’)

Let’s have a look how we can use this function in our collection

In the following example, we are going to get all the products where product type is simple and the result set is going to be sorted by name -:

$collection = Mage::getResourceModel('catalog/product_collection') ->addAttributeToFilter('type_id', Mage_Catalog_Model_Product_Type::DEFAULT_TYPE); ->addAttributeToSort('name', 'ASC');

is equivalent to

where type_id='simple' order by name

In the following example, we are going to get all the products where product type is simple and the result set is going to be sorted by type ascending and then name descending-:

$collection = Mage::getResourceModel('catalog/product_collection') ->addAttributeToFilter('type_id', Mage_Catalog_Model_Product_Type::DEFAULT_TYPE); ->addAttributeToSort('type', 'ASC'); ->addAttributeToSort('name', 'DESC');

is equivalent to

where type_id='simple' order by type, name desc

Hope this article helped you in some way. Please leave us your comment and let us know what do you think?

Similar Posts