mysql数据库操作问题咨询

文档问题 · alice · 于 3年前 发布 · 1113 次阅读

请问一下,fecmall中我看源代码都是直接操作model增删改查数据的,这样是比较好。 但是可以直接在service里拼接sql语句吗,更方便一些? 如果要直接写sql语句,有没有示例呀?请大大指点。

共收到 1 条回复
Fecmall#13年前 0 个赞

1.执行sql你搜索一下,yii2执行sql就可以了

2.给你个例子你参看一下吧: https://github.com/fecshop/yii2_fecshop/blob/439fd7b46775d68405c874aeeb1dea2e668e3cac/services/Order.php#L868

$sql    = 'update '.$this->_orderModel->tableName().' set version = version + 1  where increment_id = :increment_id';
$data   = [
       'increment_id'  => $increment_id,
];
$result     = $this->_orderModel->getDb()->createCommand($sql, $data)->execute();

3.对于变量一定不要用字符串拼接,尤其是request传递的参数,否则会有被sql注入的风险

添加回复 (需要登录)
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册
Your Site Analytics