yii2 框架的 orm 基础操作
Laiyong Wang Lv6
PS : CLASS STUDENT SCORE 默认为三张表的类
单条数据查询
1
2
3
CLASS::findOne($id);
CLASS::find()->where('id='.$id)->one();
CLASS::find()->where(['id'=>$id])->one();
多条数据查询
1
2
CLASS::findAll();
CLASS::find()->where(1)->offset(0)->limit(20)->all();
单 where 条件
1
2
CLASS::find()->where('id='.$id)->one();
CLASS::find()->where(['id'=>$id])->one();
多 where 条件
1
2
3
4
5
6
7
8
9
10
11
12
13
//andWhere
CLASS::find()->where('id='.$id)->andWhere('name='.$name)->one();
CLASS::find()->where(['id'=>$id])->andWhere(['name'=>$name])->one();
//与之类似
CLASS::find()->where(['id'=>$id,'name'=>$name])->one();
CLASS::find()->where(['and',['id'=>$id],['name'=>$name]])->one();

//orWhere
CLASS::find()->where('id='.$id)->orWhere('name='.$name)->one();
CLASS::find()->where(['id'=>$id])->orWhere(['name'=>$name])->one();
//与之类似
CLASS::find()->where(['or',['id'=>$id],['name'=>$name]])->one();

子查询
1
2
3
4
5
6
7
8
9
SCORE::find('student_id')->where(['>','value',60])->andWhere(
'student_id'=>STUDENT::find()->select('id')->where(
[
'or',
['sex'=>'男'],
['sex'=>'女']
]
)
)
跨表查询
1
2
3
4
5
6
7
CLASS::find()->alias(c)->where(['>',c.id,10])
->leftJoin(['s'=>CLASS::tableName()],'c.student_id = s.id')
->andWhere(['s.sex','男'])
->andWhere(['in','s.id',[1,2,3,4,5,6]])
->all();


复杂写法
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
CLASS::find()->Where(
['or',
['id' => $id],
['and',
['name' => $name],
['desc' => $desc]
]
]
)->one();

//与之类似

CLASS::find()->where(['id' => $id])->orWhere(
[
'and',
['name' => $name],
['desc' => $desc]
]
);
//与之类似
CLASS::find()->where(['id' => $id])->orWhere(
[
'name' => $name,
'desc' => $desc
]
);

$li-margin-bottom = 0.8rem $post-tool-button-width = 2.5rem .post-tools-container { padding-top var(--component-gap) .post-tools-list { li { margin-bottom $li-margin-bottom &:last-child { margin-bottom 0 } } li.tools-item { position relative box-sizing border-box width $post-tool-button-width height $post-tool-button-width color var(--text-color-3) font-size 1.2rem background var(--background-color-1) border-radius 50% box-shadow 2px 2px 5px var(--shadow-color) cursor pointer &:hover { box-shadow 2px 2px 8px var(--shadow-hover-color) } i { color var(--text-color-3) } &:hover { color var(--background-color-1) background var(--primary-color) i { color var(--background-color-1) !important } } &.toggle-show-toc { display none } &.go-to-comments { .post-comments-count { position absolute top 0 right -1rem display none align-items center justify-content center box-sizing border-box min-width 1.1rem height 1.1rem padding 0 0.2rem color var(--badge-color) font-size 12px background var(--badge-background-color) border-radius 0.4rem +keep-tablet() { display none !important } } } } li.status-item { width $post-tool-button-width height $post-tool-button-width color var(--text-color-3) font-size 1.6rem cursor pointer &.post-lock { cursor default .fa-lock-open { display none color var(--keep-success-color) } &.decrypt { cursor pointer .fa-lock-open { display block } .fa-lock { display none } } } } } }