Foreach循环返回两次查询

时间:2013-06-11 15:48:11

标签: php mysql magento

我真的很感激有人帮助解决问题。 顺便说一下,我在Magento 我有这个方法,基本上需要两组数据,并用它们创建一个sql语句。

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调试(7):STARTin进入orders_headers(orderID,datetime,ip,customerID,forename,surname,address1,address2,town,county,country,postcode,telephone ,传真,电子邮件,公司,deliveryName,deliveryAddress1,deliveryAddress2,deliveryTown,deliveryCounty,delivery国家,deliveryPostcode,deliveryTelephone,goodsTotal,shippingTotal,taxTotal,discountTotal,order_state,order_status,order_comments,order_save_time,status,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','','S @ gmila.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, '',' 100 004952','master / site /','ZZZ888','','2106','');插入xm1_orders_lines(orderID,productID,代码,名称,数量,重量,价格,nameNative,taxamount,ooprice,ootaxamount ,supplierID,supplierCost,supplierCostCurrencyID,orderCst,order_status,order_save_time)值('100004952',2106,'UGWA050','不锈钢
酒冰桶,马格南9 pt',1,10.0900,16.82,'不锈钢
酒冰桶,马格南9 pt',3.36,0,0,5,10.0900,1,'new','pending_awaiting_payment','20130611153745'); END 2013-06-11T15:37:45 + 00:00调试(7):STARTin进入orders_headers(orderID,datetime,ip,customerID,forename,surname,address1,address2,town,county,country,postcode,telephone,fax,电子邮件,公司,deliveryName,deliveryAddress1,deliveryAddress2,deliveryTown,deliveryCounty,delivery国家,deliveryPostcode,deliveryTelephone,goodsTotal,shippingTotal,taxTotal,discountTotal,order_state,order_status,order_comments,order_save_time,status,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','','S @ gmila.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, '','100004 952','/ master / site /','ZZZ888','','2106','');插入xm1_orders_lines(orderID,productID,代码,名称,数量,重量,价格,nameNative,taxamount,ooprice, ootaxamount,supplierID,supplierCost,supplierCostCurrencyID,order_state,order_status,order_save_time)值('100004952',2106,'UGWA050','不锈钢
酒冰桶,马格南9 pt',1,10.0900,16.82,'不锈钢钢制酒冰桶,大瓶9 pt',3.36,0,0,5,10.0900,1,'new','pending_awaiting_payment','20130611153745'); END

有人可以提供帮助吗?

由于

2 个答案:

答案 0 :(得分:2)

我认为foreach没有给你两次查询,但你的方法被调用了两次。代码中的log方法在foreach循环之外,日志文件本身包含2个条目(包含在START和END中)。 更好地找出你在哪里调用方法并在那里进行调试。

答案 1 :(得分:0)

经过一番挖掘后,我发现事件已经被调查了两次。

我相信是通过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);

现在可以使用