【问题标题】:Magento Observer, function ErrorMagento 观察者,功能错误
【发布时间】:2015-10-26 13:22:55
【问题描述】:

我已经构建了这个函数,它将记录插入到我稍后将使用的数据库中。然而,一半的功能完美运行,另一半导致错误(错误不会显示在日志中)

<?php
class Envato_CustomConfig_Model_Observer
{   
    public function adminSystemConfigChangedSection()
    {           

    /**
    *Insert new job Request
    *
    **/
        $tablename_c = Mage::getStoreConfig('customconfig_options/section_one/custom_field_one');
        $email = Mage::getStoreConfig('customconfig_options/section_two/custom_field_two');
        $days = Mage::getStoreConfig('customconfig_options/section_two/custom_field_three');

        $bind = array(
            'id'    => ' ',
            'tablename_c'   => $tablename_c,
            'email' => $email,
            'days'    => $days,
            'timeStamp' => now(),
        );

        //Open Database Conenction
        $write = Mage::getSingleton("core/resource")->getConnection("core_write");

        $query = "
        insert into Envato_CustomConfig_Job (Job_Id, tablename_colummname, email_template, days, timeStamp) 
        values (:id, :tablename_c, :email, :days, :timeStamp);
        ";

        $write->query($query, $bind);

    /**
    *Get Job Queue Ready
    *
    **/

        $res = "
        SELECT Job_Id, tablename_colummname, email_template, days  FROM Envato_CustomConfig_Job 
        WHERE tablename_colummname = :tablename_c 
        AND email_template = :email 
        AND days = :days 
        AND timeStamp =:timeStamp
        AND Job_Id != :id
        LIMIT 1;
        ";

        $result = $write->query($res, $bind);

        foreach($result as $record) {

            $Job_Id = $record['Job_Id'];
            $tablename_colummname = $record['tablename_colummname'];
            $days = $record['days'];
            $email_template = $record['email_template'];

            $email_Details[] = explode(".",$tablename_colummname);          

            if(!isset($email_Details['0']['2']))
            {
                $email_Details['0']['2'] = time();
            }
        }

        $bind = array(
            'Job_Id'    => $Job_Id,
            'tableName'   => $email_Details['0']['0'],
            'email' => $email_Details['0']['1'],
            'email_template' => $email_template,
            'days' => $days,
            'timeStamp' => $email_Details['0']['2'],
        );

        $query = "
        insert into Envato_CustomConfig_Job_Running (Job_Id, tableName, email, email_template, days, timeStamp) 
        values (:Job_Id, :tableName, :email, :email_template, :days, :timeStamp);
        ";

        $write->query($query, $bind);

下面的代码当包含在脚本停止工作的函数中时,这部分在 Magento 中不起作用。在您建议将其分解为较小的函数之前,我已经尝试过,但是当我运行或什至将另一个函数放入类中时,该站点不起作用。

switch ($bind['timeStamp']) {
        case is_numeric($bind['timeStamp']):
            //¨calculate email send date

            $email_Send_Date =  strtotime('+'.$bind['days'].' day', $bind['timeStamp']);
            $email_Send_Date = date('M d, Y', $email_Send_Date);

            $bind['timeStamp'] = date('M d, Y', $bind['timeStamp']);

            //Select Emails
            $selectBind = array(
            'email' => $email_Details['0']['1'],
            'tableName'   => $email_Details['0']['0'],
            );

            $res = "
            SELECT :email  FROM :tableName;
            ";

            $result = $write->query($res, $selectBind);

            foreach($result['email'] as $record) {      

            $binding = array(
                'Job_Id'    => $bind['Job_Id'],
                'email' => $record,
                'tableName'   => $bind['tableName'],
                'email_template' => $bind['email_template'],
                'timeStamp' => $bind['timeStamp'],
                'email_Send_Date' => $email_Send_Date,
            );


            $res = "

            insert into Envato_CustomConfig_Job_Queue
            values (:Job_Id, :email , :tableName, :email_template, :timeStamp, :email_Send_Date);

            ";

            $result = $write->query($res, $binding);
            }


            break;
        default:
            //timestamp is given.
            //Select Emails
            $selectBind = array(
            'email' => $email_Details['0']['1'],
            'timeStamp' => $email_Details['0']['2'],
            'tableName'   => $email_Details['0']['0'],
            );

            $res = "
            SELECT :email, :timeStamp  FROM :tableName;
            ";

            $result = $write->query($res, $selectBind);         

            $i = 0; 
            foreach ($result['timeStamp'] as $value) {  

                switch($value)
                {                                   
                case is_numeric($value):
                    // time()
                    $value = strtotime('+'.$bind['days'].' day', $value);
                    $value = date('M d, Y', $value);
                    break;
                default: 
                    // days + Unix
                    $value =  strtotime(str_replace(',', '', $value));
                    $value = strtotime('+'.$bind['days'].' day',$value);
                    $value = date('M d, Y', $value);
                    break;
                }

                $binding = array(
                'Job_Id'    => $bind['Job_Id'],
                'email' =>  $result['email'][$i],
                'tableName'   => $bind['tableName'],
                'email_template' => $bind['email_template'],
                'timeStamp' => $bind['timeStamp'],
                'email_Send_Date' => $value,
                );

                $res = "

                insert into Envato_CustomConfig_Job_Queue
                values (:Job_Id, :email , :tableName, :email_template, :timeStamp, :email_Send_Date);

                ";

                $result = $write->query($res, $binding);

                $i = $i+1;
            }

        break;
        }

    }

}
?>

编辑::

我已经把它分解了很多并且一直在调试代码。我得到了这个错误,我试图重写脚本,但最终结果仍然相同。关于以下错误的任何想法?

保存此配置时发生错误:SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“nfr_familypack”附近使用正确的语法,查询是:SELECT :email FROM :tableName;

【问题讨论】:

    标签: php mysql sql magento zend-framework


    【解决方案1】:

    您不能像在此处尝试那样对数据库对象使用命名参数:

    SELECT :email  FROM :tableName
    

    当您考虑准备好的语句是什么以及做什么(允许 MySQL 引擎在不知道要传递给它的特定参数的情况下预先计划查询)时,很明显您不能对 MySQL 需要的关键数据进行参数化准备查询。例如,MySQL 如何知道它正在针对哪个表创建查询执行计划?

    【讨论】:

      【解决方案2】:

      我刚刚尝试打印一个完整的查询,它看起来像这样

      SELECT email FROM 'nfr_familypack';
      

      注意 tableName 周围的单引号。 PDOStatement 用单引号包裹了绑定参数。所以会导致Mysql语法错误

      【讨论】:

        猜你喜欢
        • 2014-02-24
        • 1970-01-01
        • 2014-10-24
        • 2011-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多