Home >Backend Development >PHP Tutorial >Yii createCommand CURD operation
This article is used as a work record. Some people may ask why not use Yii’s Model to operate DB. The reason is very simple. Yii’s Model is much more convenient in writing, but it will execute redundant SQL. Open Yii’s execution log and it will Discover. So for the sake of efficiency and the performance of the DB server, it is better to use 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(), ));
Single table query
$goodsTypes = Yii::app()->getDb()->createCommand() ->select('type_id, type_name') ->from('goods_type') ->where('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`=1 and gt.`status`=1') ->order('g.create_time desc') ->queryAll();
$row = Yii::app()->getDb()->createCommand() ->delete('goods', "good_id='{$goods_id}'");
$row = Yii::app()->getDb()->createCommand()->update('goods', array( 'good_name' => $goods_name, 'good_type' => $goods_type, 'price' => $price, ), "good_id='{$goods_id}'");
Just record this~
The above introduces the Yii createCommand CURD operation, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.