Yii2.0 AR-CURD小表单简单操作

数据库连接经常需要在多个地方使用到,常见是应用组件的方式来配置

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OTeJqtUh-1660296323529)(https://img-blog.csdn.net/20170720111734085?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbTBfMzgwMjczNTg=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)]

创建LookController.php

<?php  
namespace frontend\controllers;
use Yii;
use yii\web\Controller;
use frontend\models\Look;
class LookController extends Controller{
	public function actionIndex(){
		$model=new Look();
		$age=$model->getAge();
		return $this->renderpartial('index',['model'=>$model,'age'=>$age]);
	}
	public function actionAdd(){
		$post=yii::$app->request->post('Look');
		// print_r($post);
		$username=$post['usernames'];
		$sex=$post['sexs'];
		$hobby=implode(',', $post['hobbys']) ;
		$age=$post['ages'];
		$model=new Look();
		$model->username=$username;
		$model->sex=$sex;
		$model->hobby=$hobby;
		$model->age=$age;
		$res=$model->save();
		if($res){
			echo '<script>alert("添加成功");location.href="'.'?r=look/show'.'";</script>';
		}
	}
	public function actionShow(){
		$model=new Look();
		$data=$model->find()->asArray()->all();
		return $this->renderpartial('show',['data'=>$data]);		    
	}
	public function actionDel(){
		$model=new Look();
		$id=yii::$app->request->get('id');
		$row=$model->find()->where(['id'=>$id])->one();
		$res=$row->delete();
		if($res){
			echo '<script>alert("删除成功");location.href="'.'?r=look/show'.'";</script>';
		}
	}
	public function actionUpd(){
		$model=new Look();
		$post=yii::$app->request->post('Look');
		$id=$post['ids'];
		// print_r($post);die;
		$row=$model->find()->where(['id'=>$id])->one();

		$username=$post['usernames'];
		$sex=$post['sexs'];
		$hobby=implode(',', $post['hobbys']) ;
		$age=$post['ages'];
		$row->username=$username;
		$row->sex=$sex;
		$row->hobby=$hobby;
		$row->age=$age;
		$res=$row->save();
		if($res){
			echo '<script>alert("修改成功");location.href="'.'?r=look/show'.'";</script>';
		}
	}
		public function actionSave(){
		$model=new Look();
		$id=yii::$app->request->get('id');
		$row=$model->find()->where(['id'=>$id])->asArray()->one();

		$hobby=explode(',',$row['hobby'] );
		$arr=[];
		foreach($hobby as $k => $v){
			$arr[$v]=$v;
		}
		
		// print_r($row);die;
		$age=$model->getAge();
		return $this->renderpartial('save',['row'=>$row,'model'=>$model,'age'=>$age,'arr'=>$arr]);
	}
}
?>

创建模型Look.php

<?php  
namespace frontend\models;

use yii\db\ActiveRecord;

class Look extends ActiveRecord
{
	public $usernames;
	public $sexs;
	public $hobbys;
	public $ages;
	public $ids;

        public function attributeLabels()
        {
            return [
                'ids' => '',
            ];
        }
    /**
     * @return string 返回该AR类关联的数据表名
     */
    public static function tableName()
    {
        return 'look';
    }
    public function getAge(){
        $arr=[];
        for($i=18;$i<=50;$i++){
            $arr[$i]=$i;
        }
        return $arr;
    }
}
?>

创建视图/views/look/index.php

<center>
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;

$form = ActiveForm::begin([
    'id' => 'login-form',
    'options' => ['class' => 'form-horizontal'],
    'action'=>'?r=look/add',
    'method'=>'post'
]) ?>
    <?= $form->field($model, 'usernames') ?>
    <?= $form->field($model, 'sexs')->radioList([1=>'男',0=>'女']) ?>
    <?= $form->field($model, 'hobbys')->checkboxList(['sleep'=>'睡觉','play'=>'玩']) ?>
    <?= $form->field($model, 'ages')->dropDownList($age) ?>

    <div class="form-group">
        <div class="col-lg-offset-1 col-lg-11">
            <?= Html::submitButton('Login', ['class' => 'btn btn-primary']) ?>
        </div>
    </div>
<?php ActiveForm::end() ?>
</center>	

创建视图/views/look/show.php

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
<center>
	<table border="1">
		<tr>
			<td>ID</td>
			<td>USERNAME</td>
			<td>SEX</td>
			<td>HOBBY</td>
			<td>AGE</td>
			<td>OPTION</td>
		</tr>
		<?php foreach($data as $v){ ?>
		<tr>
			<td><?= $v['id'] ?></td>
			<td><?= $v['username'] ?></td>
			<td><?= $v['sex'] ?></td>
			<td><?= $v['hobby'] ?></td>
			<td><?= $v['age'] ?></td>
			<td>
				<a href="?r=look/del&id=<?=$v['id']?>">DEL</a>
				<a href="?r=look/save&id=<?=$v['id']?>">UPDATE</a>
			</td>
		</tr>
		<?php } ?>
	</table>
</center>
</body>
</html>

创建视图/views/look/save.php

<center>
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;

$form = ActiveForm::begin([
    'id' => 'login-form',
    'options' => ['class' => 'form-horizontal'],
    'action'=>'?r=look/upd',
    'method'=>'post'
]) ?>
<?php $model->usernames=$row['username'] ?>
    <?= $form->field($model, 'usernames') ?>
<?php $model->sexs=$row['sex'] ?>
    <?= $form->field($model, 'sexs')->radioList([1=>'男',0=>'女']) ?>

<?php $model->hobbys=$arr ?>
    <?= $form->field($model, 'hobbys')->checkboxList(['sleep'=>'睡觉','play'=>'玩']) ?>

<?php $model->ages=$row['age'] ?>
    <?= $form->field($model, 'ages')->dropDownList($age) ?>

<?php $model->ids=$row['id'] ?>
    <?= $form->field($model, 'ids')->hiddenInput() ?>
    <div class="form-group">
        <div class="col-lg-offset-1 col-lg-11">
            <?= Html::submitButton('SAVE', ['class' => 'btn btn-primary']) ?>
        </div>
    </div>
<?php ActiveForm::end() ?>
</center>	
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值