PhpBoot是一个专为快速开发RESTfulAPI而设计的PHP框架(加星请点这里>>>PbpBootGithub<<<)。PhpBootDB封装了PDO,方便开发者更方便的编写正确安全的SQL。下面接上一篇文章:RESTful接口快速开发实例,介绍PhpBoot\DB的使用。配置可以通过依赖注入来配置数据库。在需要数据库的类中添加依赖注入代码:usePhpBoot\DB\DB;usePhpBoot\DI\Traits\EnableDIAnnotations;classBooks{useEnableDIAnnotations;//通过@inject标签启用依赖注入/***@inject*@varDB*/private$db;publicfunctiongetBooks()...}实例化Books后,框架会根据@inject注解自动为属性$db赋值,其逻辑等价于:$books->db=$app->get(数据库::类);修改数据库配置在config.php中添加如下配置(数据库地址等需要根据实际情况修改):'DB.connection'=>'mysql:dbname=phpboot-example;host=127.0.0.1','DB.username'=>'root','DB.password'=>'root','DB.options'=>[],写SQL实现createBook,deleteBook,updateBook,findBooks方法,演示使用插入、删除、更新、选择。INSERTpublicfunctioncreateBook(Book$book){$newId=$this->db->insertInto('books')->values(['name'=>$book->name,'brief'=>$book->简短,...])->exec()->lastInsertId();返回$newId;}DELETEpublicfunctiondeleteBook($id){$this->db->deleteFrom('books')->where(['id'=>$id])->exec();}UPDATEpublicfunctionupdateBook(Book$book){$this->db->update('books')->set(['name'=>$book->name,'brief'=>$book->brief,...])->where(['id'=>$book->id])->exec();}SELECTpublicfunctionfindBooks($name,$offsit,$limit){$books=$this->db->select('*')->from('books')->where('nameLIKE?',"%$name%")->orderBy('id')->limit($offsit,$limit)->get();return$books;}高级用法说明示例显示PhpBoot\DB的基本用法。PhpBoot\DB还支持更复杂的SQLcomplexWHERE类似SQLWHEREa=1OR(b=2andc=3),可以通过以下代码实现:->where(['a'=>1])->orWhere(function(ScopedQuery$query){$query->where(['b'=>2,'c'=>3])})在上面的例子中,ScopedQuery可以是NestedScopedQuery。JOIN$db->select('books.*',DB::raw('authors.nameasauthor'))->from('books')->where(['books.id'=>1])->leftJoin('authors')->on('books.authorId=authors.id')->get()WHERE...IN...使用PDO时,WHEREIN的预处理方式很不方便,需要保留相等数量的IN元素?,例如:$pdo->prepare('SELECT*FROMtableWHEREaIN(?,?,?)')->execute([1,2,3])并使用PhpBoot\DB可以解决这个问题:$db->select()->from('table')->where('aIN(?)',[1,2,3]);默认使用SQL函数,框架会对输入进行转换,比如在表名和列名外加``,把变量当做绑定,比如下面的语句$db->select('count(*)AScount')->from('table')->where(['time'=>['>'=>'now()']]);等价的SQL:SELECT`count(*)AScount`FROM`table`where`time`>'now()'如果想让框架不转换,需要使用DB::raw(),例如:$db->select(DB::raw('count(*)AScount'))->from('table')->where(['time'=>['>'=>DB::raw('现在()')]]);相当于下面的SQLSELECTcount(*)AScountFROM`table`where`time`>now()subquery下面的代码演示了子查询的用法:$parent=$db->select()->from('table1')->哪里('a=1');$child=$db->select()->from($parent);等同于以下SQLSELECT*FROM(SELECT*FROM`table1`WHEREa=1)transaction$db->transaction(function(DB$db){$db->update('table1')->...$db->update('table1')->...})事务允许嵌套,但是只有最外层事务有效,内层嵌套事务和最外层事务会被视为同一个事务使用多个数据库PhpBoot定义了一个默认的DB类的构造方法,形式如下:DB::class=>\DI\factory([DB::class,'connect'])->parameter('dsn',\DI\get('DB.connection'))->parameter('username',\DI\get('DB.username'))->parameter('password',\DI\get('DB.password'))->parameter('options',\DI\get('DB.options')),所以如果你的业务只需要连接一个数据库,只需要配置DB.connection,DB.username,DB.password,DB.options.但有时可能需要在应用程序中连接到不同的数据库。这时候可以通过依赖注入配置多个库,比如:先配置另一个数据库连接'another_db'=>\DI\factory([DB::class,'connect'])->parameter('dsn','mysql:dbname=phpboot-example;host=127.0.0.1')->parameter('username','root')->parameter('password','root')->parameter('options',[])InjectthisconnectionwhereneededusePhpBoot\DB;classBooks{/***@injectanother_db*@varDB*/private$db2;}帮助文档在线文档QQ交流群:185193529我的邮箱caoyangmin@gmail.com
