Magento: Getting a shipment collection containing only shipments that contain a comment that contains a certain piece of text
I need to query for a collection of order shipments (Mage_Sales_Model_Mysql4_Order_Shipment_Collection
). I only want to see the order shipment on which a comment has been written that matches a certain piece of text.
In non-EAV SQL, it would l开发者_运维百科ook something like this:
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
Obviously I want to use Magento's native model methods to accomplish it:
$shipments = Mage::getResourceModel('sales/order_shipment_collection')
->addAttributeToSelect('*')
// ??
// ??
->load();
Is this possible?
To keep things neat I would put the following inside my own resource model which extends Mage_Sales_Model_Mysql4_Order_Shipment_Collection
.
public function addCommentsToFilter($comments = array())
{
return $this->join('sales/shipment_comment', 'main_table.entity_id=parent_id', 'comment')
->addFieldToFilter('comment', array('in'=>$comments));
}
Then call it with:
$shipments = Mage::getResourceModel('mymodule/my_custom_collection')
->addAttributeToSelect('*')
->addCommentsToFilter(array('Possible comment', 'Another possible comment'));
精彩评论