Home > Backend Development > PHP Tutorial > Yii20 database operation 1

Yii20 database operation 1

WBOY
Release: 2016-07-29 09:02:19
Original
899 people have browsed it
1. Add a piece of data
$post = new Post;
$post->name = 'yayue';
$post->age = 24;
$post->save();
$pk = $post->getPrimaryKey(); //返回主键id
//也可以这么写
$array = [
  'name' => 'yayue',
  'age' => 24,
];
$post->setAttributes($array);
$post->save();
Copy after login

2. Find data

//根据主键获取一条数据
$data= $post->findOne($pk);
$data= $post->findOne(['pk'=>1,'name'=>'yayue']);
$data= $post->find()->where(['id'=>$pk])->one();
//获取age=24多条数据
$data= $post->find()->where(['age'=>24])->all();
$data= $post->findAll(['name'=>'yayue','age'=>24]);

//获取age=24并且name=yayue多条数据
$data= $post->find()->where(['age'=>24])->andWhere(['name'=>'yayue'])->all();

//获取age=24或者name=yayue多条数据
$data= $post->find()->where(['age'=>24])->orWhere(['name'=>'yayue'])->all(); 

//获取指定字段name、age age=24多条数据
$data= $post->find()->select(['name','age'])->where(['age'=>24])->all();

//统计age=24数据
$data= $post->find()->where(['age'=>24])->cout();

//查询age最大的数据
$data= $post->find()->max('age');

//查询age最小的数据
$data= $post->find()->min('age');

//获取数据通过age排序
$data= $post->find()->orderBy('age desc')->all();

//获取age=24并且name=yayue多条数据
$data= $post->find()->where(['age'=>24])->andWhere(['name'=>'yayue'])->all();

//获取age=24或者name=yayue多条数据
$data= $post->find()->where(['age'=>24])->orWhere(['name'=>'yayue'])->all();

//获取指定字段name、age age=24多条数据
$data= $post->find()->select(['name','age'])->where(['age'=>24])->all();

//统计age=24数据
$data= $post->find()->where(['age'=>24])->cout();

//查询age最大的数据
$data= $post->find()->max('age');

//查询age最小的数据
$data= $post->find()->min('age');

//获取数据通过age排序
$data= $post->find()->orderBy('age desc')->all();


//获取age=24并且name=yayue多条数据
$data= $post->find()->where(['age'=>24])->andWhere(['name'=>'yayue'])->all();

//获取age=24或者name=yayue多条数据
$data= $post->find()->where(['age'=>24])->orWhere(['name'=>'yayue'])->all(); 

//获取指定字段name、age age=24多条数据
$data= $post->find()->select(['name','age'])->where(['age'=>24])->all();

//统计age=24数据
$data= $post->find()->where(['age'=>24])->count();

//查询age最大的数据
$data= $post->find()->max('age');

//查询age最小的数据
$data= $post->find()->min('age');

//获取数据通过age排序
$data= $post->find()->orderBy('age desc')->all();

//获取数据age>24
$data= $post->find()->where('age > :age',['age'=>24])->all();

//获取数据age=24 or age=25
$data = $post->find()->where(['or','age=24', 'age=25'])->all();

//其它where条件
// WHERE id=1 or id=2 
where('id=1 or id=2'); 
// WHERE id=:id1 or id=:id2 
where('id=:id1 or id=:id2', [':id1'=>1, ':id2'=>2]); 
// WHERE id=1 OR id=2   
where(['or', 'id=1', 'id=2']); 
// WHERE id=1 AND (type=2 OR type=3) 
where(['and', 'id=1', ['or', 'type=2', 'type=3']]);
// WHERE `id` IN (1, 2) 
where(['in', 'id', [1, 2]]);
// WHERE `id` NOT IN (1, 2) 
where(['not in', 'id', [1,2]]);
// WHERE `name` LIKE '%Qiang%' 
where(['like', 'name', '%Qiang%']); 
// WHERE `name` LIKE '%Qiang' AND `name` LIKE '%Xue' 
where(['like', 'name', ['%Qiang', '%Xue']]); 
// WHERE `name` LIKE '%Qiang' OR `name` LIKE '%Xue' 
where(['or like', 'name', ['%Qiang', '%Xue']]); 
// WHERE `name` NOT LIKE '%Qiang%' 
where(['not like', 'name', '%Qiang%']); 
// WHERE `name` NOT LIKE '%Qiang%' OR `name` NOT LIKE '%Xue%' 
where(['or not like', 'name', ['%Qiang%', '%Xue%']]);
Copy after login

The above introduces Yii20 database operation 1, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template