Get all item-Ids from a Collection
-
12-12-2019 - |
Question
I found out how to get the first item from a collection:
$pageId = Mage::getModel('cms/page')->getCollection()
->addFieldToFilter('identifier', 'your name in the pages')
->getFirstItem()
->getId();
But now I have 3 IDs with the same name but different store views. How can I get these item-Ids? When I write that statement above without "getFirstItem()" and/or without "getId()" then Magento throws me an error.
Solution
Tipo,as you want using getFirstItem
(),it only given 1 item,
you need to remove this function
and add getAllIds
() function on this collection.
Full code:
$pageIds = Mage::getModel('cms/page')->getCollection()
->addFieldToFilter('identifier', 'your name in the pages')
//->getFirstItem()
->getAllIds();)
getAllIds() give all ids in an array.
OTHER TIPS
Try getAllIds($limit = null, $offset = null)
That is a method for a collection object. So, once you get your collection, you would do something like this:
$collection->getAllIds();
The $limit and $offset arguments are optional provide you with "paging" capabilities. E.g.
$collection->getAllIds(100,2500); // gets you 100 ids, starting from the 2500th of the collection (2500 - 2599)
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange