2010-11-29 52 views
0

我需要查询订单货件集合(Mage_Sales_Model_Mysql4_Order_Shipment_Collection)。我只想看看订单上的货件是否与某段文字相匹配。Magento:获取仅包含包含特定文本评论的货件的货件集合

在非EAV SQL,它会是这个样子:

SELECT shipments.id 
FROM shipments 
JOIN comments ON (
    shipments.id = comments.shipment_id 
    AND comments.content IN('Possible comment', 'Another possible comment') 
    ) 
GROUP BY shipments.id 

很显然,我想用Magento的原生模型的方法来完成它:

$shipments = Mage::getResourceModel('sales/order_shipment_collection') 
    ->addAttributeToSelect('*') 
    // ?? 
    // ?? 
    ->load(); 

这可能吗?

回答

2

为了保持整洁,我会在我自己的资源模型中放置以下内容,它扩展了Mage_Sales_Model_Mysql4_Order_Shipment_Collection

$shipments = Mage::getResourceModel('mymodule/my_custom_collection') 
    ->addAttributeToSelect('*') 
    ->addCommentsToFilter(array('Possible comment', 'Another possible comment')); 

public function addCommentsToFilter($comments = array()) 
{ 
    return $this->join('sales/shipment_comment', 'main_table.entity_id=parent_id', 'comment') 
     ->addFieldToFilter('comment', array('in'=>$comments)); 
} 

然后调用它