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

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

    I've got another way to add an or condition in the field:

    ->addFieldToFilter(
        array('title', 'content'),
        array(
            array('like'=>'%$titlesearchtext%'), 
            array('like'=>'%$contentsearchtext%')
        )
    )
    
    0 讨论(0)
  • 2020-12-04 10:32

    Here is my solution in Enterprise 1.11 (should work in CE 1.6):

        $collection->addFieldToFilter('max_item_count',
                        array(
                            array('gteq' => 10),
                            array('null' => true),
                        )
                )
                ->addFieldToFilter('max_item_price',
                        array(
                            array('gteq' => 9.99),
                            array('null' => true),
                        )
                )
                ->addFieldToFilter('max_item_weight',
                        array(
                            array('gteq' => 1.5),
                            array('null' => true),
                        )
                );
    

    Which results in this SQL:

        SELECT `main_table`.*
        FROM `shipping_method_entity` AS `main_table`
        WHERE (((max_item_count >= 10) OR (max_item_count IS NULL)))
          AND (((max_item_price >= 9.99) OR (max_item_price IS NULL)))
          AND (((max_item_weight >= 1.5) OR (max_item_weight IS NULL)))
    
    0 讨论(0)
  • 2020-12-04 10:35

    OR conditions can be generated like this:

    $collection->addFieldToFilter(
        array('field_1', 'field_2', 'field_3'), // columns
        array( // conditions
            array( // conditions for field_1
                array('in' => array('text_1', 'text_2', 'text_3')),
                array('like' => '%text')
            ),
            array('eq' => 'exact'), // condition for field 2
            array('in' => array('val_1', 'val_2')) // condition for field 3
        )
    );
    

    This will generate an SQL WHERE condition something like:

    ... WHERE (
             (field_1 IN ('text_1', 'text_2', 'text_3') OR field_1 LIKE '%text')
          OR (field_2 = 'exact')
          OR (field_3 IN ('val_1', 'val_2'))
        )
    

    Each nested array(<condition>) generates another set of parentheses for an OR condition.

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

    Thanks Anda, your post has been a great help!! However the OR sentence didnt' quite work for me and I was getting an error: getCollection() "invalid argument supplied for foreach".

    So this is what I ended with (notice the attribute being specified 3 times instead of 2 in this case):

      $collection->addFieldToFilter('attribute', array(  
        array('attribute'=>'my_field1','eq'=>'my_value1'),            
        array('attribute'=>'my_field2','eq'=>'my_value2') ));
    

    addFieldToFilter first requires a field and then condition -> link.

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