我已经构建了这个函数,它将把记录插入到数据库中,我将在稍后使用。然而,一半的功能工作完美,另一半导致错误(错误不显示在日志中)
<?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]: Syntax error or access violation: 1064 SQL语法错误;检查与MySQL服务器版本对应的手册,以便在第1行"nfr_familypack"附近使用正确的语法,查询为:SELECT:email FROM:tableName;
不能像下面这样为数据库对象使用命名参数:
SELECT :email FROM :tableName
当你考虑准备好的语句是什么和做什么(允许MySQL引擎在不知道要传递给它的特定参数的情况下预先计划查询)时,很明显你不能参数化MySQL需要准备查询的关键数据。例如,MySQL如何知道它正在针对哪个表创建查询执行计划?
我刚刚尝试打印一个完整的查询,它看起来像这样
SELECT email FROM 'nfr_familypack';
注意tableName周围的单引号。PDOStatement用单引号包装了绑定参数。这会导致Mysql语法错误