首页 > 网站 > WEB开发 > 正文

yii简单增删改查操作

2024-04-27 15:15:38
字体:
来源:转载
供稿:网友
//①连接数据库连接//②创建model层
<?phpnamespace app/models;use yii/db/ActiveRecord;class Motto extends ActiveRecord{}?>//③控制器调用model层进行CURD操作 
<?php/** * Created by PhpStorm. * User: crazy stone * Date: 2017/2/8 * Time: 16:07 */namespace frontend/controllers;  //命名空间use Yii;        //使用yii核心类库use yii/web/Controller;   ///使用yii核心类库里的 controlleruse app/models/Motto;class HelloController extends Controller{         //查看    public function actionList(){        $sql='select m_id,m_name from motto ';        $data=Motto::findBySql($sql)->asArray()->all();//        PRint_r($data);die;        return $this->renderPartial('list',$data);//赋值给页面    }    //删除    public function actionDel(){        //方法1        $res=Motto::find()->where(['m_id'=>5])->all();        $res[0]->delete();        //方法2        Motto::deleteAll('m_id>3');    }    //添加    public function actionAdd(){        $motto=new Motto;        $motto->m_name='啊啊啊';        $motto->type_id=16;        $motto->l_id=1;        $motto->m_add_time=time();        $motto->hitts=2;        $motto->author=2;        $motto->save();    }    //修改    public function actionSave(){        $res=Motto::find()->where(['m_id'=>3])->one();        $res->m_name='石文远';        $res->save();    }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表