>我现在可以使用此代码
->addAttributeToFilter('best_flag');
$collection->getSelect()->orwhere("at_best_flag. VALUE = '1'");
谢谢大家的帮助
如何在代码中使用 OR 条件
我的问题代码*
->addAttributeToFilter('best_flag', array('eq' => '1') )
但我测试获取选择 SQL 代码显示
AND(at_best_flag. VALUE = '1')
我想将 AND 更改为 OR 我可以为此代码设置条件吗?
更新后 问这个问题我尝试使用此代码但不起作用
->addAttributeToFilter(array(
array('attribute' =>'best_flag', array('eq' => '1')),
array('attribute' =>'best_from_date', array('date' => true, 'to' => $todayDate)),
array('attribute' =>'best_to_date', array('or'=> array(
0 => array('date' => true, 'from' => $todayDate),
1 => array('is' => new Zend_Db_Expr('null')))
), 'left')
))
但是结果与我想要的不匹配 日期结果不起作用,因为产品忽略到日期条件
我想在同一集合中选择带有按日期和按标志的产品
编辑产品列表
- 所有产品均为 23
- 在最佳范围内从 - 最佳到 = 10
- 如果只有标志 = 7(只是不在从 - 到 的范围内)
- 如果只有最佳从 = 23
- 如果最好 = 10
- 此集合的正确结果将有 17
我会尝试更新代码
$collection->addAttributeToFilter(array(
array('attribute' =>'best_from_date', array('date' => true, 'to' => $todayDate))),
array('attribute' =>'best_to_date', array('or'=> array(
0 => array('date' => true, 'from' => $todayDate),
1 => array('is' => new Zend_Db_Expr('null')))
), 'left')
)
->addAttributeToFilter('best_flag', array('eq' => '1'));
结果 = 7
->addAttributeToFilter(array(
array('attribute' =>'best_flag', array('eq' => '1')),
array('attribute' =>'best_to_date', array('or'=> array(
0 => array('date' => true, 'from' => $todayDate),
1 => array('is' => new Zend_Db_Expr('null')))
), 'left')
))
->addAttributeToFilter(array(
array('attribute' =>'best_flag', array('eq' => '1')),
array('attribute' =>'best_from_date', array('date' => true, 'to' => $todayDate))
))
结果 = 23
->addAttributeToFilter('best_to_date', array('or'=> array(
0 => array('date' => true, 'from' => $todayDate),
1 => array('is' => new Zend_Db_Expr('null')))
), 'left')
->addAttributeToFilter(array(
array('attribute' =>'best_flag', array('eq' => '1')),
array('attribute' =>'best_from_date', array('date' => true, 'to' => $todayDate))
))
结果 = 10
->addAttributeToFilter(array(
array('attribute' =>'best_flag', array('eq' => '1')),
array('attribute' =>'best_to_date', array('or'=> array(
0 => array('date' => true, 'from' => $todayDate),
1 => array('is' => new Zend_Db_Expr('null')))
), 'left')
))
->addAttributeToFilter('best_from_date', array('date' => true, 'to' => $todayDate))
结果 = 23
您可以将此代码用于或条件
$collection->addAttributeToFilter(
array(
array('attribute'=> 'attribute1','like' => 'value1'),
array('attribute'=> 'attribute2','like' => 'value2'),
array('attribute'=> 'attribute3','like' => 'value3'),
)
);
这将输出格式为 WHERE 子句:
WHERE ((attribute1 LIKE 'value1') OR (attribute2 LIKE 'value2') OR (attribute3 LIKE 'value3'))
关于您的更新,它不起作用,因为您将所有内容都切换到 OR 语句。试试这个:
$yourmodel->addAttributeToFilter(array(
array('attribute' =>'best_from_date', array('date' => true, 'to' => $todayDate))),
array('attribute' =>'best_to_date', array('or'=> array(
0 => array('date' => true, 'from' => $todayDate),
1 => array('is' => new Zend_Db_Expr('null')))
), 'left')
)
->addAttributeToFilter('best_flag', array('eq' => '1'));
这将呈现(best_from date OR best_to_date) AND best_flag
正如我所说,如果你使用@Emipro技术答案,你会得到OR,如果你->addAttributeToFilter()
链接,你会得到和,就像你一开始所做的那样。如果你想同时执行这两种操作,则只需混合使用这两种语法即可。
这
应该可以解决问题:
->addAttributeToFilter('best_flag', array('in' => array(1,2,3,4)))