6

My code is as below but it won't work with loading first or last item.

    $shipidarr[0] = 100007536;

$shipModel = Mage::getModel('orderstatus/orderstatus')->getCollection();
    $shipModel->addFieldToFilter('shipment_incrementid', array(array('like' => $shipidarr[0].'%')))->getLastItem();
  • Do you get data by var_dump($shipModel->getData()) after you get collection? – Jaimin Sutariya Mar 24 '17 at 5:31
  • yes get whole collection. – MageLerner Mar 24 '17 at 5:35
  • 1
    Then I think below answer should work for you. – Jaimin Sutariya Mar 24 '17 at 5:42
2

try this

$shipModelCollection = Mage::getModel('orderstatus/orderstatus')->getCollection();
$shipModelCollection->addFieldToFilter('shipment_incrementid', array(array('like' => $shipidarr[0].'%')));

if($shipModelCollection->getSize()){
$lastItem = $shipModelCollection->getLastItem();

}
  • it's very strange.when i use $shipModelCollection->getLastItem(); it won't work.but when i use with another variable define it works. like you did $lastItem = $shipModelCollection->getLastItem(); how is this possible. – MageLerner Mar 24 '17 at 5:43
2

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);

0

Try this:

$shipModelCollection = Mage::getModel('orderstatus/orderstatus')->create()->addFieldToFilter('shipment_incrementid', array(array('like' => $shipidarr[0].'%')))->getLastItem();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.