igorv/database
Composer 安装命令:
composer require igorv/database
包简介
Simple database querybuilder for PDO.
README 文档
README
Perform install using composer:
composer require igorv/database
Configuration in your bootstrap file:
require 'vendor/autoload.php'; use IgorV\Database\DB; DB::config([ 'dsn' => 'mysql:host=localhost;dbname=example', 'username' => 'username', 'password' => 'password', 'options' => [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION] ]);
DB can now be accessed using the DB static class.
Usage
To use the query builder, call table() method containing the table your wish to query followed by other methods needed to build your query.
Select queries can be performed by appending either first() or get() method. first() returns the first record as a ResultSet object. get() returns a Collection object with the ResultsSet objects.
$user = DB::table('users')->where('id', 1)->first(); $posts = DB::table('posts')->where('visible', true)->get();
To inject query results into a different class use the as() method:
$user = DB::table('users')->sortBy('created_at', 'desc')->as(User::class)->first();
Inserting database records is done by appending insert() method at the end. Returns the number of rows affected:
$inserted = DB::table('users')->insert([ 'user' => $user, 'password' => $password, 'active' => true ]);
Updating records is done by appending update() method. Returns the number of affected rows:
$updated = DB::table('users')->where('id', $id)->update([ 'name' => 'John Doe', 'age' => 41 ]);
Deleting records is done by appending delete() method. Returns the number of affected rows:
$deleted = DB::table('users')->where('age', '>', '10')->delete();
Counting records can be done by appending count() method. Returns the number of rows matching your query:
$number = DB::table('users')->where('active', true)->count();
统计信息
- 总下载量: 24
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-05-31