yii2使用Dao并对接受到的参数进行过滤

查询数据:

#$id为前台传过来的值,我们需要对他进行过滤转义
Yii::$app->db->createCommand("select * from user where id=:id",[':id'=>$id])->queryAll();

更新数据:

#$id为前台传过来的值,我们需要对他进行过滤转义
Yii::$app->db->createCommand()->update('user',['name'=>'内容'],'id=:id',[":id"=>$id])->execute();
#另一种写法,利用bindValue绑定参数:
$result = Yii::$app->db->createCommand()->update('product',['name'=>":name112"],"id=:id")->bindValues([':id'=>1])->execute();

删除数据:

$result = Yii::$app->db->createCommand()->delete('product','id=:id',[':id'=>$id])->execute();

可以参考源码:yii2\db\Command;

你可能感兴趣的:(yii笔记)