标准条件;逻辑转换 (a+b)(c+d) 到 ((w ? x) ? y) ? z

Criteria condition; logical conversion (a+b)(c+d) to ((w ? x) ? y) ? z

我正在尝试在 Yii 中添加以下条件:

$a = [1,2,3];
$b = [1,2,3];

$criteria->addCondition('A IS NULL', 'AND');
$criteria->addInCondition('A', $a, 'OR');
$criteria->addCondition('B IS NULL', 'AND');
$criteria->addInCondition('B', $b, 'OR');

我希望下面的 MySQL 会出来:

WHERE (A IS NULL OR A IN (1,2,3)) 
  AND (B IS NULL OR B IN (1,2,3))

但不幸的是:

WHERE (((A IS NULL) OR A IN (1,2,3)) AND B IS NULL) OR B IN (1,2,3)

基本上,对于不了解yii的人,我想将(a+b)(c+d)重写为((w ? x) ? y) ? z,这样可以不重复任何条件吗?或者我需要考虑替代方案吗?

我不明白你在重写 (a+b)(c+d)((w ? x) ? y) ? z 的问题,但是如果你想创建以下条件:

WHERE (A IS NULL OR A IN (1,2,3)) 
  AND (B IS NULL OR B IN (1,2,3))

然后只需使用此代码:

$a = [1,2,3];
$b = [1,2,3];

$criteria1 = new CDbCriteria;
$criteria1->addCondition('A IS NULL');
$criteria1->addInCondition('A', $a,'OR');

$criteria2 = new CDbCriteria;
$criteria2->addCondition('B IS NULL');
$criteria2->addInCondition('B', $b,'OR');

$criteria = new CDbCriteria();
$criteria->addCondition($criteria1->condition);
$criteria->addCondition($criteria2->condition);

$criteria->params = array_merge($criteria1->params,$criteria2->params);

结果条件通过

给出
echo $criteria->condition;

作为

((A IS NULL) OR (A IN (:ycp0, :ycp1, :ycp2))) 
AND ((B IS NULL) OR (B IN (:ycp3, :ycp4, :ycp5)))

ycp#$a$b中指定的参数化值。)