ort-interactive-gmbh / laravel-couchbase
基于Couchbase的Eloquent模型和查询构建器,用于Laravel
Requires
- php: ^7.2
- ext-couchbase: >=2.4.0
- illuminate/container: ^7
- illuminate/database: ^7
- illuminate/events: ^7
- illuminate/support: ^7
Requires (Dev)
- mockery/mockery: ^1.3.1
- orchestra/testbench: ^5
- phpunit/phpunit: ^8.5
- vlucas/phpdotenv: >=4.0.0
- dev-master
- 7.0.0
- 6.0.0
- 5.8.0
- 5.7.0
- 5.6.0
- 5.5.0
- 5.4.0
- 0.6.4
- 0.6.3
- 0.6.2
- 0.6.1
- 0.6.0
- 0.5.1
- 0.5.0
- 0.4.0
- 0.3.0
- 0.2.0
- 0.1.0
- dev-release/7
- dev-laravel/6
- dev-laravel/5,8
- dev-laravel/5.7
- dev-laravel/5.6
- dev-laravel/5.5
- dev-laravel/5.4
- dev-develop
- dev-feature/laravel-5.8
- dev-feature/inline-parameters-fix
- dev-feature/hasLocalMany
This package is auto-updated.
Last update: 2024-09-26 16:57:03 UTC
README
一个支持Couchbase的Eloquent模型和查询构建器,使用原始的Laravel API。 此库扩展了原始Laravel类,因此使用的是完全相同的方法。
目录
安装
请确保已安装Couchbase PHP驱动程序。您可以在以下位置找到安装说明:http://developer.couchbase.com/documentation/server/current/sdk/php/start-using-sdk.html
使用Composer安装
composer require mpociot/couchbase
并在config/app.php
中添加服务提供者
Mpociot\Couchbase\CouchbaseServiceProvider::class,
对于Lumen的使用,请在bootstrap/app.php
中添加服务提供者。在此文件中,您还需要启用Eloquent。但是,您必须确保对$app->withEloquent();
的调用位于注册CouchbaseServiceProvider
的下方
$app->register(Mpociot\Couchbase\CouchbaseServiceProvider::class); $app->withEloquent();
服务提供者将向原始数据库管理器注册一个Couchbase数据库扩展。无需注册额外的门面或对象。当使用Couchbase连接时,Laravel将自动为您提供相应的Couchbase对象。
对于Laravel之外的使用,请参阅Capsule管理器并在其中添加
$capsule->getDatabaseManager()->extend('couchbase', function($config) { return new Mpociot\Couchbase\Connection($config); });
配置
在config/database.php
中更改默认数据库连接名称
'default' => env('DB_CONNECTION', 'couchbase'),
并添加一个新的Couchbase连接
'couchbase' => [ 'driver' => 'couchbase', 'host' => env('DB_HOST', 'localhost'), 'port' => env('DB_PORT', 8091), 'bucket' => env('DB_DATABASE'), 'username' => env('DB_USERNAME'), 'n1ql_hosts' => [ 'http://'.env('DB_HOST', 'localhost').':8093' ] ],
您可以使用以下配置连接到多个服务器或副本集
'couchbase' => [ 'driver' => 'couchbase', 'host' => ['host1', 'host2'], 'port' => env('DB_PORT', 8091), 'bucket' => env('DB_DATABASE'), 'username' => env('DB_USERNAME'), 'n1ql_hosts' => [ 'http://host1:8093', 'http://host2:8093' ] ],
Eloquent
此软件包包括一个启用Couchbase的Eloquent类,您可以使用它来定义对应集合的模型。
use Mpociot\Couchbase\Eloquent\Model as Eloquent; class User extends Eloquent {}
由于Couchbase不提供表的概念,因此将使用名为_type
的属性来定义文档。与原始Eloquent一样,类的下划线小写复数名称将被用作“表”名称,并将其放置在每个文档的_type
属性中。
您可以通过在模型上定义一个table
属性来指定自定义类型(表别名)
use Mpociot\Couchbase\Eloquent\Model as Eloquent; class User extends Eloquent { protected $table = 'my_users'; }
注意: Eloquent还将假设每个集合都有一个名为_id
的主键列。您可以通过定义primaryKey
属性来覆盖此约定。同样,您还可以定义一个connection
属性来覆盖当使用模型时应该使用的数据库连接的名称。
use Mpociot\Couchbase\Eloquent\Model as Eloquent; class MyModel extends Eloquent { protected $connection = 'couchbase'; }
其余的(应该)与原始Eloquent模型的工作方式相同。有关Eloquent的更多信息,请参阅https://laravel.net.cn/docs/eloquent
可选:别名
您还可以通过在config/app.php
中的别名数组中添加以下内容来注册Couchbase模型的别名
'CouchbaseModel' => 'Mpociot\Couchbase\Eloquent\Model',
这将允许您使用注册的别名,例如
class MyModel extends CouchbaseModel {}
查询构建器
数据库驱动程序直接集成到原始查询构建器中。当使用Couchbase连接时,您将能够构建流畅的查询以执行数据库操作。
$users = DB::table('users')->get(); $user = DB::table('users')->where('name', 'John')->first();
如果您没有更改默认的数据库连接,则在查询时需要指定它。
$user = DB::connection('couchbase')->table('users')->get();
有关查询构建器的更多信息,请参阅https://laravel.net.cn/docs/queries
模式
由于此Couchbase驱动程序实现使用单个存储桶来存储所有文档,因此未实现模式构建器。
示例
基本用法
检索所有模型
$users = User::all();
通过主键检索记录
$user = User::find('517c43667db388101e00000f');
Where语句
$users = User::where('votes', '>', 100)->take(10)->get();
或语句
$users = User::where('votes', '>', 100)->orWhere('name', 'John')->get();
与语句
$users = User::where('votes', '>', 100)->where('name', '=', 'John')->get();
使用Where In与数组
$users = User::whereIn('age', [16, 18, 20])->get();
使用Where Between
$users = User::whereBetween('votes', [1, 100])->get();
Where null
$users = User::whereNull('updated_at')->get();
Order By
$users = User::orderBy('name', 'desc')->get();
Offset & Limit
$users = User::skip(10)->take(5)->get();
Distinct
Distinct需要返回唯一值的字段。
$users = User::distinct()->get(['name']); // or $users = User::distinct('name')->get();
Distinct可以与where结合使用
$users = User::where('active', true)->distinct('name')->get();
高级Where
$users = User::where('name', '=', 'John')->orWhere(function($query) { $query->where('votes', '>', 100) ->where('title', '!=', 'Admin'); }) ->get();
Group By
未分组的选定列将使用$last函数进行聚合。
$users = Users::groupBy('title')->get(['title', 'name']);
聚合
$total = Order::count(); $price = Order::max('price'); $price = Order::min('price'); $price = Order::avg('price'); $total = Order::sum('price');
聚合可以与where结合使用
$sold = Orders::where('sold', true)->sum('price');
Like
$user = Comment::where('body', 'like', '%spam%')->get();
注意:Couchbase中的Like检查是区分大小写的
增加或减少列值
执行指定属性的递增或递减(默认1)
User::where('name', 'John Doe')->increment('age'); User::where('name', 'Jaques')->decrement('weight', 50);
返回更新对象的数量
$count = User->increment('age');
您还可以指定要更新的其他列
User::where('age', '29')->increment('age', 1, ['group' => 'thirty something']); User::where('bmi', 30)->decrement('bmi', 1, ['category' => 'overweight']);
软删除
当软删除模型时,实际上不会从您的数据库中删除。相反,会在记录上设置一个deleted_at时间戳。要为模型启用软删除,请将SoftDeletingTrait应用于模型
use Mpociot\Couchbase\Eloquent\SoftDeletes; class User extends Eloquent { use SoftDeletes; protected $dates = ['deleted_at']; }
更多信息请查看https://laravel.net.cn/docs/eloquent#soft-deleting
插入、更新和删除
插入、更新和删除记录与原始Eloquent的工作方式相同。
保存新模型
$user = new User; $user->name = 'John'; $user->save();
您还可以使用create方法在单行中保存新模型
User::create(['name' => 'John']);
更新模型
要更新模型,您可以检索它,更改属性,并使用save方法。
$user = User::first(); $user->email = 'john@foo.com'; $user->save();
删除模型
要删除模型,只需在实例上调用delete方法
$user = User::first(); $user->delete();
或通过其键删除模型
User::destroy('517c43667db388101e00000f');
有关模型操作更多信息,请查看https://laravel.net.cn/docs/eloquent#insert-update-delete
关系
支持的关联有
- hasOne
- hasMany
- belongsTo
- belongsToMany
- embedsOne
- embedsMany
示例
use Mpociot\Couchbase\Eloquent\Model as Eloquent; class User extends Eloquent { public function items() { return $this->hasMany('Item'); } }
以及反向关系
use Mpociot\Couchbase\Eloquent\Model as Eloquent; class Item extends Eloquent { public function user() { return $this->belongsTo('User'); } }
belongsToMany关系不会使用一个“中间”表,而是将id推送到一个名为related_ids的属性中。这使得belongsToMany方法的第二个参数变得无用。如果您想为关系定义自定义键,请将其设置为null
use Mpociot\Couchbase\Eloquent\Model as Eloquent; class User extends Eloquent { public function groups() { return $this->belongsToMany('Group', null, 'user_ids', 'group_ids'); } }
其他关系目前尚不支持,但将来可能会添加。更多关于这些关系的详细信息请查看https://laravel.net.cn/docs/eloquent#relationships
EmbedsMany关系
如果您想嵌入模型而不是引用它们,可以使用embedsMany
关系。这个关系与hasMany
类似,但它将模型嵌入到父对象中。
记住:这些关系返回Eloquent集合,它们不返回查询构建器对象!
use Mpociot\Couchbase\Eloquent\Model as Eloquent; class User extends Eloquent { public function books() { return $this->embedsMany('Book'); } }
您可以通过动态属性访问嵌入的模型
$books = User::first()->books;
反向关系自动可用,您不需要定义此反向关系。
$user = $book->user;
插入和更新嵌入的模型与hasMany
关系类似
$book = new Book(['title' => 'A Game of Thrones']); $user = User::first(); $book = $user->books()->save($book); // or $book = $user->books()->create(['title' => 'A Game of Thrones'])
您可以使用嵌入的模型的save
方法来更新嵌入的模型
$book = $user->books()->first(); $book->title = 'A Game of Thrones'; $book->save();
您可以通过在关系上使用destroy
方法或在模型上使用delete
方法来删除嵌入的模型
$book = $user->books()->first(); $book->delete(); // or $user->books()->destroy($book);
如果您想在不接触数据库的情况下添加或删除嵌入的模型,可以使用associate
和dissociate
方法。要将更改最终写入数据库,请保存父对象
$user->books()->associate($book); $user->save();
与其他关系一样,embedsMany基于模型名称假设关系的本地键。您可以通过向embedsMany方法传递第二个参数来覆盖默认的本地键
return $this->embedsMany('Book', 'local_key');
嵌入的关系将返回嵌入项的集合,而不是查询构建器。在此查看可用的操作:https://laravel.net.cn/docs/master/collections
嵌套One关系
嵌套One关系类似于嵌套Many关系,但只嵌套单个模型。
use Mpociot\Couchbase\Eloquent\Model as Eloquent; class Book extends Eloquent { public function author() { return $this->embedsOne('Author'); } }
您可以通过动态属性访问嵌入的模型
$author = Book::first()->author;
插入和更新嵌套模型的工作方式类似于hasOne
关系
$author = new Author(['name' => 'John Doe']); $book = Books::first(); $author = $book->author()->save($author); // or $author = $book->author()->create(['name' => 'John Doe']);
您可以使用save
方法更新嵌套模型
$author = $book->author; $author->name = 'Jane Doe'; $author->save();
您可以通过这种方式替换嵌套模型为新的模型
$newAuthor = new Author(['name' => 'Jane Doe']); $book->author()->save($newAuthor);
MySQL关系
如果您正在使用混合的Couchbase和SQL配置,那么您很幸运!模型将根据相关模型类型自动返回Couchbase或SQL关系。当然,如果您想双向使用此功能,您的SQL模型需要使用Mpociot\Couchbase\Eloquent\HybridRelations
特质。请注意,此功能仅适用于hasOne、hasMany和belongsTo关系。
示例基于SQL的用户模型
use Mpociot\Couchbase\Eloquent\HybridRelations; class User extends Eloquent { use HybridRelations; protected $connection = 'mysql'; public function messages() { return $this->hasMany('Message'); } }
以及基于Couchbase的消息模型
use Mpociot\Couchbase\Eloquent\Model as Eloquent; class Message extends Eloquent { protected $connection = 'couchbase'; public function user() { return $this->belongsTo('User'); } }
查询缓存
您可以使用remember方法轻松缓存查询结果
$users = User::remember(10)->get();
参考: https://laravel.net.cn/docs/queries#caching-queries
查询日志
默认情况下,Laravel会在当前请求中记录所有已运行的查询。然而,在某些情况下,例如插入大量行时,这可能导致应用程序使用过多的内存。要禁用日志,您可以使用disableQueryLog
方法
DB::connection()->disableQueryLog();