Yii2数据库操作

1、使用gii创建user model 和 user controller

<?php


namespace frontend\controllers;
use app\models\User;
use yii\data\Pagination;




class MsgController extends \yii\web\Controller
{
public function actionIndex()
{
$query = User::find();
$countQuery = clone $query;
$pages = new Pagination(['totalCount' => $countQuery->count(),'pagesize'=>'10']);//pagesize每页显示条数
$models = $query->offset($pages->offset)->limit($pages->limit)->All();
return $this->render('index', ['models' => $models,'pages' => $pages]);
}


public function actionAdd(){
echo 'ff';
$sql = "insert into user (username) values ('ddd')";
$bool = \Yii::$app->db->createCommand($sql)->execute();
//return $this->render('add');
}


public function actionDel(){
$conn = \Yii::$app->db;
$id = \Yii::$app->request->get('id');
$command = $conn->createCommand("delete from user where id='$id'");
$command->execute();
}

public function actionUpdate(){

$connection = \Yii::$app->db;
$db=\Yii::$app->db->createCommand();
$res=$connection->createCommand()->update('user',['username' => '999','email' => '888'], ['id' => 561])->execute();
//最后一个是条件 $this->redirect('index.php?r=msg');//重定向
}

public function actionRead(){
$rows = (new \yii\db\Query())->select(['*'])->from('user')->all();
var_dump($rows);
}


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值