Magento Observer,函数错误
Magento Observer, function Error
我已经构建了这个函数,它会将记录插入到我稍后会用到的数据库中。然而,一半的功能完美运行,另一半会导致错误(错误不会显示在日志中)
<?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 中不起作用。在你建议将它分解成更小的功能之前,我已经尝试过,但是当我 运行 或者甚至在 class 中放置另一个功能时,该网站不起作用。
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;
我刚刚尝试打印一个完整的查询,它看起来像这样
SELECT email FROM 'nfr_familypack';
注意表名周围的单引号。 PDOStatement 将绑定参数用单引号括起来。所以会导致Mysql语法错误
您不能像您在此处尝试的那样对数据库对象使用命名参数:
SELECT :email FROM :tableName
当您考虑准备语句是什么和做什么时(允许 MySQL 引擎在不知道要传递给它的特定参数的情况下预先计划查询),很明显您不能参数化MySQL 准备查询所需的关键数据。例如,MySQL 如何知道 table 它正在针对什么创建查询执行计划?
我已经构建了这个函数,它会将记录插入到我稍后会用到的数据库中。然而,一半的功能完美运行,另一半会导致错误(错误不会显示在日志中)
<?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 中不起作用。在你建议将它分解成更小的功能之前,我已经尝试过,但是当我 运行 或者甚至在 class 中放置另一个功能时,该网站不起作用。
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;
我刚刚尝试打印一个完整的查询,它看起来像这样
SELECT email FROM 'nfr_familypack';
注意表名周围的单引号。 PDOStatement 将绑定参数用单引号括起来。所以会导致Mysql语法错误
您不能像您在此处尝试的那样对数据库对象使用命名参数:
SELECT :email FROM :tableName
当您考虑准备语句是什么和做什么时(允许 MySQL 引擎在不知道要传递给它的特定参数的情况下预先计划查询),很明显您不能参数化MySQL 准备查询所需的关键数据。例如,MySQL 如何知道 table 它正在针对什么创建查询执行计划?