2013-06-11 90 views
0

我真的很感謝有人幫助解決了這個問題。 我在Magento的方式 我有這種方法,它基本上需要兩組數據,並與他們創建一個SQL語句。Foreach循環返回查詢兩次

private function _getInsertSql($data, $itemData) 
{ 

    $sql = 'insert into orders_headers ('; 
    $keys = array_keys($data); 
    $sql .= implode(', ', $keys); 
    $sql .= ') values ('; 
    foreach($data as $value) { 
     $type = $value['type']; 
     $vv = $value['value']; 
     if ($type == 'number') { 
      $sql .= $vv; 
     } else { 
      $sql .= $this->_db->quote($vv); 
     } 
     $sql .= ','; 
    } 
    $sql = substr($sql, 0, strlen($sql)-1); 
    $sql .= ');'; 

    $first = 0; 
    $sql .= 'insert into orders_lines ('; 
    foreach($itemData as $data) { 
     if ($first <= 0) { 
      $keys = array_keys($data); 
      $sql .= implode(', ', $keys); 
      $sql .= ') values ('; 
     } else { 
      $sql .= '('; 
     } 
     foreach($data as $value) { 
      $type = $value['type']; 
      $vv = $value['value']; 
      if ($type == 'number') { 
       $sql .= $vv; 
      } else { 
       $sql .= $this->_db->quote($vv); 
      } 
      $sql .= ','; 
     } 
     $sql = substr($sql, 0, strlen($sql)-1); 
     $sql .= '),'; 
     $first++; 
    } 

    $sql = substr($sql, 0, strlen($sql)-1); 

    $sql .= ';'; 
    Mage::log("START" .$sql . "END", NULL, 'sql.log'); 
    return $sql; 
} 

正如你可以看到在最後我註銷.sql和奇怪它會產生相同的查詢兩次。因此在DB中插入相同的行兩次。

我不能爲我的生活看到它的循環到頂部和再次創建查詢。

輸出SQL是:

2013-06-11T15:37:45 + 00:00 DEBUG(7):STARTinsert成orders_headers(的orderID,日期時間,IP,的customerID,名,姓,地址1,地址,城鎮,縣,國家,郵編,電話,傳真,電子郵件,公司,deliveryName,deliveryAddress1,deliveryAddress2,deliveryTown,deliveryCounty,deliveryCountry,deliveryPostcode,deliveryTelephone,goodsTotal,shippingTotal,taxTotal,discountTotal,order_state,order_status,order_comments,order_save_time,狀態,shippingMethod,paymentID,paymentName,paymentDate,shippingID,orderNotes,paymentNameNative,shippingMethodNative,referURL,accTypeID,offerCode,randID,e_website,e_status,e_purchaseordref,e_statuschk,e_accepted)values('100004952','20130611153744','127.0.0.1' ,0,」 TES','S','S','','S','','GB','S','SA','','[email protected]','','TES S 」, 'S', '', 'S', '', 'GB', 'S', 'SA',28.88,7.25,4.81,0, '新', 'pending_awaiting_payment', '', '20130611153745' , 'P', 'udropship_default',5, '查詢', '',0, '', '查看', 'udropship_default', '主/站點/',0, '', '100004952','主/ ('order',productID,code,name,qty,weight,price,nameNative,taxamount,ooprice,ootaxamount,supplierID,supplierCost,supplierCostCurrencyID);插入xm1_orders_lines ,'order_state,order_status,order_save_time)values('100004952',2106,'UGWA050','Stainless Steel
葡萄酒冰桶,magnum 9 pt',1,10.0900,16.82,'不鏽鋼
葡萄酒冰桶,magnum 9 pt' ,'3.36,0,0,5,10.0900,1','new','pending_awaiting_payment','20130611153745'); END 2013-06-11T15:37:45 + 00:00 DEBUG(7):START插入orders_headers orderID,datetime,ip,customerID,姓氏,姓氏,地址1,地址2,城鎮,縣,國家郵政編碼,電話,傳真,電子郵件,公司,deliveryName,deliveryAddress1,deliveryAddress2,deliveryTown,deliveryCounty,deliveryCountry,deliveryPostcode,deliveryTelephone,goodsTotal,shippingTotal,taxTotal,discountTotal,order_state,order_status,order_comments,order_save_time,status,shippingMethod,paymentID, ('100004952','20130611153744','127.0.0.1',0,'TES,paymentName,paymentID,shippingID,orderNotes,paymentNameNative,shippingMethodNative,referURL,accTypeID,offerCode,randID,e_website,e_status,e_purchaseordref,e_statuschk,e_accepted) ','S','S','','S','','GB','S','SA','','Sggila.com','','TES S' , 'S', '', 'S', '', 'GB', 'S', 'SA',28.88,7.25,4.81,0, '新', 'pending_awaiting_payment', '', '20130611153745', 'P', 'udropship_default',5, '查詢', '',0, '', '查看', 'udropship_default', '主/站點/',0, '', '100004952','/主/ ','ZZZ888','','2106','');插入xm1_orders_lines(訂單ID,產品ID,代碼,名稱,數量,重量,價格,名稱,本地,taxamount,ooprice,oota ('100004952',2106,'UGWA050','不鏽鋼
葡萄酒冰桶,大酒杯9',1,10.0900,16.82,'不鏽鋼
葡萄酒(xamount,supplierID,供應商成本,supplierCostCurrencyID,order_state,order_status,order_save_time)冰桶,magnum 9 pt',3.36,0,0,5,10.0900,1,'new','pending_awaiting_payment','20130611153745'); END

任何人都可以提供幫助嗎?

謝謝

+0

你可以發佈什麼$ sql中的終值是什麼? – Joe

+0

我已將它添加到最初的問題 – user2475259

回答

2

我認爲foreach不給你兩次查詢,但你的方法被調用兩次。代碼中的日誌方法在foreach循環之外,日誌文件本身包含2個條目(包含在START和END中)。 最好找出你在哪裏調用方法並在那裏調試。

0

經過一番探索後,我發現afterordersaved事件被調用兩次。

我相信這是customer_save_observer_executed 要解決這個問題,我只是做了以下內容: -

if(Mage::register('customer_save_observer_executed')) { 
return; 
} 
Mage::log('afterOrderSavedObserver call', NULL, 'method_calls.log');     
$model = Mage::getModel('ordersintegration/export'); 
$model->afterOrderSavedXm1($order); 
Mage::register('customer_save_observer_executed', true); 

這現在工作