Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento Conditional addFieldToFilter

Tags:

magento

I wanted to create a IF CONDITION in collection. If $mtype is not empty then this condition work addFieldToFilter('main_table.m_type', $mtype) and if the criteria does not match this filter would not work.

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

I hope you people know about the answer of this question or you would guide me a better way to do.

Thanks, Hanan Ali

like image 697
Hanan Ali Avatar asked Jul 25 '26 10:07

Hanan Ali


1 Answers

$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();
like image 195
Roman Snitko Avatar answered Jul 28 '26 11:07

Roman Snitko