Minas Jean
Minas Jean

Reputation: 107

What is the usage of observer.php in a module of magento

Actually I working now in magento for developing a module to check the voucher code used or not. The details are stored in a new table. In my config.xml, I specified the observer page for fetching the details from db table. But I don't know the exact use of observer page in magento. Can I use observer page for this usage.

But it proceed to an error I checked the log file

which is

a:5:{i:0;s:203:"SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '=' at line 1";i:1;s:1677:"#0 C:\wamp\www\Mymagento\lib\Varien\Db\Statement\Pdo\Mysql.php(110): Zend_Db_Statement_Pdo->_execute(Array)

My observer.php file is also shown below

class Module_Voucher_Model_Observer {

 public function __contruct()
    {
        $coupon_code = trim(Mage::getSingleton("core/session")->getData("coupon_code"));
    }


public function getresultofVoucher($coupon_code)
{

$resource = Mage::getSingleton('core/resource');

$readConnection = $resource->getConnection('core_read');

$table = "voucher_code_status_table";

$query = 'SELECT * FROM ' . $table. 'WHERE value='.$coupon_code;

$results = $readConnection->fetchAll($query);

    return $results;
}

}

Please help what is the mysql error here. Please help as soon as possible

Thanks in advance

Upvotes: 1

Views: 573

Answers (2)

Gervs
Gervs

Reputation: 1397

An observer is an EventListener, events are dispatched in Magento with:

Mage::dispatchEvent('event_name', array('item' => $this));

When an event is dispatched, Magento will check which observers are bound to it, and will call the function defined in the config with a Varien_Event_Observer object as its parameter.

You're function could be something like this:

public function getresultofVoucher(Varien_Event_Observer $observer)
{
    $item = $observer->getItem();
    // do something with it
}

Upvotes: 1

Akash Pius
Akash Pius

Reputation: 336

Observer.php is a model class file, like all models this also can be called any were we need its function.

Normally we use observers when using magento events. In config.xml we declare events and we use observer functions to handle the event when it occurs.

I have gone through your error and code. It seems the code doesn't get the value of coupon code. Please check whether there is any value coming in $coupon_code.

That may be the issue.

Thanks.

Upvotes: 1

Related Questions