我正在尝试过滤magento API通过客户属性返回的订单。我试了几种方法,但似乎都不管用。
我正在使用Magento 1.4.1.1 atm, 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
这样的平面表上不可能吗?
每当我需要这样做的时候,我使用如下代码:
将EAV表(带属性)连接到平面表
它没有很好地优化,但你应该能够挑选出你需要的部分。
p。
我想我会解释我所说的优化是什么意思,因为它很重要。方法的核心是这一点:
->joinLeft(array($alias => $table),
'main_table.'.$mainTableForeignKey.' = '.$alias.'.entity_id and '.$alias.'.attribute_id = '.$attribute->getAttributeId(),
array($attribute->getAttributeCode() => $field)
);
如果你了解MySQL,那么你就会知道它在连接表时只会选择一个索引,越具体越好。在这种情况下,只有entity_id
和attribute_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'