我在 Yii 中使用多个嵌套搜索条件来选择一个名为库的模型。条件如下:
$criteria = new CDbCriteria();
$criteria->select = 't.id, t.barcode_1_id, t.barcode_2_id, t.library_type_id, t.qc_pass';
$criteria->order = 'patient.id DESC, t.id DESC';
$criteria->together = true;
$with=array(
'isolates'=>array(
'select'=>false,
),
'samples'=>array(
'select'=>'samples.id, samples.sample_preservation_id, samples.tumor',
),
'patient'=>array(
'select'=>'patient.id',
),
'flowcells'=>array(
'select'=>'flowcells.id,flowcells.serial_id',
),
'libraryType'=>array(
'select'=>'libraryType.id,libraryType.short_name',
),
'samplePreservation'=>array(
'select'=>'samplePreservation.name',
),
'program'=>array(
'select'=>false,
'condition'=>'program.name NOT LIKE "%Lab%"',
),
'runs'=>array(
'select'=>'runs.id',
)
);
// by run ID
if (is_array($runs) && (! empty($runs))) {
$runCriteria = new CDbCriteria();
$runCriteria->addInCondition("runs.id", $runs, 'OR');
$criteria->mergeWith($runCriteria, $operator='OR');
$something=true;
}
// libraries without patients
if ( ($patients=='Unassigned') || (is_array($patients) && in_array('Unassigned',$patients)) ) {
$unassCriteria = new CDbCriteria();
$unassCriteria->addCondition('patient.id IS NULL');
$criteria->mergeWith($unassCriteria, $operator='OR');
$something=true;
}
if ( ($patients=='QC Pass') || (is_array($patients) && in_array('QC Pass',$patients)) ) {
$qcCriteria = new CDbCriteria();
$qcCriteria->addCondition('t.qc_pass = 1',$operator='AND');
$qcCriteria->addCondition('t.created BETWEEN CURDATE() - INTERVAL 30 DAY AND SYSDATE()','AND');
$criteria->mergeWith($qcCriteria, $operator='OR');
$something=true;
}
if ( ($patients=='Ready for Flowcell') || (is_array($patients) && in_array('Ready for Flowcell',$patients)) ) {
$readyCriteria = new CDbCriteria();
$readyCriteria->addCondition('flowcells.id IS NULL',$operator='AND');
$readyCriteria->addCondition('runs.current_pipeline_node_id = 7',$operator='AND');
$criteria->mergeWith($readyCriteria, $operator='OR');
$something=true;
}
if (is_array($patients) && (! empty($patients))) {
$ptArr= array_diff($patients, array('QC Pass','Unassigned','Ready for Flowcell'));
$ptCriteria = new CDbCriteria();
$ptCriteria->addInCondition("patient.id", $ptArr,$operator='OR');
if ($ptArr) $criteria->mergeWith($ptCriteria, $operator='OR');
$something=true;
}
if ($something) $librariesModel=Libraries::model()->with($with)->findAll($criteria);
然后我将这些结果格式化为一个视图。
问题是 Yii 忽略了我的 $operator='OR' 并且总是使用 AND 而不是 OR。这是它生成的 WHERE 子句:
(SELECT 和 JOIN 看起来不错)
...
WHERE
(
(
( (patient.id IS NULL) AND ( (t.qc_pass = 1) AND (t.created BETWEEN CURDATE() - INTERVAL 30 DAY AND SYSDATE()) ) )
AND ( (flowcells.id IS NULL) AND (runs.current_pipeline_node_id = 7) )
)
AND (patient.id IN (:ycp0, :ycp1))
)
AND (program.name NOT LIKE "%Lab%")
ORDER BY patient.id DESC, t.id DESC, inventory_id ASC
知道为什么 Yii 不让我使用“OR”合并这些标准吗?我的目标是有几个括号表达式,其中包含 AND,但不是排他的。mergeWith 方法应该使用 OR 而不是 AND 将这些条件添加到 WHERE 子句中。
谢谢!