个人笔记 [ 新手入门 ]
创建表单小部件Model
<?php
/**
* Created by PhpStorm.
* User: lx
* Date: 2017/5/23
* Time: 22:30
*/
namespace backend\models;
use yii\db\ActiveRecord;
class Exam extends ActiveRecord
{
//定义字段名
public $news_id;
public $news_name;
public $news_content;
public $news_classify;
//验证
public function rules(){
return[
];
}
//给字段起别名
public function attributeLabels(){
return[
'news_name' => '新闻名称',
'news_content' => '新闻内容',
'news_classify' => '新闻分类',
'news_id'=>'',
];
}
//数据库查询数据循环-返回结果
public static function classify_data($arr){
$data = [];
foreach ($arr as $key=>$val){
$data[$val['news_id']]=$val['classify_name'];
}
return $data;
}
}
AR表1主表 Model News
<?php
namespace backend\models;
use yii\db\ActiveRecord;
class Exam extends ActiveRecord
{
//定义字段名
public $news_id;
public $news_name;
public $news_content;
public $news_classify;
//验证
public function rules(){
return[
];
}
//给字段起别名
public function attributeLabels(){
return[
'news_name' => '新闻名称',
'news_content' => '新闻内容',
'news_classify' => '新闻分类',
'news_id'=>'',
];
}
//数据库查询数据循环-返回结果
public static function classify_data($arr){
$data = [];
foreach ($arr as $key=>$val){
$data[$val['news_id']]=$val['classify_name'];
}
return $data;
}
}
再创一个关联的Model Classify
<?php
namespace backend\models;
use yii\db\ActiveRecord;
class Classify extends ActiveRecord
{
public static function tableName(){
return 'news_classify';
}
}
//控制器的实际增删改查
<?php
/**
* Created by PhpStorm.
* User: lx
* Date: 2017/5/23
* Time: 22:29
*/
namespace backend\controllers;
use yii\web\Controller;
use backend\models\Exam;
use backend\models\News;
use backend\models\Classify;
use yii;
class ExamController extends Controller{
//是否去除框架默认样式
//public $layout = false;
//加载默认方法
public $defaultAction = 'index';
public function p ($data){
echo "<pre>";
print_r($data);die;
}
//添加表单首页
public function actionIndex(){
$result = Classify::find() ->asArray()->all();
$data = Exam::classify_data($result);
//$this->p($data);
$model = new Exam();
return $this->render('index',['data'=>$data,'model'=>$model]);
}
//添加方法
public function actionAdd(){
//获取表单所有数据
$data = yii::$app->request->post();
//$this->p($data);
//分别接受每一个值
$news_name = $data['Exam']['news_name'];
$news_content = $data['Exam']['news_content'];
$news_classify = $data['Exam']['news_classify'];
//加载model--执行添加
$model = new News();
$model->news_name = $news_name;
$model->news_content = $news_content;
$model->news_classify = $news_classify;
$model->save();
return $this->redirect(['exam/list']);
}
//展示数据
public function actionList(){
//两表联查
$result = News::find()
->joinWith('classify')
->select('news.*,news_classify.classify_name')
->asArray()
->all();
//$data = Exam::find()->asArray()->all();
return $this->render('list',['data'=>$result]);
}
//删除
public function actionDel(){
$news_id = yii::$app->request->get('news_id');
$model = News::findOne($news_id);
//$this->p($model);die;
if($model !== null && !$model->delete()) {
echo 1;die;
}else{
return $this->redirect(['exam/list']);
}
}
//修改
public function actionSave(){
//接收超链接传过来的ID
$news_id = yii::$app->request->get('news_id');
//根据ID查出数据
$arr = News::find()->where(['news_id'=>$news_id])->asArray()->one();
//$this->p($arr);
//关联分类表
$result = Classify::find() ->asArray()->all();
$data = Exam::classify_data($result);
$model = new Exam();
//渲染页面
return $this->render('save',['data'=>$data,'model'=>$model,'arr'=>$arr]);
}
//执行修改
public function actionSave_do(){
$data = yii::$app->request->post();
//查询出一条,id为隐藏域传过来的数据--执行修改
$model = News::findOne($data['Exam']['news_id']);
$model->news_name = $data['Exam']['news_name'];
$model->news_content = $data['Exam']['news_content'];
$model->news_classify = $data['Exam']['news_classify'];
$model->save();
return $this->redirect(['exam/list']);
}
}
//视图层 //添加表单页面
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<center>
<a href="?r=exam/list">点我飞往展示页面</a>
<?php
/**
* Created by PhpStorm.
* User: lx
* Date: 2017/5/23
* Time: 23:37
*/
use yii\helpers\Html;
use yii\widgets\ActiveForm;
$form = ActiveForm::begin([
'id' => 'exam-form',
'options' => ['class' => 'form-horizontal','enctype'=>'multipart/form-data'],
'action' =>'?r=exam/add',
'method' =>'post',
]);
?>
<?= $form->field($model, 'news_name') ?>
<?= $form->field($model, 'news_content') ?>
<?= $form->field($model, 'news_classify')->dropDownList($data,['prompt' => $data[1]]) ?><!--prompt下拉菜单默认选中-->
<div class=”form-group”>
<div class=”col-lg-offset-1 col-lg-11″>
<?= Html::submitButton('添加', ['class' => 'btn btn-primary']) ?>
</div>
</div>
<?php ActiveForm::end() ?>
</center>
</body>
</html>
//修改页面
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<center>
<a href="?r=exam/list">点我飞往展示页面</a>
<?php
/**
* Created by PhpStorm.
* User: lx
* Date: 2017/5/23
* Time: 23:37
*/
use yii\helpers\Html;
use yii\widgets\ActiveForm;
$form = ActiveForm::begin([
'id' => 'exam-form',
'options' => ['class' => 'form-horizontal','enctype'=>'multipart/form-data'],
'action' =>'?r=exam/save_do',
'method' =>'post',
]);
?>
<?= $form->field($model, 'news_name')->textInput(['value'=>$arr['news_name']]) ?>
<?= $form->field($model, 'news_content')->textInput(['value'=>$arr['news_content']]) ?>
<?= $form->field($model, 'news_classify')->dropDownList($data,['prompt' => $data[1]]) ?><!--prompt下拉菜单默认选中-->
<?= $form->field($model, 'news_id')->hiddenInput(['value'=>$arr['news_id']])?>
<div class=”form-group”>
<div class=”col-lg-offset-1 col-lg-11″>
<?= Html::submitButton('修改', ['class' => 'btn btn-primary']) ?>
</div>
</div>
<?php ActiveForm::end() ?>
</center>
</body>
</html>
共 0 条回复
没有找到数据。
海海丫 Beijing
注册时间:2017-05-09
最后登录:2019-01-18
在线时长:8小时48分
最后登录:2019-01-18
在线时长:8小时48分
- 粉丝6
- 金钱520
- 威望40
- 积分1000