首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Magento Conditional addFieldToFilter

Magento Conditional addFieldToFilter
EN

Stack Overflow用户
提问于 2012-01-06 21:57:27
回答 1查看 714关注 0票数 3

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

代码语言:javascript
运行
复制
$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();

我希望你们知道这个问题的答案,或者你们能指导我做一个更好的方法。

谢谢,哈南·阿里

EN

回答 1

Stack Overflow用户

发布于 2012-01-06 22:10:46

代码语言:javascript
运行
复制
$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();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8758910

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档