Magento getFirstItem and getLastItem not working on collection
try this
$shipModelCollection = Mage::getModel('orderstatus/orderstatus')->getCollection();
$shipModelCollection->addFieldToFilter('shipment_incrementid', array(array('like' => $shipidarr[0].'%')));
if($shipModelCollection->getSize()){
$lastItem = $shipModelCollection->getLastItem();
}
The reason you are not getting result with getFirstItem
or getLastItem
on your collection is because they are function which return values. If a function returns data, then you must use a variable to store the returned value.
check Varien_Data_Collection::getFirstItem()
in lib\Varien\Data\Collection.php
/**
* Retrieve collection last item
*
* @return Varien_Object
*/
public function getLastItem()
{
$this->load();
if (count($this->_items)) {
return end($this->_items);
}
return new $this->_itemObjectClass();
}
You can see in its details @return Varien_Object
it returns Varian_Object
.
As this function returns data, you have to use a variable to get returned data or it will give you full collection data with filter.
Thats why
$shipModel->addFieldToFilter('shipment_incrementid', array(array('like' => $shipidarr[0].'%')))->getLastItem();
will give you full collection with filtered data and
$lastItem = $shipModel->addFieldToFilter('shipment_incrementid', array(array('like' => $shipidarr[0].'%')))->getLastItem();
will give you last item by code end($this->_items);