首先
<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;
use app\models\test;//数据模型的命名空间
use app\models\reg;
use app\models\flower;
class FlowerController extends Controller
{
/**
* 添加页面
*/
public function actionGrass()
{
$model=new Flower;
if(yii::$app->request->isPost)
{
//获取用户输入的值
$data=\yii::$app->request->post();
//保存数据
if($model->load($data) && $model->save())
{
$this->redirect(['show']);
}
}
else
{
return $this->render('grass',['model'=>$model]);
}
}
/**
* 展示页面
*/
public function actionShow()
{
$model=new Flower;
$data=$model->find()->asArray()->all();
return $this->render('show',['data'=>$data,]);
}
/**
* 删除某条数据
*/
public function actionDelete()
{
// $model=new Flower;
$id=\yii::$app->request->get('id');
$results=Flower::findOne($id);
$results->delete();
return $this->redirect(['show']);
}
/**
* 查找某条数据
*/
public function actionSel()
{
$model=new Flower;
$id=\yii::$app->request->get('id');
$data = Flower::find()->where(['id'=>$id])->asArray()->one();
return $this->render('sel',['data'=>$data,'model'=>$model]);
}
/**
* 修改某条数据
*/
public function actionUpd()
{
$model=new Flower;
$data = \yii::$app->request->post();
$id=$data['Flower']['id'];
$model = Flower::findOne(['id'=>$id]);
//保存数据
if($model->load($data) && $model->save())
{
return $this->redirect(['show']);
}else{
alert('修改失败');
}
}
}
?>
Yii框架写增删改查 AR模式
最新推荐文章于 2023-08-12 10:07:57 发布