aiwo3336 2017-05-31 19:20:18 3417次浏览 2条评论 0 0 0

控制器层

<?php


namespace frontend\controllers;

use yii\web\Controller;
use yii;
use db;
use frontend\models\Classify;
use frontend\models\Novels;
class IndexController extends Controller
{

    public function actionIndex()
    {
        $model = new novels();
        $models = new classify();
        $da = Classify::find()->asArray()->all();
        $arry = array();
        foreach ($da as $v) {
            $arry[$v['classify_id']] = $v['classify_name'];
        };
        return $this->render('index', ['model' => $model, 'models' => $models, 'arry' => $arry]);
    }

    public function actionInsert()
    {
        $data = Yii::$app->request->post('Novels');
        $datas = Yii::$app->request->post('Classify');
        $model = new Novels();
        $model->classify_id = $datas['classify_name'];
        $model->novel_name = $data['novel_name'];
        $model->novel_author = $data['novel_author'];
        $model->novel_content = $data['novel_content'];
        $model->novel_time = $data['novel_time'];
        $model->novel_author_content = $data['novel_author_content'];
        $aa = $model->save();
        if ($aa) {
            return $this->redirect(['index/select']);
        } else {
            echo "添加失败";
        }
    }

    public function actionSelect()
    {
        $data = Novels::find()->joinWith(['classify'])->select('novels.*,classify.*')->limit(8)->asArray()->all();
        return $this->render('show', ['data' => $data]);
    }

    public function actionDelete()
    {
        $id = Yii::$app->request->get('id');
        $info = Novels::findOne($id)->delete();
        if ($info) {
            return $this->redirect(['index/select']);
        } else {
            echo "删除失败";
        }
    }

    public function  actionUpda()
    {
        $model = new Novels();
        $models = new classify();
        $id = yii::$app->request->get('id');
        $data = Novels::find()->joinWith(['classify'])->where(['novel_id' => $id])->select('novels.*,classify.*')->asArray()->one();
        $da = Classify::find()->asArray()->all();
        $arry = array();
        foreach ($da as $v) {
            $arry[$v['classify_id']] = $v['classify_name'];
        };
        return $this->render('moren', ['model' => $model, 'models' => $models, 'data' => $data, 'arry' => $arry]);
    }

    public function  actionUpdate()
    {
        $data = yii::$app->request->post('Novels');
        $datas = Yii::$app->request->post('Classify');
        $exam = Novels::findOne($data['novel_id']);
        $exam->classify_id=$datas['classify_name'];
        $exam->novel_name = $data['novel_name'];
        $exam->novel_author = $data['novel_author'];
        $exam->novel_content = $data['novel_content'];
        $exam->novel_time = $data['novel_time'];
        $exam->novel_author_content = $data['novel_author_content'];
        $aa = $exam->save();
        if ($aa) {
            return $this->redirect(['index/select']);
        } else {
            echo "修改失败";
        }

    }
}

模型层Classify

<?php


namespace frontend\models;

use yii\db\ActiveRecord;


class Classify extends ActiveRecord {

    public  static function tableName(){
        return 'classify';
    }
    public  function rules(){
        return[

        ];
    }
    public  function  attributeLabels(){
        return [
          'classify_name'=>'分类名称',
            'classify_content'=>'分类简介',
        ];
    }

}

models

<?php
/**
 * Created by PhpStorm.
 * User: 工作
 * Date: 2017/5/31
 * Time: 10:14
 */

namespace frontend\models;
use yii\db\ActiveRecord;

class Novels extends  ActiveRecord {
    public  static function tableName(){
        return 'novels';
    }
    //关联表      get(关联表Model名)
    public function getClassify(){
    return $this->hasOne(Classify::className(),['classify_id'=>'classify_id']);
}
    public  function rules(){
        return[

        ];
    }
    public  function  attributeLabels(){
        return [
            'novel_name'=>'小说名称',
            'novel_author'=>'小说作者',
            'novel_content'=>'小说介绍',
            'novel_author_content'=>'作者介绍',
            'novel_time'=>'上传时间'
        ];
    }
}

view
index.php

<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;

$form = ActiveForm::begin([
    'id' => 'login-form',
    'options' => ['class' => 'form-horizontal'],
    'action'=>'?r=index/insert',
    'method'=>'post'
]) ?>
<?= $form->field($models, 'classify_name')->dropDownList($arry,['style'=>'width:300px']) ?>
<?= $form->field($model, 'novel_name')->textInput(['style'=>'width:300px']) ?>
<?= $form->field($model, 'novel_author')->textInput(['style'=>'width:300px']) ?>
<?= $form->field($model, 'novel_content')->textarea(['style'=>'width:300px']) ?>
<?= $form->field($model, 'novel_time')->textInput(['style'=>'width:300px']) ?>
<?= $form->field($model, 'novel_author_content')->textarea(['style'=>'width:300px']) ?>

    <div class="form-group">
        <div class="col-lg-offset-1 col-lg-11">
            <?= Html::submitButton('Login', ['class' => 'btn btn-primary']) ?>
        </div>
    </div>
<?php ActiveForm::end() ?>

moren.php

<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;

$form = ActiveForm::begin([
    'id' => 'login-form',
    'options' => ['class' => 'form-horizontal'],
    'action'=>'?r=index/update',
    'method'=>'post'
]) ?>
<?= Html::activeHiddenInput($model,'novel_id',array('value'=>$data['novel_id'] )) ?>
<?php $models->classify_name=$data['classify_name']   ?>
<?= $form->field($models, 'classify_name')->dropDownList($arry,['style'=>'width:300px']) ?>
<?php $model->novel_name=$data['novel_name']   ?>
<?= $form->field($model, 'novel_name')->textInput(['style'=>'width:300px']) ?>
<?php $model->novel_author=$data['novel_author']   ?>
<?= $form->field($model, 'novel_author')->textInput(['style'=>'width:300px']) ?>
<?php $model->novel_content=$data['novel_content']   ?>
<?= $form->field($model, 'novel_content')->textarea(['style'=>'width:300px']) ?>
<?php $model->novel_time=$data['novel_time']   ?>
<?= $form->field($model, 'novel_time')->textInput(['style'=>'width:300px']) ?>
<?php $model->novel_author_content=$data['novel_author_content']   ?>
<?= $form->field($model, 'novel_author_content')->textarea(['style'=>'width:300px']) ?>

    <div class="form-group">
        <div class="col-lg-offset-1 col-lg-11">
            <?= Html::submitButton('Login', ['class' => 'btn btn-primary']) ?>
        </div>
    </div>
<?php ActiveForm::end() ?>

show.php

<table border="1">
    <tr>
        <td>小说ID</td>
        <td>分类名称</td>
        <td>小说名称</td>
        <td>小说作者</td>
        <td>小说介绍</td>
        <td>作者介绍</td>
        <td>上传时间</td>
        <td>操作</td>
    </tr>
    <?php foreach($data as $v){ ?>
    <tr>
        <td><?php echo $v['novel_id'] ?></td>
        <td><?php echo $v['classify_name'] ?></td>
        <td><?php echo $v['novel_name'] ?></td>
        <td><?php echo $v['novel_author'] ?></td>
        <td><?php echo $v['novel_content'] ?></td>
        <td><?php echo $v['novel_author_content'] ?></td>
        <td><?php echo $v['novel_time'] ?></td>
        <td><a href="?r=index/delete&id=<?php echo $v['novel_id'] ?>">删除</a>|<a href="?r=index/upda&id=<?php echo $v['novel_id'] ?>">修改</a></td>
    </tr>
    <?php } ?>
</table>

重要知识点 AR的两表联查

  • 评论于 2017-06-01 21:01 举报

    'prompt'=>$data['classify_name']
    如果下来默认不出来,可以加这个关键字prompt

  • 评论于 2017-05-31 19:27 举报

    Active Record (活动记录,以下简称AR)提供了一个面向对象的接口, 用以访问数据库中的数据。一个 AR 类关联一张数据表, 每个 AR 对象对应表中的一行,对象的属性(即 AR 的特性Attribute)映射到数据行的对应列。 一条活动记录(AR对象)对应数据表的一行,AR对象的属性则映射该行的相应列。 您可以直接以面向对象的方式来操纵数据表中的数据, 妈妈再不用担心我需要写原生 SQL 语句啦。

您需要登录后才可以评论。登录 | 立即注册