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

后端 未结 10 1238
孤街浪徒
孤街浪徒 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();
    

提交回复
热议问题