Magento条件addFieldToFilter

时间:2012-01-06 13:57:27

标签: magento

我想在集合中创建一个IF CONDITION。如果$mtype不为空,则此条件有效addFieldToFilter('main_table.m_type', $mtype),如果条件不匹配,则此过滤器将无效。

$collection = Mage::getModel('manufacturers/manufacturers')->getCollection()
                                ->addStoreFilter(Mage::app()->getStore(true)->getId())
                                ->addFieldToFilter('main_table.status', 1)
                                ->addFieldToFilter('main_table.m_type', $mtype)
                                ->addOrder(Mage::helper('manufacturers')->getManufacturerSort(), Mage::helper('manufacturers')->getManufacturerOrder())
                            ->getData();

我希望你们的人知道这个问题的答案,否则你会指导我一个更好的方法。

谢谢, 哈南阿里

1 个答案:

答案 0 :(得分:1)

$collection = Mage::getModel('manufacturers/manufacturers')->getCollection()
    ->addStoreFilter(Mage::app()->getStore(true)->getId())
    ->addFieldToFilter('main_table.status', 1)
    ->addOrder(Mage::helper('manufacturers')->getManufacturerSort(), Mage::helper('manufacturers')->getManufacturerOrder());

if (!empty($mtype)) {
    $collection->addFieldToFilter('main_table.m_type', $mtype);
}

$data = $collection->getData();