前言
公司业务需要转学PHP,而PHP中一个功能强大且生态链完整的PHP接口框架 PhalAPI
值得大家去学习,本学习笔记持续更新!
虽然官方文档写的十分明白,以及CSDN中PhalAPI框架内容也少之又少。
因此,以自我踩坑为基础,提供一个更为精简的学习笔记,本学习笔记将会省略部分安装及简单操作。
拓展篇内容将全面直线进入强化期
约束
- 需要满足PhalAPI框架的 ADM(API Domain Model) 模式
- 内容需全自做,若遇到不会的题目,可在末尾找到相关答案
- Model查询需要使用NotORM
基于MySQL数据库交互题目
自建数据库,字段需求:
简单篇
查询某所有的数据
参考语句 $this->getORM()->select()
答:………
单查询id、project、title
参考语句 $this->getORM()->select()
答:………
聚合函数返回总数量
参考语句 $this->getORM()->select()
答:………
查询id为49的数据
参考语句 $this->getORM()->where()
答:………
查询id为49并且title为账号管理的数据
参考语句 $this->getORM()->where()
答:………
中级篇
查询id为50并且title为账号列表的数据(数组方式)
参考语句 $array = array(); $this->getORM()->where($array);
答:………
查询id为1或title为账号列表的title数据
参考语句 $this->getORM()->where()
答:………
嵌套查询id为51并且title为添加账号
参考语句 $this->getORM()->where()
答:………
嵌套查询id为51并且title为添加账号(关联性数组)
参考语句 $this->getORM()->where()
答:………
高级篇
in方式查询id为49,50,51的数据
参考语句 $this->getORM()->where()
答:………
in方式查询id为49,50,51和title为账号管理、账号列表、添加账号
参考语句 $this->getORM()->where()
答:………
查询title中包含 账号 的所有数据
参考语句 $this->getORM()->where()
答:………
查询title中为空的所有数据
参考语句 $this->getORM()->where()
答:………
倒序查询id所有数据
参考语句 $this->getORM()->order()
答:………
以id为分组查询所有project字段中数据为 admin的所有数据
参考语句 $this->getORM()->group()
答:………
查询10条数据
参考语句 $this->getORM()->litmit()
答:………
答案
/**
* csdn PhalAPI 数据库交互答案
* @Author Marinda
*/
// 初级篇
public function selectAll(){
return $this->getORM()->select("*")->fetchAll();
}
public function selectByIDAndProjectAndTitle(){
return $this->getORM()->select("id , project ,title")->fetchAll();
}
public function selectCount(){
// 另外ORM也封装了一个count函数
// return $this->getORM()->count();
return $this->getORM()->select("COUNT(*)")->fetchAll();
}
public function selectById(){
return $this->getORM()->where("id = ?",49)->fetchOne();
}
public function selectByIdAndTitle(){
return $this->getORM()->where("(id = ? or title = ?)",49,"账号管理")->fetchOne();
}
// 中级篇
public function midSelectByIdAndTitle(){
$arr = array();
$arr['id'] = 50;
$arr['title = ?'] = "账号列表";
return $this->getORM()->where($arr)->fetchOne();
}
public function midSelectByIdOrTitle(){
return $this->getORM()->where("id = ?",1)->or("title = ?",'账号列表')->fetchAll();
}
public function midSelectByIdAndsTitle(){
$arr = array("51","添加账号");
return $this->getORM()->where("(id = ? OR title = ?)",$arr)->fetchOne();
}
public function midSelectByIdRelationAndTitle(){
$arr = array(":id" => 51,":title","添加账号");
return $this->getORM()->where("(id = :id OR title = :title)",$arr)->fetchOne();
}
// 高级篇
public function highInSelectById(){
$arr = array(49,50,51);
return $this->getORM()->where("id",$arr)->fetchAll();
}
// in方式查询id为49,50,51和title为账号管理、账号列表、添加账号
public function highInSelectByIdAndTitle(){
$arr = array(array(49,"账号管理"),array(50,"账号列表"),array(51,"添加账号"));
return $this->getORM()->where("(id,title)",$arr)->fetchAll();
}
// 查询title中包含 账号 的所有数据
public function highLikeTitle(){
return $this->getORM()->where("title like ?","%账号%")->fetchAll();
}
// 查询title中为空的所有数据
public function highTitleNull(){
return $this->getORM()->where("title is null")->fetchAll();
}
public function highDescId(){
return $this->getORM()->order("id desc")->fetchAll();
}
public function highGroup(){
return $this->getORM()->group("id","project = 'admin'")->fetchAll();
}
public function highLimit(){
return $this->getORM()->limit(10)->fetchAll();
}
结束语
关于 PhalAPI学习笔记拓展篇 ——— 基于MySQL数据库交互题目 就讲到这里,对你有帮助的话!
- 点赞
- 收藏
谢谢你的观看!