Yii createCommand CURD操作+个人总结
来源:互联网 发布:左程云 算法视频 下载 编辑:程序博客网 时间:2024/06/06 15:51
打开跟踪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(), ));
单表查询
$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 ]);当然这只是一部分。
个人总结:
封装类,进行sql语句的 更新(增,删,改)和查询
在models文件夹中创建类
更新
public function upd($_sql){$connection = Yii::app()->db;$result = $connection->createCommand($_sql)->execute();return $result;}
查询
public function sel($_sql){$connection = Yii::app()->db;$command = $connection->createCommand($_sql);$result = $command->queryAll();return $result;}
计数
public function getCount($_sql){$result = yii::app()->db->createCommand($sql)->query()->rowCount;return $result;}
当然了,正如我们所期待的,他们都具有返回值。成功返回1,失败返回0
0 0
- Yii createCommand CURD操作+个人总结
- Yii createCommand CURD操作
- Yii createCommand CURD操作
- Yii createCommand CURD操作
- Yii createCommand CURD操作
- Yii createCommand CURD操作
- Yii createCommand CURD操作
- yii-数据库CURD操作
- Yii CURD 基本操作
- (九)yii 数据库操作curd
- Yii的createCommand用法
- Yii的createCommand用法
- yii curd
- 二:使用yii框架建立应用(数据库CURD基本操作)
- 使用yii AR 完成单个表的CURD操作
- yii的CURD(创建、修改、读取、删除)操作
- 使用yii AR 完成单个表的CURD操作
- Yii2.0数据操作【使用createCommand()函数】
- String ,Stringbuffer ,StringBuilder;
- 用JAVA进行图像识别的小程序
- 面试1-HTTP状态码
- At91sam9g35K烧录Linux系统
- Qml文件的两种加载方式|启动Qt quick app的两种方法
- Yii createCommand CURD操作+个人总结
- 学习python2.7的迭代器
- UIPickerView
- 设计模式_观察者模式
- a:link,a:visited,a:hover,a:active 区别
- codeforces--651B Beautiful Paintings
- Linux平台weblogic集群安装(一)
- HTML学习笔记(一)
- 我的Android进阶之旅------>Android自定义View实现带数字的进度条(NumberProgressBar)