diezeel / laravel-sphinx
用于 SphinxQL 的 Laravel 查询构建器
Requires
- php: >=5.5.9
- foolz/sphinxql-query-builder: ^2.1.0
Requires (Dev)
- illuminate/bus: ~5.2.0|~5.3.0|~5.4.0
- illuminate/database: ~5.2.0|~5.3.0|~5.4.0
- illuminate/support: ~5.2.0|~5.3.0|~5.4.0
- orchestra/testbench: ~3.1
- phpunit/phpunit: ~5.5
- symfony/filesystem: ^3.0
Suggests
- laravel/framework: The Laravel Framework
- prettus/l5-repository: Repositories to the database layer
README
Laravel-Sphinx 数据库连接器,提供表达式的查询构建器,Eloquent,ActiveRecord 风格的 ORM
安装
laravel-sphinx 可以通过添加到项目的 composer.json 文件中作为依赖项,使用 Composer 进行安装。
{ "require": { "diezeel/laravel-sphinx": "*" } }
有关更详细安装和用法说明,请参阅 Composer 的文档。
配置
更新 composer 后,将 ServiceProvider 添加到 config/app.php 中的 providers 数组中。
DieZeeL\Database\SphinxConnection\SphinxServiceProvider::class,
最后,您只需将 Sphinx 连接
添加到 config/database.php 中的数据库数组中。
'sphinx' => [ 'driver' => 'sphinx', 'host' => env('SPHINX_HOST', env('DB_HOST','127.0.0.1')), 'port' => 9306, ],
用法
获取连接并构建查询
$db = \DB::connection('sphinx');
使用查询构建器
$users = $db->table('rt')->where('votes', '>', 100)->get();
使用 Eloquent ORM
class Product extends \DieZeeL\Database\SphinxConnection\Eloquent\Model {} $product = Product::find(1); $products = Product::where('votes', '>', 1)->get(); $products = Product::match('name', 'match text')->get();
属性转换 对于 attr_multi
列的结果,可以选择格式,将其转换为数组。
对于 attr_multi
列类型,将 '(1, 2, 3)'
的值转换为数组 [1, 2, 3]
。
class Product extends \DieZeeL\Database\SphinxConnection\Eloquent\Model { protected $casts = [ 'tags' => 'mva', ]; }
查询构建器
$sq = $db->table('rt');
构建查询时,使用值的强类型(如何在 SphinxQl 中)。
注意:
id = 1
和id = '1'
不相同
-
integer 用于类型整数
attr_uint
-
float 用于类型浮点数
attr_float
-
bool (integer) 用于类型 bool
attr_bool
,将被转换为整数(0 或 1) -
array (MVA) 用于类型 MVA
attr_multi
$sq->insert([ 'id' => 1, 'tags' => [1, 2, 3] ]); // Output: INSERT INTO rt (id, tags) VALUES(1, (1, 2, 3))
-
string - 字符串值,在请求时转义
$sq->insert([ 'id' => 1, 'name' => "name 'text'" ]); // Output: INSERT INTO rt (id, name) VALUES(1, 'name \'text\'')
MATCH
-
$sq->match($column, $value, $half = false)
在全文字段中搜索。可以在同一个查询中使用多次。列可以是数组。值可以是表达式以绕过转义(并使用您自己的自定义解决方案)。
<?php $sq->match('title', 'Otoshimono') ->match('character', 'Nymph') ->match(array('hates', 'despises'), 'Oregano'); $sq->match(function(\Foolz\SphinxQL\Match $m) { $m->not('text'); });
对于
match
函数,使用库 SphinxQL::match
WITHIN GROUP, ORDER, OPTION
-
$sq->withinGroupOrderBy($column, $direction = 'ASC')
WITHIN GROUP ORDER BY $column [$direction]
方向可以省略为
null
,或为ASC
或DESC
,不区分大小写。 -
$sq->orderBy($column, $direction = null)
ORDER BY $column [$direction]
方向可以省略为
null
,或为ASC
或DESC
,不区分大小写。 -
$sq->option($name, $value)
OPTION $name = $value
为查询设置 SphinxQL 选项,例如
max_matches
或reverse_scan
。
whereMulti
-
$sq->whereMulti($column, $operator, $values)
所有参数转换为平坦数组
$sq->whereMulti('id', '=', [1, 2, 3, [4, 5]]); // Output: WHERE id = 1 and id = 2 and id = 3 and id = 4 and id = 5
对于
in
和not in
是不同的$sq->whereMulti('id', 'in', [1, 2, 3]); // Output: WHERE id in (1) and id in (2) and id in (3)
$sq->whereMulti('id', 'in', [1, [20, 21], [30, 31]]); // Output: WHERE id in (1) and id in (20, 21) and id in (30, 31) // Equivalently $sq->whereMulti('id', 'in', 1, [20, 21], [30, 31]); // Output: WHERE id in (1) and id in (20, 21) and id in (30, 31)
替换
-
$sq->replace($values)
$sq->replace([ 'id' => 1, 'name' => 'text name' ]);
API SphinxConnection
- \Foolz\SphinxQL\Drivers\Pdo\Connection $db->getSphinxQLDriversConnection()
- \Foolz\SphinxQL\Helper $db->getSphinxQLHelper()
- \Foolz\SphinxQL\SphinxQL $db->createSphinxQL()