AR 两表联查(初) [ 2.0 版本 ]
//根据分类查询文章
$type = Type::find()->where(['type'=>'美容'])->one();
$desc = $type->hasMany(Desc::className(),['type_id'=>'t_id'])->asArray()->all(); //也可以写到model层
//根据文章查询分类
$art = Desc::find()->where(['title'=>'血友病吧被卖时间'])->one();
$type = $art->hasOne(Type::className(),['t_id'=>'type_id'])->asArray()->one();
//跳到展示页面
public function actionArt()
{
$model = new Ceshi();
$type = Type::find()->asArray()->all();
$data = array();
foreach($type as $k=>$v)
{
$data[$v['t_id']] = $v['type'];
}
return $this->render('art_list',['model'=>$model,'data'=>$data]);
}
//删除
$id = yii::$app->request->get('id');
$desc = Desc::findOne($id);
$res = $desc->delete();
//修改
public function actionUp()
{
if($_POST)
{
$info = yii::$app->request->post();
$id = $info['Ceshi']['id'];
$ss = Desc::findOne($id);
$ss->title = $info['Ceshi']['title'];
$ss->type_id = $info['Ceshi']['type'];
$ss->content = $info['Ceshi']['content'];
$res = $ss->save();
if($res)
{
return $this->redirect('?r=five/index');
}
}
else
{
$id = yii::$app->request->get('id');
$model = new Ceshi();
$desc = Desc::find()->where(['id'=>$id])->asArray()->one();
$type = Type::find()->asArray()->all();
$data = array();
foreach($type as $k=>$v)
{
$data[$v['t_id']] = $v['type'];
}
return $this->render('up',['model'=>$model,'desc'=>$desc,'data'=>$data]);
}
}
//添加
$info = yii::$app->request->post(); //接到的值
$type = new Type(); //实例化type model
$type->type = $info['type']; //
$res = $type->save(); //添加
石头超人
注册时间:2017-05-23
最后登录:2017-09-14
在线时长:3小时12分
最后登录:2017-09-14
在线时长:3小时12分
- 粉丝1
- 金钱365
- 威望20
- 积分595
共 0 条评论