php - 在 magento 中我的自定义模块上显示错误

标签 php mysql magento controller observers

我现在在 magento 开发一个模块来检查是否使用了优惠券代码。详细信息存储在新表中。在我的 config.xml 中,我指定了用于从数据库表获取详细信息的观察者页面。但我不知道magento中观察者页面的确切用途。我可以使用观察者页面来实现此用途吗?

但它继续出现错误,我检查了日志文件:

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)

我的observer.php文件也显示在下面

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;
    }
}

我的 Indexcontroller.php 如下所示:

class Module_Voucher_IndexController extends Mage_Core_Controller_Front_Action {
    /**
    * Coupon code checking
    **/
    public function indexAction() {
        $this->loadLayout();
        $block = $this->getLayout()->createBlock('Mage_Core_Block_Template','vouchercode',
        array('template' => 'voucher/vouchercode.phtml')
        );
        $this->getLayout()->getBlock('left')->append($block);
        $this->renderLayout();
    }

    public function CheckAction() {
        $coupon_code = $this->getRequest()->getParam('coupon_code');
        //$coupon_code ='63663';
        if ($coupon_code != '') {
            Mage::getSingleton("core/session")->setData("coupon_code",$coupon_code);     //("checkout    /session")->
        }
        else {
            //
            //echo 'error : Voucher code issue';
        }
        if ($this->getRequest()->getParam('url')) {
            header('HTTP/1.1 301 Moved Permanently');
            $gclid = $this->getRequest()->getParam('');
            $url = $this->getRequest()->getParam('url');
            header('Location: /' . $url . '?voucherbox=' . $gclid);
            die();
        }
        else {
            $this->_redirect("/");
        }
    }
}

我认为 Controller 页面中没有调用观察者页面函数的选项。

最佳答案

@SIBI A,我只是想回答有关观察员使用的问题。

观察者主要用于在magento应用程序流程中发生特定事件时添加更多功能/更改一些现有行为。

在关注观察者之前,我们应该了解 magento 中可观察的各种事件。例如,“sales_order_place_after”是 magento 在下新订单时触发的事件。

如果您在 magento 根文件夹中搜索字符串“Mage::dispatchEvent”,您可以看到许多在不同场合触发各种事件的语句。

让我们考虑以下场景。这只是一个理解基本概念的示例。

场景: 当客户下新订单时,我们需要更新 sales_flat_order 表中的“customer_note”字段。(注意:sales_flat_order 是保存每个订单基本信息的表)

尽管可能存在许多传统方法来完成此功能,但我们也可以通过新的观察者来完成此操作。让我写一下该过程的一些基本步骤

  1. 创建模块
  2. 创建模型类
  3. 添加一个观察者方法,用于监听事件“order_place_after”
  4. 定义观察者方法,在该方法内访问$order对象
  5. 更新 $order 对象中的“customer_note”字段并保存。

如果上述步骤完成并且工作正常,当我们下新订单时,我们可以看到“customer_note”列(在 sales_flat_order 表中)已通过我们的新观察者方法更新了值。

如果您检查 Mage_Sales_Model_Order 类中的 place() 函数,您可以看到 magento 触发了我们正在监听的事件“sales_order_place_after”。它将当前的 $order 对象作为参数传递给事件。我们将在观察者方法中获取此 $order 对象作为参数,因为我们正在监听此特定事件。

我在这里给出了可用于创建模块来完成上述任务的文件。

1) app/etc/modules/Research_OrderCustomerNoteModify.xml

<?xml version="1.0"?>
<config>
 <modules>
  <Research_OrderCustomerNoteModify>
   <active>true</active>
   <codePool>local</codePool>
  </Research_OrderCustomerNoteModify>
 </modules>
</config>

2) app/code/local/Research/OrderCustomerNoteModify/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Research_OrderCustomerNoteModify>
            <version>0.1.0</version>
        </Research_OrderCustomerNoteModify>
    </modules>
    <global>
        <models>
            <research_ordercustomernotemodify>
                <class>Research_OrderCustomerNoteModify_Model</class>
            </research_ordercustomernotemodify>
        </models>
        <events>
            <sales_order_place_after>
                <observers>
                    <research_ordercustomernotemodify_update_customernote_field>
                        <class>research_ordercustomernotemodify/observer</class>
                        <method>updateCustomernoteField</method>
                    </research_ordercustomernotemodify_update_customernote_field>
                </observers>
            </sales_order_place_after>
        </events>
    </global>
</config>

3) app/code/local/Research/OrderCustomerNoteModify/Model/Observer.php

<?php
class Research_OrderCustomerNoteModify_Model_Observer
{
    public function updateCustomernoteField($observer)
    {
        $order = $observer -> getEvent() -> getOrder();
        $currentNote = $order->getCustomerNote();
        $order->setCustomerNote('Customer has quoted this'.$currentNote);
        $order->save();
        return;
    }
}

关于php - 在 magento 中我的自定义模块上显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26355710/

相关文章:

php - Zend 框架 : Select with join and PDO name param binding

MySQL 与高 utf-8 代码点的比较问题

php - 将数据从一个 Controller 操作传递到另一个 Controller 操作 - Magento

php - 关于Paypal——2016年商户安全升级

php - Magento - 管理员缺少 CSS

php - 使用 strtotime 获取一个月中的第一个工作日

php - lessphp 加载文件 LESS 时出错

php - 从 PHP 数组读取时 [] 是什么意思?

javascript - 如何使 db 值在 jquery slider 内工作?

c# - 没有时间没有 string.format 的日期时间