I have a collection of attributes which was made by combining several tables. I want to get the primary key of the objects resides in this collection. Any suggestions would be helpful,
Magento 1.9 – How to Get Primary Key of Collection Objects
custom-collectionmagento-1.9
Related Solutions
So let's go trough this line by line
$_productCollection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToFilter('popular_walldecal', array('eq' => 1))
->setPageSize(8);
Perfect, no need to change this. Just make sure you have the flat product data enabled under System > Configuration > Use Flat Catalog Product
and the attribute popular_walldecal settings Use in product listing
set to yes
$_collectionSize = $_productCollection->count();
Loads the whole collection, I suggest using getSize()
instead of count
since the first doesn't load the collection.
foreach ($_productCollection as $product):
$_product = Mage::getModel("catalog/product")->load($product->getID());
endforeach;
Now here's the real issue. The collection just executes one query over one table (catalog_flat_product_[id]) but loading the product model in the loop executes a whole bunch of queries per iteration. It's better to add the required data to the collection.
$_productCollection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToFilter('popular_walldecal', array('eq' => 1))
->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
->setPageSize(8);
$_productCollection->addFinalPrice()
->addUrlRewrite();
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection(_productCollection);
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($_productCollection);
This will
- filter by
popular_walldecal
- add all the attributes in the catalog_flat_product_[id] table
- set the page size to 8
- add the final price and url rewrite data for the product url
- filter the collection by visibility in catalog
- filter the collection by status enabled
And now you're good to go!
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect(array('meta_title','meta_description'));
foreach ($collection as $product) {
print_r($product->getData());
}
output
Array ( [entity_id] => 231 [entity_type_id] => 4 [attribute_set_id] => 13 [type_id] => simple [sku] => msj000 [has_options] => 0 [required_options] => 0 [created_at] => 2013-03-05 10:48:12 [updated_at] => 2017-07-07 11:35:03 [meta_title] => fgdg [meta_description] => dfgfdgf [is_salable] => 1 [stock_item] => Varien_Object Object ( [_data:protected] => Array ( [is_in_stock] => 1 ) [_hasDataChanges:protected] => [_origData:protected] => [_idFieldName:protected] => [_isDeleted:protected] => [_oldFieldsMap:protected] => Array ( ) [_syncFieldsMap:protected] => Array ( ) ) )
Best Answer
Whenever you have an item from a collection calling
->getId()
on the item will return the primary key value of that item.To get the name of the field from a certain collection the following snippet should help