一只小Songshu 2017-06-01 14:58:36 261次浏览 0条评论 0 1 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>
    没有找到数据。
您需要登录后才可以评论。登录 | 立即注册
在线
816