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

Yii createCommand CURD operation

WBOY
Release: 2016-08-08 09:32:05
Original
914 people have browsed it

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

select

Single table query

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

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();
Copy after login

delete

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

update

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

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.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template