简体   繁体   English

Codeigniter 4 查询 IF ISNULL

[英]Codeigniter 4 query IF ISNULL

I am migragint from Codeigniter 3 to Codeigniter 4 and the next query is problematic to me.我从 Codeigniter 3 迁移到 Codeigniter 4 并且下一个查询对我来说是有问题的。

In Codeigniter 3, the query was:在 Codeigniter 3 中,查询为:

$this->db->query("UPDATE myTable SET data = '$data', 
                                myData = IF ( ISNULL(myData), '$state', CONCAT('$state', '-', myData) ) 
                        WHERE id = '$id'");

I tried to change this with the next format:我试图用下一种格式改变它:

$data = array(
            'myData' => "IF( ISNULL(myData), " . $state. ", CONCAT(" . $state. ", '-', myData')")
        );

However, the function updates the content of $myData like a string "IF( ISNULL(myData), ". stateOfExample. ", CONCAT(". stateOfExample. ", '-', myData')".但是,function 会更新 $myData 的内容,如字符串“IF( ISNULL(myData), ".stateOfExample.", CONCAT(".stateOfExample.", '-', myData')"。

I have looked into the CI4 documentation but i have not find the way to develop this query.我查看了 CI4 文档,但我还没有找到开发此查询的方法。 I find the way to do that with the where() CI function, but i think is not the best way to do that.我找到了使用 where() CI function 的方法,但我认为这不是最好的方法。

Thanks in advance提前致谢

If you put SQL functions in data, codeigniter won't understand that those are functions.如果您将 SQL 函数放入数据中,codeigniter 将无法理解这些是函数。 It just adds them as string.它只是将它们添加为字符串。

Put IF , CONCAT , ISNULL in sql and use data array just for variables.IFCONCATISNULL放入 sql 并仅将数据数组用于变量。

$this->db->query("UPDATE myTable SET data = ?, 
    myData = IF ( ISNULL(myData), ?, CONCAT(?, '-', myData) ) 
    WHERE id = ?", array($data, $state, $state, $id));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM