Magento makes (OR) use of collections

I have a custom module with a custom table, now I'm trying to do something like a filter for this table,

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

this is enough, and works fine, but I need to add another field, just if you do

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

it works, but it does (AND) and I want (OR), I looked into the Varien_Data_Collection_Db class, the addFieldToFilter function calls the _getConditionSql call, 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 can be executed (OR), I tried with

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

but the request he makes is

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

I need help here thanks

+3
source share
3 answers

, , addAttributeToFilter, addFieldToFilter. Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection, , addAttributeToFilter , 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 Varien_Data_Collection_Db where .


, , Mage. , addAttributeToFilter Collection.php Model\mysql4\News\. , Varien_Data_Collection_Db, Mage_Eav_Model_Entity_Collection_Abstract, .

, JD

+1

, : OR.

+1

addFieldToFilter, :

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

this works when I type sql (this is just part of the whole printed statement):

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

Now I'm trying to figure out how to use 'like'instead =.

hope this helps.

0
source

Source: https://habr.com/ru/post/1764540/


All Articles