Yii2.0demo

https://github.com/itwangmingfei/YII-demo

多表连接 控制器不支持hasmany hasone直接定义在model中

1
2
3
4
5
6
7
8
//Models 关联一个表,对应一个 hasone   Content::className() 存在或创建一个Models Content;
public function getcontent(){
return $this->hasOne(Content::className(),["id"=>"cid"]);
}
//关联一个表对应多条数据 hasMany
public function getnovel(){
return $this->hasMany(Novels::className(),['cid'=>'id']);
}

控制器调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

//index.php?r=adminquery/list
public function actionList(){

$content = Content::find(2)->with('novel')->asarray()->one();
echo "<pre>";
var_dump($content);

}
//index.php?r=adminquery/novels
public function actionNovels(){

$novel = Novels::find()->where(['cid'=>3])->with('content')->asarray()->all();
echo "<pre>";
var_dump($novel);
}
//index.php?r=adminquery/info
public function actionInfo(){

$sql = "select * from content where id=:id";
$res = Content::findbysql($sql,[":id"=>3])->one();
echo "<pre>";
var_dump($res);

}

原生操作

1
2
3
4
5
6
7
8
9
//index.php?r=adminquery/info
public function actionInfo(){

$sql = "select * from content where id=:id";
$res = Content::findbysql($sql,[":id"=>3])->one();
echo "<pre>";
var_dump($res);

}

基本增删改查

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
namespace app\controllers;

use \yii\base\Controller;
use Yii;
use app\models\Content;

class AdminController extends Controller
{
/*
@组件接收 $Request = Yii::$app->request;
@get传值并验证是否是get请求
@params id
@index.php?r=admin/get&id=50
*/
public function actionGet(){
$Request = Yii::$app->request;
$id = $Request->get('id');
if (!$Request->isget){
echo "只接受get请求";
return;
}
if($id != null){
//执行输出或操作
echo "get id ". $id;
}else{
echo "not get file params: id ";
}

}
/*@post传值并验证是否是post请求 */
public function actionPost(){
$Request = Yii::$app->request;

if (!$Request->ispost){
echo "只接受POST请求";
return;
}

$id = $Request->post('id');
if($id !=null){
//执行输出或操作
echo "POST id ". $id;
}else{
echo "not post file params: id ";
}
}

/*@List */
public function actionList(){
$model = Content::find();
$total = $model->count();
$page = 1;
$pagesize = 2;
$offset = ($page-1)*$pagesize;
$where = "";
$list = $model->where($where)->offset($offset)->limit($pagesize)->all();
var_dump($list);
exit;
}
/*@Add */
public function actionAdd(){
/*@添加数据 */
$model = new Content();
$model->title = "ggllook";
$model->contents = "sdf";
$model->uid = 2;
$res = $model->save();
var_dump($res);

}
/*@update */
public function actionUp(){
/*@修改数据 */
$model = Content::findOne(1);
$model->title= "wmf";
$model->contents="出水电费";
$res = $model->save();
var_dump($res);
}
/*@info */
public function actionInfo(){
$row = Content::findOne(3);
var_dump($row);
echo "info";
}
/*@del */
public function actionDel(){
/*@删除 */
$model = Content::findOne(1);
$model->delete();
echo "list";
}
}