Yii2.0 AR-CURD小表单简单操作
数据库连接经常需要在多个地方使用到,常见是应用组件的方式来配置
创建LookController.php
<?php
namespace frontendcontrollers;
use Yii;
use yiiwebController;
use frontendmodelsLook;
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 frontendmodels;
use yiidbActiveRecord;
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 yiihelpersHtml;
use yiiwidgetsActiveForm;
$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 yiihelpersHtml;
use yiiwidgetsActiveForm;
$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>
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇: PHP 即点即改
- 下一篇:没有了