1

here is my code.. when i am using getData() , it will give whole array..

 $model = Mage::getModel('warranty/warranty');

                    $war = $model->getCollection()
                                    ->addFieldToSelect('*')
                                    ->addFieldToFilter('product_id',  array('eq' => $productId))
                                    ->load();

echo'<pre>';print_r($war->getData()); die;

output:

Array
(
    [0] => Array
        (
            [id] => 9
            [product_id] => 554
            [warranty_period] => 2 month
            [price_in_warranty] => 500
            [price_out_warranty] => 24
        )

)

but when i try to get id using

echo $war->getId(); die;

nothing will display. how to get id through above array?

2
  • 1
    you can not call get id on collection object May 25, 2016 at 9:20
  • 1
    if you have only one record in collection try as $war->getFirstItem()->getId() May 25, 2016 at 9:23

2 Answers 2

2

As you're dealing with a collection, you can't simply call:

$war->getId();

If you want the id of the first item of your collection you can call:

$war->getFirstItem()->getId();

If you want to get the ids of every items in your collection you can do:

foreach ($war as $item)
{
    echo $item->getId();
}
0
0

I think what you really wanted is this:

$war[0]->getId();

Because from your var dump, you can see that $war is a collection.

Of course, you can iterate through $war if you have multiple items in it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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