我必须升级 cakephp 的版本( x2 到 x3 ),我必须翻译这个可怕的 sql 表达式。我开始翻译,但每次都被阻止。
public function getDefautPPKLAC($nuit, $date_deb, $date_fin) {
return $this->query("SELECT d.cycle_id, d.code_element, d.date_debut, d.date_fin, (d.date_fin-d.date_debut) AS duree,
CASE WHEN d.code_defaut='PP_PPKLAC_T' THEN d.code_defaut ELSE 'PP_PPCLAC_T'END AS code_defaut,
CASE WHEN d.code_defaut<>'PP_PPKLAC_T' THEN d.code_defaut END AS origine,
CASE WHEN d.code_defaut<>'PP_PPKLAC_T' THEN (SELECT libelle FROM referentiel_traces WHERE code=d.code_defaut) END AS info
FROM vue_defauts as d
WHERE d.code_defaut IN ('DETEC_BDU', 'DETEC_DILH', 'DETEC_DILB','DISC_N1_BDU','DISC_N2_BDU','DISC_PIZZ_BDU','DISC_DILH','DISC_DILB','DISC_PPF','DISC_K123','DISC_K507','DIL_INHIB','PP_PPKLAC_T')
AND d.nuit = '" . $nuit . "'
AND d.date_debut BETWEEN '" . $date_deb . " 00:00:00' AND '" . $date_fin . " 23:59:59.999'
ORDER BY d.cycle_id, d.date_debut, d.code_element, d.code_defaut desc");
}
现在我试着这样做,但我很确定这是完全错误的。(没做完)
$this->loadModel('ReferentielTraces');
$query = $this->find();
$code_defaut = $query->newExpr()->addCase(
$query->newExpr->add(
['code_defaut'=>'PP_PPKLC_T'],
['code_defaut'=>'identifier','PP_PPCLAC_T'],
['string','string']
)
);
$origine = $query->newExpr()->addCase(
$query->newExpr->add(
['code_defaut <>'=>'PP_PPKLAC_T'],
['code_defaut'=>'identifier'],
['string']
)
);
$info = $query->newExpr()->addCase(
$query->newExpr->add(
['code_defaut <>'=>'PP_PPKLAC_T'],
[$this->ReferentielTraces->select(['libelle'])->where(['code'=>'code_defaut'])->first()] // wrong here
['string']
)
);
$listCodeDefaut = array('DETEC_BDU', 'DETEC_DILH', 'DETEC_DILB','DISC_N1_BDU','DISC_N2_BDU','DISC_PIZZ_BDU','DISC_DILH','DISC_DILB','DISC_PPF','DISC_K123','DISC_K507','DIL_INHIB','PP_PPKLAC_T');
$query->where(['code_defaut IN'=>$listCodeDefaut,'nuit'=>$nuit]); //between ?
$query->order(['cycle_id','date_debut','code_element','code_defaut'=>'DESC']);
你能帮我吗?