2014-09-23 70 views
0

我正在與magento數據庫合作以獲取應用於產品的一些稅務相關事宜。所以在這裏我卡住了與我公司通過下面的查詢得到一個數組:Magento通過在foreach內循環更新數組

//Zend style query. 
      $select = $db->select() 
        ->from(array('t1' => $tableName1), 
        array('t1.order_id', 't1.item_id', 't1.product_id', 't1.store_id', 't1.price', 't1.tax_amount', 't1.tax_percent',new Zend_Db_Expr('"0" as product_taxclassid'))) 
        ->joinLeft(array('t2' => $tableName2),'t1.item_id = t2.item_id') 
        ->joinLeft(array('t3' => $tableName3),'t2.tax_id = t3.tax_id', array(new Zend_Db_Expr('t3.code as tax_calculation_rate_code'),'t3.priority')) 
        ->join(array('t4' => $tableName4),'t3.Code = t4.code',array('t4.tax_calculation_rate_id')) 
        ->where('t1.Order_id = ?',$OrderId)            
        ->where('t1.Item_id = ?',$ItemId);                 
      //Query 
      $result = $db->fetchall($select); 

      //return object 
      return $result;  

上述查詢給出了下面的結果回報:

array(2) { [0]=> object(stdClass)#5 (11) { ["order_id"]=> int(75) ["item_id"]=> int(127) ["product_id"]=> int(256) ["store_id"]=> int(4) ["price"]=> string(7) "47.0000" ["tax_amount"]=> string(7) "17.4200" ["tax_percent"]=> string(6) "5.0000" ["product_taxclassid"]=> int(0) ["tax_calculation_rate_code"]=> string(10) "Canada-GST" ["priority"]=> int(0) ["tax_calculation_rate_id"]=> int(3) } [1]=> object(stdClass)#6 (11) { ["order_id"]=> int(75) ["item_id"]=> int(127) ["product_id"]=> int(256) ["store_id"]=> int(4) ["price"]=> string(7) "47.0000" ["tax_amount"]=> string(7) "17.4200" ["tax_percent"]=> string(6) "7.3500" ["product_taxclassid"]=> int(0) ["tax_calculation_rate_code"]=> string(13) "Canada-VC-PST" ["priority"]=> int(1) ["tax_calculation_rate_id"]=> int(10) } } 

我試圖更新返回之前這個數組我想要更新的字段是product_taxclassid這是一個整數類型。所以我應該怎麼做,請建議我這樣我的返回陣列可以有["product_taxclassid"]=> int(0) 100

請幫我這個&讓我知道如果我在這裏做錯了什麼。

回答

0

大家好我自己找到答案,我發佈答案,任何人都有這種類型的問題與查詢。這是我做的:

$select = $db->select() 
        ->from(array('t1' => $tableName1), 
        array('t1.order_id', 't1.item_id', 't1.product_id', 't1.store_id', 't1.price', 't1.tax_amount', 't1.tax_percent',new Zend_Db_Expr('"0" as product_taxclassid'))) 
        ->joinLeft(array('t2' => $tableName2),'t1.item_id = t2.item_id') 
        ->joinLeft(array('t3' => $tableName3),'t2.tax_id = t3.tax_id', array(new Zend_Db_Expr('t3.code as tax_calculation_rate_code'),'t3.priority')) 
        ->join(array('t4' => $tableName4),'t3.Code = t4.code',array('t4.tax_calculation_rate_id')) 
        ->where('t1.Order_id = ?',$OrderId)            
        ->where('t1.Item_id = ?',$ItemId);                 
      //Query 
      $result = $db->fetchall($select); 

foreach($result as $result){ 
    $newArray[] = $result->toArray(); 
} 

foreach($newArray as $key => $value){ 
    $value['product_taxclassid'] = '100'; 
    echo $value['product_taxclassid']; 
} 

因此,它更新了我正在搜索的值。