ar两表 [ 新手入门 ]
第一个model <?php namespace frontend\models;
use yii\db\ActiveRecord;
class News extends ActiveRecord {
/**
* @return string 返回该AR类关联的数据表名
*/
public static function tableName()
{
return 'news';
}
//关联
public function getNews()
{
return $this->hasOne(Type::className(), ['id' => 't_id']);
}
} ?> 第二个model <?php namespace frontend\models;
use yii\db\ActiveRecord;
class Type extends ActiveRecord {
/**
* @return string 返回该AR类关联的数据表名
*/
public static function tableName()
{
return 'type';
}
} ?> 控制器 <?php namespace frontend\controllers;
use Yii; use db; use yii\web\Controller; use frontend\models\Type; use frontend\models\Form; use frontend\models\News; /**
ar练习 */ class TestController extends Controller { //添加类型 public function actionType_add() {
$form=new Form(); if(!yii::$app->request->post()){ return $this->render('type_add',['model'=>$form]); }else{ $type=new Type; $data=yii::$app->request->post('Form'); $type->type=$data['type']; if($type->save()){ return $this->redirect('?r=test/type_list'); } }
} //类型展示 public function actionType_list() {
$type=new Type; $typeInfo=$type->find()->asArray()->all(); return $this->render('type_list',['type'=>$typeInfo]);
} //类型删除 public function actionType_del() {
$id=yii::$app->request->get('id'); $type=new Type; $type = $type::findOne($id); if($type->delete()){ return $this->redirect('?r=test/type_list'); }
} //类型修改 public function actionType_update() {
$type=new Type; if(!yii::$app->request->post()){ $id=yii::$app->request->get('id'); $type = $type->find()->where(['id'=>$id])->asArray()->one(); $form=new Form(); return $this->render('type_update',['model'=>$form,'type'=>$type]); }else{ $data=yii::$app->request->post('Form'); $type = $type::findOne($data['id']); $type->type=$data['type']; if($type->save()){ return $this->redirect('?r=test/type_list'); } }
} //添加新闻 public function actionNews_add() {
$form=new Form; if(!yii::$app->request->post()){ $type=new Type; $typeInfo=$type->find()->asArray()->all(); $type=array(); foreach($typeInfo as $key=>$val){ $type[$val['id']]=$val['type']; } return $this->render('news_add',['model'=>$form,'type'=>$type]); }else{ $data=yii::$app->request->post('Form'); $news=new News(); $news->title=$data['title']; $news->content=$data['content']; $news->t_id=$data['t_id']; if($news->save()){ return $this->redirect('?r=test/news_show'); } }
}
//新闻展示 public function actionNews_show() {
$news=new News(); $news_info=$news::find()->innerJoinWith('news')->asArray()->all(); return $this->render("news_show",['news_info'=>$news_info]);
} //新闻删除 public function actionNews_del() {
$id=yii::$app->request->get('id'); $news=new News; $news = $news::findOne($id); if($news->delete()){ return $this->redirect('?r=test/news_show'); }
} //新闻修改 public function actionNews_update() {
$form=new Form; $news=new News; $type=new Type; if(!yii::$app->request->post()){ $id=yii::$app->request->get('id'); $news=$news->find()->where(['id'=>$id])->asArray()->one(); $t_id=$news['t_id']; $type=$type->find()->asArray()->all(); foreach($type as $key=>$val){ if($val['id']==$t_id){ array_unshift($type,$val); unset($type[$key+1]); } } $types=array(); foreach($type as $key=>$val){ $types[$val['id']]=$val['type']; } return $this->render('news_update',['model'=>$form,'news'=>$news,'types'=>$types]); }else{ $data=yii::$app->request->post('Form'); $news = $news::findOne($data['id']); $news->title=$data['title']; $news->content=$data['content']; $news->t_id=$data['t_id']; if($news->save()){ return $this->redirect('?r=test/news_show'); } }
} } 表单 <?php use yii\helpers\Html; use yii\widgets\ActiveForm; ?> <?php $form = ActiveForm::begin([ 'action' =>'?r=test/news_add', 'method' => 'post', ]); ?> <?= $form->field($model, 'title')->textInput(['style'=>'width:250px']) ?> <?= $form->field($model, 'content')->textarea(['rows'=>3,'style'=>'width:250px']) ?> <?= $form->field($model, 't_id')->dropDownList($type, ['prompt'=>'请选择','style'=>'width:120px'])->label('新闻分类') ?>
<?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?> <?= Html::resetButton('reset', ['class'=>'btn btn-primary','name' =>'submit-button']) ?>
<?php foreach($news_info as $key=>$val){?><td width="15%" align="center">id</td> <td width="15%" align="center">类型</td> <td width="15%" align="center">标题</td> <td width="15%" align="center">内容</td>
<?php }?><td align="center"><?php echo $val['id']?></td> <td align="center"><?php echo $val['news']['type']?></td> <td align="center"><?php echo $val['title']?></td> <td align="center"><?php echo $val['content']?></td> <td align="center"><a href="?r=test/news_del&id=<?php echo $val['id']?>">删除</a> | <a href="?r=test/news_update&id=<?php echo $val['id']?>">修改</a></td>
共 0 条回复
暮澜寒冬
最后登录:2018-10-11
在线时长:10小时2分
- 粉丝7
- 金钱310
- 威望10
- 积分510