Friday, November 25, 2011

Magento Model Collection

Magento data collection inhert from the object
public function productAction()
{
$collection_of_products = Mage::getModel('catalog/product')->getCollection();
var_dump($collection_of_products->getFirstItem()->getData());
}

Most Magento Model objects have a method named getCollection which will return a collection that, by default, is initialized to return every Object of that type in the system.

The products Collection, as well as many other Magento Collections, also have the Varien_Data_Collection_Db class in their ancestor chain if you want to see the select statement your Collection is using



public function productAction()
{
$collection_of_products = Mage::getModel('catalog/product')->getCollection();
var_dump($collection_of_products->getFirstItem()->getData());
}

Magento uses the Zend db aabstraction layed so select is also an object

public function productAction()
{
$collection_of_products = Mage::getModel('catalog/product')->getCollection();
var_dump(string($collection_of_products->getSelect()));
}

The discrepancy depends on which attributes you're selecting, as well as the aforementioned indexing and cache. If you've been following along with the other articles in this series, you know that many Magento models (including the Product Model) use an EAV system. By default, a EAV Collection will not include all of an Object's attributes. You can add them all by using the addAttributeToSelect method

publice function productAction()
{
$collection_of_products = Mage::getModel('catalog/product')
->getCollection()
->getAttributeToSelect('*');
->getAttributeToSelect('meta_title')
->getAttributeToSelect('price');

}

No comments:

Post a Comment

Thankyou for your Comments

LinkWithin

Blog has moved, searching new blog...