$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();
2. データを検索します
//根据主键获取一条数据 $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%']]);
以上、Yii20 のデータベース操作 1 を内容も含めて紹介しましたが、PHP チュートリアルに興味のある友人の参考になれば幸いです。