yii2 读取db操作db命令

yii2 读取db操作db命令



$connection = Yii::app()->db; //连接

插入数组形式
Yii::app()->dbName->createCommand()->insert('tbl_user',
    array(
        'username'=>'phpernote', 
        'password'=>'123456'
 )
);

本文用作工作记录,也许有人会问为什么不用 Yii 的 Model 去操作 DB,原因很简单,Yii 的 Model 写法上是方便了很多,但是会执行多余的 SQL,打开 Yii 的执行 log 就会发现。

打开跟踪log的方法,config/main.php中 log routes 中添加

[php]  view plain  copy
  1. [  
  2.     'class' => 'CWebLogRoute',  
  3. ]  

所以为了效率,为了 DB 服务器的性能考虑,还是使用 createCommand 的好。

insert

[php]  view plain  copy
  1. $row = Yii::app()->getDb()->createCommand()->insert('goods'array(  
  2.             'good_name' => $goods_name,  
  3.             'good_type' => $goods_type,  
  4.             'price' => $price,  
  5.             'buy_nums' => 0,  
  6.             'commit_nums' => 0,  
  7.             'create_time' => time(),  
  8.         ));  

select

单表查询

[php]  view plain  copy
  1. $goodsTypes = Yii::app()->getDb()->createCommand()  
  2.         ->select('type_id, type_name')  
  3.         ->from('goods_type')  
  4.         ->where('status=:status', [  
  5.             ':status' => 1  
  6.         ])  
  7.         ->queryAll();  


连表查询

[php]  view plain  copy
  1. $goods = Yii::app()->getDb()->createCommand()->from('goods g')  
  2.         ->select('g.good_id, g.good_name, gt.type_name, g.price, g.buy_nums, g.commit_nums, g.create_time')  
  3.         ->join('goods_type gt''g.good_type=gt.type_id')  
  4.         ->where('g.`status`=:status1 and gt.`status`=:status2', [  
  5.             ':status1' => 1,  
  6.             ':status2' => 2  
  7.         ])  
  8.         ->order('g.create_time desc')  
  9.         ->queryAll();  


delete

[php]  view plain  copy
  1. $row = Yii::app()->getDb()->createCommand()  
  2.         ->delete('goods'"good_id=:good_id"array(  
  3.             ':good_id' => $goods_id,  
  4.         ));  


update

[php]  view plain  copy
  1. $row = Yii::app()->getDb()->createCommand()->update('goods', [  
  2.     'good_name' => $goods_name,  
  3.     'good_type' => $goods_type,  
  4.     'price' => $price,  
  5.         ], "good_id=:good_id", [  
  6.     ':good_id' => 1  
  7.         ]);  


说明下,where 方法的使用方法很多,具体看 Yii 的代码注释,写的很详细。

就记录这点吧~

//查找 $sql = “SELECT * FROM `tbl_user` ORDER BY id DESC”; $command = $connection->createCommand($sql); $result = $command->queryAll(); print_r($result); //添加 $sql = ” INSERT INTO `tbl_user` (`username`, `password`, `email`) VALUES (‘test’, ‘test’, ‘test@test.com’) “; $command=$connection->createCommand($sql); print_r($command->execute()); //添加 返回自增id $command1 = $connection->createCommand(“SELECT last_insert_id()”); $result = $command1->queryAll(); //常用函数 (1)如果你执行的SQL语句有返回结果集: 例如SELECT。通常用query开头的系列函数: $dataReader=$command->query(); // 执行一个 SQL 查询 $rows=$command->queryAll(); // 查询并返回结果中的所有行 $row=$command->queryRow(); // 查询并返回结果中的第一行 $column=$command->queryColumn(); // 查询并返回结果中的第一列 $value=$command->queryScalar(); // 查询并返回结果中第一行的第一个字 (2)你执行的SQL语句返回的不是结果集,只是状态值,例如:INSERT ,UPDATE,DELETE.则用execute() $this->command->execute(); //使用事务的一种常见情形:CDbTransaction $transaction = $connection->beginTransaction(); try{ $connection->createCommand($sql1)->execute(); $connection->createCommand($sql2)->execute(); ……… $transaction->commit(); } catch(Exception $e){ // 如果有一条查询失败,则会抛出异常 $transaction->rollBack(); }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值