0

I would really appreciate someones help with the problem problem. I'm in Magento by the way I have this method which basically takes two sets of data and creates an sql statement with them.

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;
}

As you can see at the end I'm logging out .sql and weirdly it produces the same query twice. Therefore inserting the same row twice in the DB.

I can't for the life of me see where its looping to the top and creating the query again.

The output sql is:

2013-06-11T15:37:45+00:00 DEBUG (7): STARTinsert into orders_headers (orderID, datetime, ip, customerID, forename, surname, address1, address2, town, county, country, postcode, telephone, fax, email, company, deliveryName, deliveryAddress1, deliveryAddress2, deliveryTown, deliveryCounty, deliveryCountry, 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','','[email protected]','','TES S','S','','S','','GB','S','SA',28.88,7.25,4.81,0,'new','pending_awaiting_payment','','20130611153745','P','udropship_default',5,'Cheque','',0,'','Cheque','udropship_default','master/site/',0,'','100004952','master/site/','ZZZ888','','2106','');insert into xm1_orders_lines (orderID, productID, code, name, qty, weight, price, nameNative, taxamount, ooprice, ootaxamount, supplierID, supplierCost, supplierCostCurrencyID, order_state, order_status, order_save_time) values ('100004952',2106,'UGWA050','Stainless Steel
wine ice bucket, magnum 9 pt',1,10.0900,16.82,'Stainless Steel
wine ice bucket, 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): STARTinsert into orders_headers (orderID, datetime, ip, customerID, forename, surname, address1, address2, town, county, country, postcode, telephone, fax, email, company, deliveryName, deliveryAddress1, deliveryAddress2, deliveryTown, deliveryCounty, deliveryCountry, 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','','[email protected]','','TES S','S','','S','','GB','S','SA',28.88,7.25,4.81,0,'new','pending_awaiting_payment','','20130611153745','P','udropship_default',5,'Cheque','',0,'','Cheque','udropship_default','master/site/',0,'','100004952','/master/site/','ZZZ888','','2106','');insert into xm1_orders_lines (orderID, productID, code, name, qty, weight, price, nameNative, taxamount, ooprice, ootaxamount, supplierID, supplierCost, supplierCostCurrencyID, order_state, order_status, order_save_time) values ('100004952',2106,'UGWA050','Stainless Steel
wine ice bucket, magnum 9 pt',1,10.0900,16.82,'Stainless Steel
wine ice bucket, magnum 9 pt',3.36,0,0,5,10.0900,1,'new','pending_awaiting_payment','20130611153745');END

Can anyone offer their help?

Thanks

2
  • Can you post what the final value of $sql is? Commented Jun 11, 2013 at 15:55
  • I've added it to the initial question Commented Jun 11, 2013 at 16:03

2 Answers 2

2

I think foreach is not giving you query twice but your method is being called twice. The log method in your code is outside the foreach loop and the log file itself contains 2 entries (wrapped in START and END). Better find out where you are calling the method and debug there.

Sign up to request clarification or add additional context in comments.

Comments

0

After a bit of digging around, I've found that the event afterordersaved IS being called twice.

I believe it is by customer_save_observer_executed To get around this I've just done the following:-

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);

This now works

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.