Home >Backend Development >PHP Tutorial >Yii createCommand CURD operation

Yii createCommand CURD operation

WBOY
WBOYOriginal
2016-08-08 09:32:05926browse

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(),
        ));

select

Single table query

$goodsTypes = Yii::app()->getDb()->createCommand()
            ->select('type_id, type_name')
            ->from('goods_type')
            ->where('status=1')->queryAll();

Connected table query

$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();

delete

$row = Yii::app()->getDb()->createCommand()
        ->delete('goods', "good_id='{$goods_id}'");

update

$row = Yii::app()->getDb()->createCommand()->update('goods', array(
    'good_name' => $goods_name,
    'good_type' => $goods_type,
    'price' => $price,
), "good_id='{$goods_id}'");

Explain , There are many ways to use the where method. For details, see Yii's code comments, which are written in great detail.

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.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn