tags:

views:

52

answers:

1

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

A: 

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
actually addFieldToFilter does test for an array, read my post again
Kstro21
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
Kstro21
But if i extends my collection from Mage_Eav_Model_Entity_Collection_Abstract, will work for a single table??
Kstro21
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