guifcoelho / immu-table
用于使用不可变ndjson模型代替常规SQL或NoSQL数据库,语法类似于Laravel Eloquent的包。
0.2.2
2019-07-15 20:05 UTC
Requires
- php: ~7.3
- illuminate/config: ^5.8
- symfony/finder: ^4.3
Requires (Dev)
- fzaninotto/faker: ^1.8
- illuminate/database: ^5.8
- illuminate/filesystem: ^5.8
- jaschilz/php-coverage-badger: ^2.0
- phpunit/phpunit: ^8
- symfony/var-dumper: ^4.3
- vlucas/phpdotenv: ^3.4
Suggests
- illuminate/database: Required to use pivot tables
- vlucas/phpdotenv: Required to use the env helper (^3.3).
README
ImmuTable
用于使用不可变ndjson模型代替常规SQL或NoSQL数据库,语法类似于Laravel Eloquent的包。
如果您的数据可能会更改,请勿使用此包。
安装
composer require guifcoelho/immu-table
如何使用
配置
配置类将查找一个config_path()
函数。此函数必须返回包含immutable.php
配置文件的配置文件存储库。
将immutable.php
配置文件从src/Config
复制并粘贴到您的配置文件夹中。
默认情况下,表将存储在storage/app/immutable/tables
。
引擎类将以数据块的形式加载数据表。您可以在配置文件中增加或减少chunk_size
。
声明您的模型
创建模型的方式与Laravel Eloquent相同
use guifcoelho\ImmuTable\Model; class Sample extends Model { protected $table = "table_example"; }
它将从表table_example.ndjson
加载数据,并根据所有字段设置。如果您想限制要加载的字段,只需包含受保护的数组$fields
use guifcoelho\ImmuTable\Model; class Sample extends Model { protected $fields = ['id', 'name', 'email']; }
如果您不想在toArray()
或toJson()
函数中返回某些字段,只需将它们的名称包含在$hidden
数组中
use guifcoelho\ImmuTable\Model; class Sample extends Model { protected $hidden = ['this', 'that']; }
如果您想将主键设置为除' id'之外的内容,请声明如下(请记住,主键必须是唯一的整数)
use guifcoelho\ImmuTable\Model; class Sample extends Model { protected $primary_key = 'not_id'; }
查询您的模型
您可以像在Laravel Eloquent中一样查询模型以获取数据
$query = SampleModel::where('id', 10)->first();
或,
$query = SampleModel::where('price', '>', 50)->first();
链式' where'子句
$query = SampleModel::where('price', '>', 50)->where('id', '<=', 10)->get();
或链式' orWhere'子句
$query = SampleModel::where('price', '>', 50) ->where('id', '<=', 10) ->orWhere('price', '<', 10) ->get();
声明关系
您可以像Laravel Eloquent一样在模型之间声明关系。请查看Model
类以了解哪些关系已实现。
use guifcoelho\ImmuTable\Model; use Sample2; use Sample3; use Sample4; use Sample5; class Sample1 extends Model { protected $table = "table_example"; public function owner(){ return $this->ImmuTable_belongsTo(Sample2::class [, $field, $field_in_parent_class]); } public function parents(){ return $this->ImmuTable_belongsToMany(Sample3::class [, $pivot_table, $field_in_pivot, $parent_field_in_pivot, $field, $field_in_parent]) } public function child(){ return $this->ImmuTable_hasOne(Sample4::class [, $field_in_child_model, $field]); } public function children(){ return $this->ImmuTable_hasMany(Sample5::class [, $field_in_child_models, $field]); } }
在上面的示例中,括号内的字段是可选的。请参阅以下更好的说明
ImmuTable_belongsTo
:您必须提供父类名称。如果需要,提供子模型中的外键名称,以及父模型中的相关字段名称。ImmuTable_belongsToMany
:您必须提供父类名称。如果需要,提供枢纽表名称、当前模型在枢纽表中的外键名称、父模型在枢纽表中的外键名称、当前模型的相关字段名称,以及父模型的相关字段名称。ImmuTable_hasOne
:您必须提供子类名称。如果需要,提供子模型中的外键名称以及父模型中的相关字段名称。ImmuTable_hasMany
:您必须提供子类名称。如果需要,提供子模型中的外键名称以及父模型中的相关字段名称。
贡献和测试
- 仅测试:
./vendor/bin/phpunit
- 测试和覆盖率报告:
composer tests-report-[linux|win]