3

I've a custom module with custom table, now, i'm trying to make something like a filter for that table,

Mage::getModel('comunity/news')->getCollection()
            ->addFieldToFilter('title', array('like'=>'%'.$this->getRequest()->getParam('q').'%'));

with this is enough, and works perfect, but i need to add another field, is simple if you do like this

Mage::getModel('comunity/news')->getCollection()
            ->addFieldToFilter('title', array('like'=>'%'.$this->getRequest()->getParam('q').'%'))  
           ->addFieldToFilter('shortdesc', array('like'=>'%'.$this->getRequest()->getParam('q').'%'));

it works to, but it make an (AND) and i want an (OR), i was lookin into Varien_Data_Collection_Db class, the function addFieldToFilter make a call to _getConditionSql where you can see something like this

        if (is_array($fieldName)) {
        foreach ($fieldName as $f) {
            $orSql = array();
            foreach ($condition as $orCondition) {
                $orSql[] = '('.$this->_getConditionSql($f[0], $f[1]).')';
            }
            $sql = '('. join(' or ', $orSql) .')';
        }
        return $sql;
    }

here an (OR) can be done, i've tried with

->addFieldToFilter(array (
                array('field'=>'title', 'like'=>'%'.$this->getRequest()->getParam('q').'%'),
                array('field'=>'shortdesc', 'like'=>'%'.$this->getRequest()->getParam('q').'%'),
            ))

but the query it makes is

SELECT `main_table`.* FROM `uhma_comunidad_articulos` AS `main_table` WHERE (())

i need some help here thanks

Castro Roy
  • 7,623
  • 13
  • 63
  • 97

3 Answers3

1

I think the issue is that you need to use addAttributeToFilter rather than addFieldToFilter. Look in Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection and you'll see that the addAttributeToFilter method is testing for an array then adding as an OR:

if (is_array($attribute)) {
       $sqlArr = array();
       foreach ($attribute as $condition) {
           $sqlArr[] = $this->_getAttributeConditionSql($condition['attribute'], $condition, $joinType);
       }
        $conditionSql = '('.join(') OR (', $sqlArr).')';
        $this->getSelect()->where($conditionSql);
        return $this;
    }

addFieldToFilter in Varien_Data_Collection_Db is just adding the field to the where without testing for an array.


EDIT

Just noticed that you are using this on a Collection from your own custom module, not a Mage product collection. That means that you'll need to implement the addAttributeToFilter method yourself in your Collection.php under Model\mysql4\News\. Or, instead of Varien_Data_Collection_Db, make your Collection extend Mage_Eav_Model_Entity_Collection_Abstract which includes the implementation already.

HTH, JD

Jonathan Day
  • 18,519
  • 10
  • 84
  • 137
  • actually addFieldToFilter does test for an array, read my post again – Castro Roy Sep 13 '10 at 17:06
  • also, i can't use addAttributeToFilter, cuz i'm not using EAV, i'm just using a simple table, if i try to use addAttributeToFilter i'll get a call to undefined method – Castro Roy Sep 13 '10 at 17:13
  • But if i extends my collection from Mage_Eav_Model_Entity_Collection_Abstract, will work for a single table?? – Castro Roy Sep 13 '10 at 18:42
  • My suggestion is that you edit Collection.php under YourNamespace\module\Model\mysql4\News\ to have the method addAttributeToFilter(). You can use Mage_Eav_Model_Entity_Collection_Abstract::addAttributeToFilter() as an example. That way you don't have to change the inheritance, but you get the benefit of the OR. – Jonathan Day Sep 15 '10 at 03:12
1

Maybe this post can help: Match as OR.

Community
  • 1
  • 1
jazkat
  • 5,600
  • 3
  • 25
  • 17
0

i have managed to do this with addFieldToFilter default functionality like this:

$collection->addFieldToFilter(
    array('first_name','last_name'),
    array($post['firstName'],$post['lastName'])             
);

this is working when i print sql (this is just part from the whole statement being printed):

((first_name = 'r') OR (last_name = 't'))

now i am trying to understand how to use 'like' instead of =.

hope it helps.

R T
  • 4,369
  • 3
  • 38
  • 49