过滤集合

Magento 有一套强大的方法来过滤集合。由于集合中可以包含两种类型的对象,因此我们必须首先确定我们正在使用哪种类型的数据,然后才能对其进行过滤。Magento 为产品和类别等实体实施 EAV 数据模型。如果我们过滤 EAV 对象的集合,则有一组不同的方法可供使用。

在 Magento 中,订单不会存储为 EAV 对象。这使得订单集合成为过滤基本集合的一个很好的示例。

$collection_of_orders = Mage::getModel('sales/order')->getCollection();
$collection_of_orders->addFieldToFilter('status','processing');

如果我们查看产品系列,我们可以看到产品存储在 EAV 数据模型中。我们也可以轻松地按 EAV 属性进行过滤。

$collection_of_products = Mage::getModel('catalog/product')->getCollection();
$collection_of_products->addAttributeToFilter('visible',1);