coderatlarge
coderatlarge

Reputation: 649

Call to a member function getData() on a non-object

I am a newbie in Magento and am trying to create a controller for my custom module. The controller code is:

class Shopez_HomeSearch_IndexController extends Mage_Core_Controller_Front_Action
{
    public function dothesearchAction()
    {
        $searchQuery = $this->getRequest()->getParam("param");
        $_obj = new Varien_Data_Collection();

        $mageFilename = 'app/Mage.php';
        require_once $mageFilename;
        ini_set('display_errors', 1);
        umask(0);
        Mage::app();
        $connection = Mage::getSingleton('core/resource')->getConnection('core_read');
        $sql        = "Select seller_id, store_title from marketplace_sellerprofile";
        $_obj       = $connection->fetchAll($sql);

        var_dump($_obj);

        foreach ($_obj as $_obj2):
                var_dump($_obj2->getData());
                //do something else once you get the data out of obj2

        endforeach;
    }
}

The dump from the query is perfectly fine but when I am trying to do a foreach to get individual field values out then I am getting the error mentioned above.

The var_dump($_obj) output is as follows:

   array (size=37)
   0 => 
   array (size=2)
   'seller_id' => string '1' (length=1)
   'store_title' => string 'The Goodness Store' (length=18)
   1 => 
   array (size=2)
   'seller_id' => string '2' (length=1)
   'store_title' => string 'XYZ Store' (length=22)
   ....

Upvotes: 0

Views: 1028

Answers (1)

Makwana Ketan
Makwana Ketan

Reputation: 1388

As you are using direct query on magento, you will not be able use getData() function. You are trying to get data in array format not object. So you can access data as below.

foreach ($_obj as $_obj2):
   var_dump($_obj2['seller_id']);
   var_dump($_obj2['store_title']);
endforeach;

Upvotes: 1

Related Questions