如何在 Yii 中为 IN() 子句创建参数化数据库更新语句? [英] How do I create a parameterized database update statement in Yii for an IN() clause?
本文介绍了如何在 Yii 中为 IN() 子句创建参数化数据库更新语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试过了
$sql = "update ad_group_keyword set status = :status where google_id not in (:google_id)";
Yii::$app->db->createCommand($sql)
->bindValue(':status', Constants::DELETED)
->bindValue(':google_id', join(',',$googleIds), \PDO::PARAM_INT)
->execute();
但它把 id 数组变成了一个巨大的字符串,尽管有 PDO::PARAM_INT
.我也试过
but it turned the array of ids into one giant string, despite the PDO::PARAM_INT
. I also tried
->bindValue(':google_id', $googleIds)
但它在 vendor/yiisoft/yii2/db/Command.php:172 中给出了一个 'Array to string conversion'.我最终使用了
but it gave an 'Array to string conversion' in vendor/yiisoft/yii2/db/Command.php:172
. I ended up using
$sql = "update ad_group_keyword set status = :status where google_id not in (" . join(',',$googleIds) . ")";
推荐答案
我建议使用 QueryBuilder 来实现这个功能:
I suggest use QueryBuilder for this function:
$command = Yii::$app->db->createCommand();
$result = $command->update( // create a update sql
'ad_group_keyword', // table
['status'=>1], // update set
['NOT IN', 'google_id', [1,2,3]] // where
)->execute();
您可以阅读\yii\db\Command::update() DOC,以及如何设置条件
这篇关于如何在 Yii 中为 IN() 子句创建参数化数据库更新语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文