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

后端 未结 10 1222
孤街浪徒
孤街浪徒 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:14

    There is a bit of confusion going on here, but let me try to clarify things:

    Lets say you wanted sql that looked something like:

    SELECT 
        `main_table`.*, 
        `main_table`.`email` AS `invitation_email`, 
        `main_table`.`group_id` AS `invitee_group_id` 
    FROM 
        `enterprise_invitation` AS `main_table` 
    WHERE (
        (status = 'new') 
        OR (customer_id = '1234')
    )
    

    In order to achieve this, your collection needs to be formatted like this:

    $collection = Mage::getModel('enterprise_invitation/invitation')->getCollection();
    
    $collection->addFieldToFilter(array('status', 'customer_id'), array(
    array('status','eq'=>'new'),
    array('customer_id', 'eq'=>'1234') ));
    

    Now to see what this looks like you can always echo the query that this creates by using

    echo $collection->getSelect()->__toString();
    
    0 讨论(0)
  • 2020-12-04 10:17

    This is the real magento way:

        $collection=Mage::getModel('sales/order')
                    ->getCollection()
                    ->addFieldToFilter(
                            array(
                                'customer_firstname',//attribute_1 with key 0
                                'remote_ip',//attribute_2 with key 1
                            ),
                            array(
                                array('eq'=>'gabe'),//condition for attribute_1 with key 0
                                array('eq'=>'127.0.0.1'),//condition for attribute_2
                                    )
                                )
                            );
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-04 10:19

    To filter by multiple attributes use something like:

    //for AND
        $collection = Mage::getModel('sales/order')->getCollection()
        ->addAttributeToSelect('*')
        ->addFieldToFilter('my_field1', 'my_value1')
        ->addFieldToFilter('my_field2', 'my_value2');
    
        echo $collection->getSelect()->__toString();
    
    //for OR - please note 'attribute' is the key name and must remain the same, only replace //the value (my_field1, my_field2) with your attribute name
    
    
        $collection = Mage::getModel('sales/order')->getCollection()
            ->addAttributeToSelect('*')
            ->addFieldToFilter(
                array(
                    array('attribute'=>'my_field1','eq'=>'my_value1'),
                    array('attribute'=>'my_field2', 'eq'=>'my_value2')
                )
            );
    

    For more information check: http://docs.magentocommerce.com/Varien/Varien_Data/Varien_Data_Collection_Db.html#_getConditionSql

    0 讨论(0)
  • 2020-12-04 10:25

    To create simple OR condition for collection, use format below:

        $orders = Mage::getModel('sales/order')->getResourceCollection();
        $orders->addFieldToFilter(
          'status',
          array(
            'processing',
            'pending',
          )
        );
    

    This will produce SQL like this:

    WHERE (((`status` = 'processing') OR (`status` = 'pending')))
    
    0 讨论(0)
  • 2020-12-04 10:26

    I also tried to get the field1 = 'a' OR field2 = 'b'

    Your code didn't work for me.

    Here is my solution

    $results = Mage::getModel('xyz/abc')->getCollection();
    $results->addFieldToSelect('name');
    $results->addFieldToSelect('keywords');
    $results->addOrder('name','ASC');
    $results->setPageSize(5);
    
    $results->getSelect()->where("keywords like '%foo%' or additional_keywords  like '%bar%'");
    
    $results->load();
    
    echo json_encode($results->toArray());
    

    It gives me

    SELECT name, keywords FROM abc WHERE keywords like '%foo%' OR additional_keywords like '%bar%'.

    It is maybe not the "magento's way" but I was stuck 5 hours on that.

    Hope it will help

    0 讨论(0)
提交回复
热议问题