Fatal Error: Call to a Member Function getData() on a Non-Object in Magento 1.9

magento-1.9modelmodule

I have custom module lenses which is working well, but when I try to access data of lenses model it gives me an error. This is my code

 $Lenses= Mage::getModel('lenses/lenses');
  foreach($Lenses as $Lens)
  {
     $Lens->getData('description'); //or
     $Lens->getDescription();
  }

Best Answer

You are trying to iterate object which is not a collection. You may try following instead:

$Lenses= Mage::getModel('lenses/lenses')->getCollection();
foreach($Lenses as $Lens)
{
    $Lens->getData('description'); //or
    $Lens->getDescription();
}

If you want to access data of single object, you may do following:

$Lens = Mage::getModel('lenses/lenses')->load(12) // load by id
$Lens->getData('description'); //or
$Lens->getDescription();