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