我已经构建了这个函数,它会将记录插入到数据库中,稍后我将使用它。但是,一半功能完美运行,另一半功能导致错误(错误不会显示在日志中)
<?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;
}
}
}
?>
EDIT ::
我已经打破了很多并且正在调试代码。我有这个错误,我试图重写脚本但仍然最终结果相同。关于以下错误的任何想法??
保存此配置时发生错误:SQLSTATE [42000]:语法错误或访问冲突:1064 SQL语法中出错;检查与MySQL服务器版本对应的手册,以便在第1行的''nfr_familypack'附近使用正确的语法,查询为:SELECT:email FROM:tableName;
答案 0 :(得分:1)
我刚刚尝试打印完整的查询,看起来像这样
SELECT email FROM 'nfr_familypack';
注意tableName周围的单引号。 PDOStatement用单引号包装了绑定参数。所以它会导致Mysql语法错误
答案 1 :(得分:1)
您不能像在这里尝试那样使用数据库对象的命名参数:
SELECT :email FROM :tableName
当你考虑准备好的语句是什么和做什么时(允许MySQL引擎在不知道要传递给它的特定参数的情况下预先规划查询),显然你不能参数化MySQL需要的关键数据。准备查询。例如,MySQL如何知道它正在创建一个查询执行计划的表?