【问题标题】:Magento: Getting a shipment collection containing only shipments that contain a comment that contains a certain piece of textMagento:获取仅包含包含特定文本的评论的货件的货件集合
【发布时间】:2011-05-17 11:44:43
【问题描述】:

我需要查询订单发货集合 (Mage_Sales_Model_Mysql4_Order_Shipment_Collection)。我只想查看已写入与某段文本匹配的评论的订单发货。

在非 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();

这可能吗?

【问题讨论】:

    标签: php collections magento


    【解决方案1】:

    为了保持整洁,我会将以下内容放入我自己的资源模型中,该模型扩展了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));
    }
    

    然后调用它:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-25
      • 1970-01-01
      • 2023-03-26
      • 2013-03-02
      相关资源
      最近更新 更多