我想在集合中创建一个IF条件。如果$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();

希望大家知道这个问题的答案,或者您可以引导我采取更好的方法。

谢谢,
哈南·阿里

最佳答案

$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();

10-08 00:04