PHP code example of bingher / db
1. Go to this page and download the library: Download bingher/db library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
bingher / db example snippets
return [
// 默认使用的数据库连接配置
'default' => env('DB_DRIVER', 'dm'),
...
// 数据库连接配置信息
'connections' => [
'dm' => [
// ★Builder类
'builder' => \bingher\db\builder\DM::class,
// ★Query类
'query' => \bingher\db\query\DM::class,
// ★数据库类型
'type' => \bingher\db\connector\DM::class,
...
],
]
];
return [
// 默认使用的数据库连接配置
'default' => env('DB_DRIVER', 'gbase'),
...
// 数据库连接配置信息
'connections' => [
'gbase' => [
// ★Builder类
'builder' => bingher\db\builder\GBase::class,
// ★Query类
'query' => bingher\db\query\GBase::class,
// ★数据库类型
'type' => bingher\db\connector\GBase::class,
// ★驱动类型: pdo_gbasedbt,pdo_odbc
'driver' => env('C_DRIVER', 'pdo_odbc'),
...
],
]
];
return [
// 默认使用的数据库连接配置
'default' => env('DB_DRIVER', 'gauss'),
...
// 数据库连接配置信息
'connections' => [
'gauss' => [
// ★Builder类
'builder' => bingher\db\builder\OpenGauss::class,
// ★Query类
'query' => bingher\db\query\OpenGauss::class,
// ★数据库类型
'type' => bingher\db\connector\OpenGauss::class,
...
],
]
];
return [
// 默认使用的数据库连接配置
'default' => env('DB_DRIVER', 'kdb'),
...
// 数据库连接配置信息
'connections' => [
'kdb' => [
// ★Builder类
'builder' => bingher\db\builder\KingBase::class,
// ★Query类
'query' => bingher\db\query\KingBase::class,
// ★数据库类型
'type' => bingher\db\connector\KingBase::class,
...
],
]
];
return [
// 默认连接
'default' => 'main',
// 索引名前缀
'prefix' => '',
// 写操作是否等待 Meilisearch 任务完成
'wait' => true,
// 等待任务超时时间(毫秒)
'task_timeout' => 10000,
// 轮询任务间隔(毫秒)
'task_interval' => 50,
// 未指定 limit 时的默认返回条数
'limit' => 1000,
// 按条件批量更新/删除时最多处理的文档数(0 表示不限)
'max_update_rows' => 1000,
'connections' => [
'main' => [
// 服务地址
'host' => 'http://127.0.0.1:7700',
// API Key
'api_key' => '',
],
],
];
use bingher\db\meilisearch\Client as Meili;
Meili::setConfig([
'connections' => [
'main' => [
'host' => 'http://127.0.0.1:7700',
'api_key' => 'your-key',
],
],
]);
use bingher\db\meilisearch\Client as Meili;
// 查询
$list = Meili::table('books')
->where('category', '技术')
->where('price', '>=', 50)
->whereIn('id', [1, 2, 3])
->order('price', 'desc')
->limit(10)
->select();
// 全文检索(等价于 Meilisearch 的 q 参数)
$list = Meili::table('books')->search('php', ['title', 'author'])->select();
// like 查询会自动转换为全文检索关键词
$list = Meili::table('books')->whereLike('title', '%php%')->select();
// 单条、字段、统计、分页
$book = Meili::table('books')->find(1);
$title = Meili::table('books')->where('id', 1)->value('title');
$count = Meili::table('books')->where('status', 1)->count();
$page = Meili::table('books')->paginate(15); // 返回 think\Paginator
// 写入
Meili::table('books')->insert([...]);
Meili::table('books')->insertAll($dataList);
Meili::table('books')->where('category', '技术')->update(['status' => 1]);
Meili::table('books')->where('id', 1)->delete();
// 调试:查看编译后的 filter 与请求参数
echo Meili::table('books')->where('status', 1)->getFilter();
dump(Meili::table('books')->where('status', 1)->getSearchParams());
// 原生参数透传
Meili::table('books')->option('showRankingScore', true)->select();
use bingher\db\meilisearch\Model;
class Book extends Model
{
// 索引名(等价 ThinkORM 的表名,未设置时由类名转换)
protected $index = 'books';
// 软删除字段(置空表示不启用)
protected $deleteTime = 'delete_time';
protected $type = ['price' => 'float', 'status' => 'int'];
}
Book::find(1);
Book::where('category', '技术')->order('price', 'desc')->select();
Book::create(['title' => 'PHP 实战', 'price' => 88.5]);
$book = Book::find(1);
$book->price = 99;
$book->save();
$book->delete(); // 软删除
Book::destroy([1, 2], true); // 物理删除
Book::withTrashed()->where('id', 1)->find();
Book::onlyTrashed()->select();
$book->restore(); // 恢复软删除
Meili::table('books')->updateSettings([
'searchableAttributes' => ['title', 'author'],
'filterableAttributes' => ['id', 'category', 'price', 'status', 'delete_time'],
'sortableAttributes' => ['id', 'price'],
]);
shell
composer