Magento addFieldToFilter: Two fields, match as OR, not AND

后端 未结 10 1237
孤街浪徒
孤街浪徒 2020-12-04 09:42

I\'ve been stuck on this for the last few hours. I got it working by hacking a few lines in /lib/Varien/Data/Collection/Db.php, but I\'d rather use the proper s

10条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-04 10:18

    public function testAction()
    {
            $filter_a = array('like'=>'a%');
            $filter_b = array('like'=>'b%');
            echo(
            (string) 
            Mage::getModel('catalog/product')
            ->getCollection()
            ->addFieldToFilter('sku',array($filter_a,$filter_b))
            ->getSelect()
            );
    }
    

    Result:

    WHERE (((e.sku like 'a%') or (e.sku like 'b%')))
    

    Source: http://alanstorm.com/magento_collections

提交回复
热议问题