yii 2.0 ar模式两表操作 [ 2.0 版本 ]
首先创建MODEL
<?php
namespace frontend\models;
use Yii;
use yii\db\ActiveRecord;
class Mail extends ActiveRecord
{
public static function tableName()
{
return 'mail';
}
public function getEmail()
{
return $this->hasMany(Email::className(),['e_id'=>'e_id']);
}
}
?>
然后是controller
<?php
namespace frontend\controllers;
use Yii;
use yii\web\Controller;
use yii\db\ActiveRecord;
use frontend\models\Mail;
use frontend\models\Email;
class MailController extends Controller
{
public $enableCsrfValidation = false;
public function actionIndex()
{
$query = Mail::find()->select("mail.*,email.u_name")->joinWith('email')->asArray()->all();
return $this->render('index',['mail'=>$query]);
}
public function actionDel()
{
$id = Yii::$app->request->get('id');
$data = Mail::findOne($id);
$info = $data->delete();
if($info)
{
return $this->redirect('?r=mail/index');
}
}
public function actionAdd()
{
if(Yii::$app->request->post())
{
$uname = Yii::$app->request->post('uname');
$email = Yii::$app->request->post('email');
$mail = new Mail();
$mail->uname=$uname;
$mail->e_id=$email;
if($mail->save())
{
return $this->redirect('?r=mail/index');
}
}
else
{
$info = Email::find()->asArray()->all();
return $this->render('add',['info'=>$info]);
}
}
public function actionSave()
{
if(Yii::$app->request->post())
{
$id = Yii::$app->request->post('id');
$name = Yii::$app->request->post('uname');
$email = Yii::$app->request->post('email');
$mail = Mail::findOne($id);
$mail->uname=$name;
$mail->e_id=$email;
if($mail->save())
{
return $this->redirect('?r=mail/index');
}
}
else
{
$id = Yii::$app->request->get('id');
$query = Mail::find()->select("mail.*,email.u_name")->joinWith('email')->where('mail.id='.$id)->asArray()->all();
$info = Email::find()->asArray()->all();
return $this->render('save',['data'=>$query,'info'=>$info]);
}
}
}
接下来就是展示部分
//展示
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<a href="?r=mail/add">添加</a>
<table border="1">
<th>ID</th>
<th>用户</th>
<th>邮箱</th>
<th>操作</th>
<?php foreach($mail as $v){?>
<tr>
<td><?=$v['id']?></td>
<td><?=$v['uname']?></td>
<td><?=$v['u_name']?></td>
<td><a href="?r=mail/del&id=<?=$v['id']?>">删除</a>|<a href="?r=mail/save&id=<?=$v['id']?>">修改</a></td>
</tr>
<?php }?>
</table>
</body>
</html>
//修改
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form action="?r=mail/save" method="post">
<table>
<tr>
<td>用户名</td>
<td><input type="text" name="uname" value="<?=$data[0]['uname']?>"></td>
</tr>
<tr>
<td>邮箱</td>
<td><select name="email">
<?php foreach($info as $v){?>
<option value="<?=$v['e_id']?>" <?php if($v['e_id'] == $data[0]['e_id']){echo 'selected';}?>><?=$v['u_name']?></option>
<?php }?>
</select></td>
</tr>
<tr>
<input type="hidden" name="id" value="<?=$data[0]['id']?>">
<td><input type="submit" value="修改"></td>
</tr>
</table>
</form>
</body>
</html>
//增加
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form action="?r=mail/add" method="post">
<table>
<tr>
<td>用户名</td>
<td><input type="text" name="uname"></td>
</tr>
<tr>
<td>邮箱</td>
<td><select name="email">
<?php foreach($info as $v){?>
<option value="<?=$v['e_id']?>"><?=$v['u_name']?></option>
<?php }?>
</select></td>
</tr>
<tr>
<td><input type="submit" value="提交"></td>
</tr>
</table>
</form>
</body>
</html>
一只小Songshu
注册时间:2017-05-23
最后登录:2017-12-12
在线时长:2小时55分
最后登录:2017-12-12
在线时长:2小时55分
- 粉丝1
- 金钱70
- 威望10
- 积分190
热门源码
- 基于 Yii 2 + Bootstrap 3 搭建一套后台管理系统 CMF
- 整合完 yii2-rbac+yii2-admin+adminlte 等库的基础开发后台源码
- 适合初学者学习的一款通用的管理后台
- yii-goaop - 将 goaop 集成到 Yii,在 Yii 中优雅的面向切面编程
- yii-log-target - 监控系统异常且多渠道发送异常信息通知
- 店滴云1.3.0
- 面向对象的一小步:添加 ActiveRecord 的 Scope 功能
- Yii2 开源商城 FecShop
- 基于 Yii2 开发的多店铺商城系统,免费开源 + 适合二开
- leadshop - 基于 Yii2 开发的一款免费开源且支持商业使用的商城管理系统
共 2 条评论
谢谢
多谢分享!
评论下试试看