ActiveRecord 的 CURD(表单小部件) [ 2.0 版本 ]
<?php
namespace frontend\models;
use yii\db\ActiveRecord;
class Zhoukao1 extends ActiveRecord {
public static function tableName(){
return 'zhoukao1';
}
public function rules(){
return[
];
}
public function attributeLabels(){
return [
'username'=>'用户名'
];
}
}
控制器层
<?php
namespace frontend\controllers;
use yii\web\Controller;
use frontend\models\Zhoukao1;
use yii;
use db;
class YueController extends Controller{
public function actionIndex(){
$model=new Zhoukao1();
return $this->render('index',['model'=>$model]);
}
public function actionInsert(){
$data=yii::$app->request->post('Zhoukao1');
$aa=$data['username'];
$model=new Zhoukao1();
$model->username=$aa;
$aa= $model->save();
if ($aa) {
return $this->redirect(['yue/select']);
} else {
echo "添加失败";
}
}
public function actionSelect(){
//$data = Zhoukao1::find()->where(['id'=>1])->asArray()->one();
$data = Zhoukao1::find()->asArray()->all();
return $this->render('show',['data'=>$data]);
}
public function actionDelete(){
$id= yii::$app->request->get('id');
$exam = Zhoukao1::findOne($id);
$aa= $exam->delete();
if ($aa) {
return $this->redirect(['yue/select']);
} else {
echo "删除失败";
}
}
public function actionUpda(){
$model=new Zhoukao1();
$id= yii::$app->request->get('id');
$da = Zhoukao1::find()->where(['id'=>$id])->asArray()->one();
$data=$da['username'];
$ids=$da['id'];
return $this->render('moren',['data'=>$data,'model'=>$model,'ids'=>$ids]);
}
public function actionUpdate()
{
$data=yii::$app->request->post('Zhoukao1');
$exam = Zhoukao1::findOne($data['id']);
$exam->username = $data['username'];
$aa = $exam->save();
if ($aa) {
return $this->redirect(['yue/select']);
} else {
echo "修改失败";
}
}
}
view层
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
$form = ActiveForm::begin([
'id' => 'login-form',
'options' => ['class' => 'form-horizontal'],
'action'=>'?r=yue/update',
'method'=>'post'
]) ?>
<?php $model->username = $data; ?>
<?= Html::activeHiddenInput($model,'id',array('value'=> $ids)) ?>
<?= $form->field($model, 'username')->textInput(['style'=>'width:400px']) ?>
<div class="form-group">
<div class="col-lg-offset-1 col-lg-11">
<?= Html::submitButton('提交', ['class' => 'btn btn-primary']) ?>
</div>
</div>
<?php ActiveForm::end() ?>
aiwo3336
注册时间:2017-05-08
最后登录:2017-06-13
在线时长:7小时12分
最后登录:2017-06-13
在线时长:7小时12分
- 粉丝5
- 金钱360
- 威望20
- 积分630
共 0 条评论