Yii createCommand CURD操作
2015-10-08 15:22
609 查看
转自:http://blog.csdn.net/liruxing1715/article/details/42168953
有人会问为什么不用 Yii 的 Model 去操作 DB,原因很简单,Yii 的 Model 写法上是方便了很多,但是会执行多余的 SQL,打开 Yii 的执行 log 就会发现。
打开跟踪log的方法,config/main.php中 log routes 中添加
[
'class' => 'CWebLogRoute',
]
所以为了效率,为了 DB 服务器的性能考虑,还是使用 createCommand 的好。
insert
$row = Yii::app()->getDb()->createCommand()->insert('goods', array(
'good_name' => $goods_name,
'good_type' => $goods_type,
'price' => $price,
'buy_nums' => 0,
'commit_nums' => 0,
'create_time' => time(),
));
select
单表查询
$goodsTypes = Yii::app()->getDb()->createCommand()
->select('type_id, type_name')
->from('goods_type')
->where('status=:status', [
':status' => 1
])
->queryAll();
连表查询
$goods = Yii::app()->getDb()->createCommand()->from('goods g')
->select('g.good_id, g.good_name, gt.type_name, g.price, g.buy_nums, g.commit_nums, g.create_time')
->join('goods_type gt', 'g.good_type=gt.type_id')
->where('g.`status`=:status1 and gt.`status`=:status2', [
':status1' => 1,
':status2' => 2
])
->order('g.create_time desc')
->queryAll();
delete
$row = Yii::app()->getDb()->createCommand()
->delete('goods', "good_id=:good_id", array(
':good_id' => $goods_id,
));
update
$row = Yii::app()->getDb()->createCommand()->update('goods', [
'good_name' => $goods_name,
'good_type' => $goods_type,
'price' => $price,
], "good_id=:good_id", [
':good_id' => 1
]);
说明下,where 方法的使用方法很多,具体看 Yii 的代码注释,写的很详细。
就记录这点吧~
有人会问为什么不用 Yii 的 Model 去操作 DB,原因很简单,Yii 的 Model 写法上是方便了很多,但是会执行多余的 SQL,打开 Yii 的执行 log 就会发现。
打开跟踪log的方法,config/main.php中 log routes 中添加
[
'class' => 'CWebLogRoute',
]
所以为了效率,为了 DB 服务器的性能考虑,还是使用 createCommand 的好。
insert
$row = Yii::app()->getDb()->createCommand()->insert('goods', array(
'good_name' => $goods_name,
'good_type' => $goods_type,
'price' => $price,
'buy_nums' => 0,
'commit_nums' => 0,
'create_time' => time(),
));
select
单表查询
$goodsTypes = Yii::app()->getDb()->createCommand()
->select('type_id, type_name')
->from('goods_type')
->where('status=:status', [
':status' => 1
])
->queryAll();
连表查询
$goods = Yii::app()->getDb()->createCommand()->from('goods g')
->select('g.good_id, g.good_name, gt.type_name, g.price, g.buy_nums, g.commit_nums, g.create_time')
->join('goods_type gt', 'g.good_type=gt.type_id')
->where('g.`status`=:status1 and gt.`status`=:status2', [
':status1' => 1,
':status2' => 2
])
->order('g.create_time desc')
->queryAll();
delete
$row = Yii::app()->getDb()->createCommand()
->delete('goods', "good_id=:good_id", array(
':good_id' => $goods_id,
));
update
$row = Yii::app()->getDb()->createCommand()->update('goods', [
'good_name' => $goods_name,
'good_type' => $goods_type,
'price' => $price,
], "good_id=:good_id", [
':good_id' => 1
]);
说明下,where 方法的使用方法很多,具体看 Yii 的代码注释,写的很详细。
就记录这点吧~
相关文章推荐
- PHP echo 单引号与双引号的区别
- Yii查询生成器(Query Builder)用法实例教程
- ThinkPHP中使用ajaxReturn进行ajax交互
- PHP IP互换数字[转]
- PHP对大文件的处理思路
- phpstorm安装laravel-ide-helper实现自动完成、代码提示和跟踪
- php异步请求模拟多进程
- PHP将XML转换为数组
- ThinkPHP的field方法的用法总结
- PHP抽奖算法程序代码分享
- PHP 判断是否包含某字符串
- getAttribute和getParameter区别
- php视频拍照上传头像功能实现代码分享
- YII1升级到YII2的注意事项
- mac grep: /usr/include/php/main/php.h: No such file or directory
- PHP数组合并函数(+)、array_merge、array_merge_recursive
- cookie与session(php)
- Cannot find autoconf. Please check your autoconf installation and the $PHP_AUTOCONF environment vari
- 关于VISUAL STUDIO 14.0 (CTP6) 的编译与链接的问题
- php 常用函数