初探yii2框架,对增删改查,关联查询等数据库基本操作的简单实践。
数据库配置。
/config/db.php 进行数据库配置
实践过程中有个test库-》test表-》两条记录如下
mysql> select * from test; +----+--------+ | id | name | +----+--------+ | 1 | zhuai | | 2 | heng | +----+--------+ 18 rows in set (0.00 sec)
sql 查询方式
yii2 提供了原始的数据库查询方式findBySql;同时, 通过占位符的方式,自动进行了基本的sql注入防御 。上码
// 最基本的查询方式 $sql = "select * from test where 1"; $res = Test::findBySql($sql)->all(); var_dump(count($res)); // res->2 // findbysql 防止sql注入方式 $id = '1 or 1=1'; $sql = "select * from test where id = " . $id; $res = Test::findBySql($sql)->all(); var_dump(count($res)); // res-> 2 $sql = "select * from test where id = :id"; // 定位符会自动防止sql 注入 $res = Test::findBySql($sql,array(":id"=>$id))->all(); var_dump(count($res)); // res->1
activeRecord查询方式
每个框架除了原有的sql方式,都会提供相应的封装的查询方式,yii2亦然。
创建model
yii的model基本方式如下,代码如下不赘述。
<"htmlcode">use app\models\Test; 增加操作 // add 操作 $test = new Test(); $test->name = 'test'; // 合法性校验 $test->validate(); if($test->hasErrors()){ echo "数据不合法"; die; } $test->save();查询操作
查询操作先上官方文档
activeRecord doc
where doc
需要强调的是:yii查询提供了特别多丰富的库,例如代码中的批量查询处理等等,细节可以看文档。
// select // id = 1 $res = Test::find()->where(['id' => 1])->all(); var_dump(count($res)); //1 // id > 0 $res = Test::find()->where(['>','id',0])->all(); var_dump(count($res)); //2 // id > =1 id <=2 $res = Test::find()->where(['between','id',1,2])->all(); var_dump(count($res)); //2 // name字段like $res = Test::find()->where(['like', 'name', 'cuihuan'])->all(); var_dump(count($res)); //2 // 查询的使用 obj->array $res = Test::find()->where(['between','id',1,2])->asArray()->all(); var_dump($res[0]['id']); //2 // 批量查询,对于大内存操作的批量查询 foreach (Test::find()->batch(1) as $test) { var_dump(count($test)); }删除操作
// delete // 选出来删除 $res = Test::find()->where(['id'=>1])->all(); $res[0]->delete(); // 直接删除 var_dump(Test::deleteAll('id>:id', array(':id' => 2)));修改操作
除了代码中方式,yii2直接提供update操作。
// 活动记录修改 $res = Test::find()->where(['id'=>4])->one(); $res->name = "update"; $res->save();关联查询操作
关联查询示例中两个表:
一个学生表(student):id ,name;
一个分数表(score):id,stu_id,score
// 相应学生的所有score $stu = Student::find()->where(['name'=>'xiaozhuai'])->one(); var_dump($stu->id); // 基本获取 $scores_1 = $stu->hasMany('app\model\Score',['stu_id'=>$stu->id])->asArray()->all(); $scores_2 = $stu->hasMany(Score::className(),['stu_id'=>'id'])->asArray()->all(); var_dump($scores_1); var_dump($scores_2);两种关联查询方式;但是,在controller进行相关操作,代码显的过于混乱,在model中封装调用
首先在student model中封装相关关联调用函数
<"htmlcode">// 函数封装之后调用 $scores = $stu->getScores(); var_dump($scores); // 利用__get 的自动调用的方式 $scores = $stu->scores; var_dump($scores);最后
上面在yii2的部署和使用过程中的一些基本的增删改查,关联查询等操作。
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
- 群星.1986-新格名曲精丫新格】【WAV+CUE】
- 群星.1986-新格名曲精选-歌3【新格】【WAV+CUE】
- 妙音唱片净化心灵·归还真我群星-无尘系列6CDWAV+CUE
- 孙协志2004-胜立精选(闽南语)[大旗][WAV+CUE]
- 徐若瑄《先听我说完》[320K/MP3][93.78MB]
- 徐若瑄《先听我说完》[FLAC/分轨][498.77MB]
- Resa Club《Soft Opening(Explicit)》[320K/MP3][72.41MB]
- 宝丽金群星.1997-舞池中Remix【宝丽金】【WAV+CUE】
- 吕继宏2010-广州独唱音乐会LIVE专辑[DMC唱片][WAV+CUE]
- 李丹阳1999-红月亮[上海音像][WAV+CUE]
- 廖昌永2007-怀念[中唱][WAV+CUE]
- 區瑞強1999-極品陌上歸人II[FLAC]
- [In-AkustikINAK7961]ATastySoundCollection-CoffeeLatin《咖啡和拉斗(2009)[WAV+CUE]
- Resa Club《Soft Opening(Explicit)》[FLAC/分轨][192.37MB]
- 紫龙游戏《钢岚 (原声音乐辑)》[320K/MP3][145.56MB]