2011-04-07 47 views
2

我試圖過濾由客戶屬性通過magento API返回的訂單。我嘗試了幾種方法,但似乎沒有任何工作。加入客戶屬性

我使用Magento的ATM 1.4.1.1和API做到這一點的時刻:

$billingAliasName = 'billing_o_a'; 
$shippingAliasName = 'shipping_o_a'; 


    $collection = Mage::getModel("sales/order")->getCollection() 
     ->addAttributeToSelect('*') 
     ->addAddressFields() 
     ->addExpressionFieldToSelect(
      'billing_firstname', "{{billing_firstname}}", array('billing_firstname'=>"$billingAliasName.firstname") 
     ) 
     ->addExpressionFieldToSelect(
      'billing_lastname', "{{billing_lastname}}", array('billing_lastname'=>"$billingAliasName.lastname") 
     ) 
     ->addExpressionFieldToSelect(
      'shipping_firstname', "{{shipping_firstname}}", array('shipping_firstname'=>"$shippingAliasName.firstname") 
     ) 
     ->addExpressionFieldToSelect(
      'shipping_lastname', "{{shipping_lastname}}", array('shipping_lastname'=>"$shippingAliasName.lastname") 
     ) 
     ->addExpressionFieldToSelect(
       'billing_name', 
       "CONCAT({{billing_firstname}}, ' ', {{billing_lastname}})", 
       array('billing_firstname'=>"$billingAliasName.firstname", 'billing_lastname'=>"$billingAliasName.lastname") 
     ) 
     ->addExpressionFieldToSelect(
       'shipping_name', 
       'CONCAT({{shipping_firstname}}, " ", {{shipping_lastname}})', 
       array('shipping_firstname'=>"$shippingAliasName.firstname", 'shipping_lastname'=>"$shippingAliasName.lastname") 
     ); 

這是默認的API調用,我猜。現在我只想加入一個名爲update的客戶屬性 - 我該如何實現這個簡單的任務?

或者這是不可能在像sales_flat_order這樣的平板上?

+0

+1對於'addExpressionFieldToSelect',我經常看不到使用正確。 – clockworkgeek 2011-04-07 11:43:16

回答

4

每當我需要做的這一點,我使用類似:
Joining An EAV Table (With Attributes) To A Flat Table

這不是很好的優化,但你應該能夠挑選出您需要的部分。

PS。
我想我會解釋我的意思是通過優化,因爲它很重要。在該方法的心臟是該位:

->joinLeft(array($alias => $table), 
    'main_table.'.$mainTableForeignKey.' = '.$alias.'.entity_id and '.$alias.'.attribute_id = '.$attribute->getAttributeId(), 
    array($attribute->getAttributeCode() => $field) 
); 

如果你知道MySQL的,那麼你就會知道,加入一個表時,越具體越好,只會選擇一個指標。在這種情況下,只有entity_idattribute_id字段正在使用,所以MySQL僅限於這些字段。兩列都被編入索引,但基數很低。

如果條件還包括實體類型,那麼MySQL可以選擇使用IDX_BASE,它按順序對列entity_type_id,entity_id,attribute_id,store_id進行索引(它需要從左到右處理它們)。因此,像這樣的東西會導致EAV性能大幅提高 - 這取決於「左」表上有多少行可能會好幾百或幾千倍。

$alias.'.entity_type_id='.$entityType->getId().' AND main_table.'.$mainTableForeignKey.' = '.$alias.'.entity_id AND '.$alias.'.attribute_id = '.$attribute->getAttributeId().' AND '.$alias.'.store_id=0'