Search code examples
mysqlmagentopdomoduleobservers

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


Solution

  • 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.