2

我想通过在Collection中应用OR条件从表中获取数据

我正在使用这些代码行

$collect = Mage::getModel('storelocater/storelocater')->getCollection()->addFieldToFilter(array(
                大批(
'属性' => '国家', 'eq' => '印度'),
数组(
'属性' => '状态', 'eq' => '向上')
)); 回声 $data = $collect->getSelect();

它打印输出

SELECT main_table.* FROM storelocaterAS main_tableWHERE (( Array = '' ) OR ( Array = '' ))

我也使用了addAttributeToFilter而不是addFieldToFilter但它返回致命错误

4

4 回答 4

5

addAttributeToFilter函数仅适用于 EAV 实体(如客户、产品、类别等)。ORaddFieldToFilter运算符的语法略有不同:

$collect  = Mage::getModel('storelocater/storelocater')
    ->getCollection()->addFieldToFilter(array('country','state'), array('india','up'));
于 2012-09-25T11:55:37.307 回答
4

除了Slayer Birden 的回答。

您还可以创建更复杂的查询,例如

    $rulesCollection
        ->addOrder('sort_order', Zend_Db_Select::SQL_ASC)
        ->addFieldToFilter(
            array('to_date', 'to_date'),
            array(array('gteq' => $now), array('null' => 'null')))
        ->addFieldToFilter(
            array('from_date', 'from_date'),
            array(array('lteq' => $now), array('null' => 'null')))
        ->addFieldToFilter('is_active', '1');

构造了这样的SQL:

     SELECT `main_table`.* FROM `enterprise_targetrule` AS `main_table` 
     WHERE ((to_date >= '2013-03-21') OR (to_date IS NULL)) 
     AND ((from_date <= '2013-03-21') OR (from_date IS NULL)) 
     AND (is_active = '1') 
     ORDER BY sort_order ASC
于 2013-03-21T15:49:32.303 回答
2
$collection->addFieldToFilter(array(
                array('attribute'=>'name', array('like' => '%'.$qry.'%')),
                array('attribute'=>'sku',  array('like' => '%'.$qry.'%'))));
for search by name and search by sku
于 2014-09-04T09:14:19.107 回答
0

对于 $collections = Mage::getModel('sales/order')->getCollection() ->addAttributeToFilter('increment_id', array('in' => $sellerIncrementIds)) ->addAttributeToFilter('status', ['in ' => ['待定','pending_seller_confirmation']]);

于 2017-10-16T15:13:23.680 回答