1. 程式人生 > >yii連線資料庫新增資料--demo

yii連線資料庫新增資料--demo

yii連線資料庫新增資料--demo

<?php
/**
 * Created by PhpStorm.
 * User: kkk
 * Date: 2018/4/1
 * Time: 21:11
 */

namespace app\modules\index\models;
use yii\db;
use yii;
use yii\base\Model;

class MsgModel extends Model
{
    public function doctor_info($data){
        $db = Yii::$app->db;
        $transaction = $db->beginTransaction();
        try {
            $res = $db->createCommand()->batchInsert(
                'yii_release',//這是表名
                ['name','doc_tel','doc_sex','doc_hos','doc_admini','doc_job'],//欄位
                [[$data['name'],$data["tel"],$data["sex"],$data["hos"],$data["admin"],$data["job"]]]//陣列,如果單獨有一組資料,必須在一組資料外層巢狀一個[]
            )->execute();
            $transaction->commit();
            return $res;
        } catch(\Exception $e) {
            $transaction->rollBack();
            throw $e;
        }
    }
}