[英]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中不起作用。 在建議將其分解為較小的功能之前,我已經嘗試過,但是當我運行甚至將另一個功能放在類中時,該站點將無法正常工作。
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語法有錯誤;請參見表11。 檢查與您的MySQL服務器版本相對應的手冊,以在第1行的“ nfr_familypack”附近使用正確的語法,查詢為:SELECT:email FROM:tableName;
我只是嘗試打印一個完整的查詢,它看起來像這樣
SELECT email FROM 'nfr_familypack';
請注意tableName周圍的單引號。 PDOStatement用單引號引起來的綁定參數。 所以會導致Mysql語法錯誤
您不能像在此處嘗試那樣對數據庫對象使用命名參數:
SELECT :email FROM :tableName
考慮一下准備好的語句的作用和作用(允許MySQL引擎在不知道要傳遞給它的特定參數的情況下預先計划查詢),很明顯,您無法參數化MySQL准備查詢所需的關鍵數據。 。 例如,MySQL如何知道它針對哪個表創建查詢執行計划?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.