yii2 框架的 orm 基础操作
Laiyong Wang Lv5
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
]
);