小巫师 2018-04-03 18:37:14 6852次浏览 1条评论 1 3 0

model层 建立一个文件是Article.php

<?php
/*

小巫师专属编辑
IT的时代领航 | come on
*/
namespace frontend\models;

use yii\db\ActiveRecord;

class Article extends ActiveRecord
{

public static function tableName() {
    return 'user';
}
}

控制器层 建立ArticleController.php

<?php
/*

小巫师专属编辑
IT的时代领航 | come on
*/
namespace frontend\controllers;

use yii\web\Controller;
use frontend\models\Article;

class ArticleController extends Controller
{

//增加
    public function actionAdd(){
        $model = new Article();
        $model->id = 3;
        $model->username = 'zhang';
        $model->save();
    }
//删除  删除首先要查询
    public function actionDel(){
        $param = Article::findOne(2);
// var_dump($param);die;

        $param->delete();
    } 
    
//查询
    public function actionSel(){
        $data = Article::find()
                ->where(['id'=>1])
                ->asArray()
                ->one();    
    }

//修改
    public function actionUpd(){
        $param = Article::findOne(1);
// var_dump($param);die;

        $param->id = 1;
        $param->username= '老乡吃不上饭';
        $param->save();
    }
}

新手如果有不懂的地方,可以私信扣我,我及时向你讲解

觉得很赞
您需要登录后才可以评论。登录 | 立即注册