一只小Songshu 2017-06-01 16:06:20 2361次浏览 1条回复 1 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]);
	}
	
}

}

接下来就是展示部分 展示

<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>
<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>

add

<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>

觉得很赞
  • 回复于 2017-06-03 14:12 举报

    肯发帖就不错,只是你这是查询两表,还以为一次修改两张表呢

您需要登录后才可以回复。登录 | 立即注册