dragosmocrii / model-meta
保存和检索任何模型的元设置。
Requires
- php: >=5.6
- laravel/framework: ~5.3
Requires (Dev)
- orchestra/testbench: ~3.0
- phpunit/phpunit: ~5.0
This package is not auto-updated.
Last update: 2024-09-28 19:38:17 UTC
README
介绍
Laravel 5.3 的 Model Meta 包允许您轻松存储和检索任何模型的元数据。该包实现了属性包模式,旨在帮助您处理需要存储各种模型属性(元数据)但将属性添加到模型中不是可行选项的情况。
安装
Composer
composer require dragosmocrii/model-meta
Laravel 提供者
接下来,您需要将 ModelMetaServiceProvider 添加到您的 config/app.php 文件中的 providers
数组。
/* * Package Service Providers... */ DragoshMocrii\ModelMeta\ModelMetaServiceProvider::class,
迁移和发布资源
Model Meta 需要设置其表。要这样做,请运行 php artisan migrate
。
接下来,运行 php artisan vendor:publish --provider="DragoshMocrii\ModelMeta\ModelMetaServiceProvider"
以将供应商文件复制到您的应用程序。
配置
模型设置
要添加 Model Meta 功能,您需要将 MetableFunctionality
特性添加到您的模型中,如下所示
use DragoshMocrii\ModelMeta\Traits\MetableFunctionality; class Client extends Model { use MetableFunctionality; }
用法
设置元数据
bool metaSet(string $key, mixed $value, bool $force = true)
如果 $force 设置为 false,则元数据将在父模型保存时保存到数据库。否则,元数据将立即保存。
注意:$key 需要是 字符串,否则将抛出异常。
在新的模型上设置元数据
$model = new MetableModel; $model->metaSet( 'key', 'value' ); //at this time, the meta is not saved to the DB yet, because the model does not have a foreign key set yet $model->save(); // meta will be saved when the model is saved
在现有的模型上设置元数据
$model = MetableModel::findOrFail( 1 ); $model->metaSet( 'key', 'value' ); //this meta will be saved to DB instantly
一次设置多个元数据
bool metaSetMany(array $values, bool $force = true)
如果 $force 设置为 false,则元数据将在父模型保存时保存到数据库。否则,元数据将立即保存。
注意:$values 参数需要是一个关联数组,其中键是 字符串。如果不满足这些条件,将抛出异常。
$model = new MetableModel; $model->metaSetMany( [ 'key' => 'value', 'foo' => 'bar' ] ); //at this time, the meta is not saved to the DB yet, because the model does not have a foreign key set yet $model->save(); // meta will be saved when the model is saved
检索元数据
mixed metaGet(string|array $keys, null|mixed $default = null)
注意:$default 参数仅在检索单个元数据时生效。
获取单个元数据
$model = MetableModel::findOrFail( 1 ); $meta_value = $model->metaGet( 'foo', 'bar' ); //returns the value of meta[foo] or 'bar' if meta[foo] does not exist
获取多个元数据
$model = MetableModel::findOrFail( 1 ); $metas = $model->metaGet( [ 'key', 'foo' ] ); //will return an associative array containing the values for the respective meta keys. if meta does not exist, it will be assigned with a null value
获取所有元数据
mixed metaAll()
$model = MetableModel::findOrFail( 1 ); $all_meta = $model->metaAll(); //returns an array containing all meta for the current model
删除元数据
metaRemove(string|array $keys)
注意:如果模型缺少外键,元数据将从数据库中立即删除。
删除单个元数据
$model = MetableModel::findOrFail( 1 ); $model->metaRemove( 'key' );
删除多个元数据
$model = MetableModel::findOrFail( 1 ); $model->metaRemove( [ 'key', 'foo' ] );
检查元数据
bool metaExists(string|array $keys, bool $return_missing = false)
如果 $return_missing 为 true,则此函数将返回一个包含缺少元数据键的数组。如果没有缺少元数据,则返回一个空数组。
检查单个元数据
$model = MetableModel::findOrFail( 1 ); $meta_exists = $model->metaExists( 'key' );
检查多个元数据
$model = MetableModel::findOrFail( 1 ); $meta_exists = $model->metaExists( [ 'key', 'foo' ] ); //return true/false if $return_missing is false. Otherwise, returns an array containing the keys of the missing meta
形态映射
默认情况下,Laravel 将使用完全限定的类名来存储相关模型的类型。如果您想将应用程序内部结构与应用程序数据库解耦,应定义一个形态映射。因此,如果您更改模型的类名或扩展模型而希望不丢失相关元数据,您只需更改形态映射。
要更改形态映射,您需要编辑 model_meta.morph_map
配置设置,例如
'morph_map' => [ 'posts' => App\Post::class, 'videos' => App\Video::class, ]
使用单个元数据获取预加载所有元数据
在某些情况下,你可能在代码中多次调用metaGet。为了避免每次都执行数据库查询,你可以告诉模型元数据在检索单个元数据时预加载所有模型元数据,这样后续获取单个元数据将使用缓存。
默认情况下,模型元数据将预加载元数据。如果你想禁用此功能,可以将配置model_meta.preload_on_get
设置为false
。
模型元数据 & Laravel
模型元数据使用多态关系来实现其功能。如果你需要构建复杂查询,请记住这一点。
例如,你可以使用默认的Laravel方法获取模型元数据
$model = MetableModel::findOrFail( 1 ); $metas = $model->meta()->get()->toArray(); $single_meta = $model->meta()->where( 'key', 'keyname' )->get()->toArray();
已知问题
- 设置元数据将为每个元数据执行2次查询。这是因为模型元数据使用Laravel的
updateOrCreate
方法,该方法对数据库执行2次查询。 - 在每次检索元数据时,都会执行数据库查询。将通过在模型上预加载元数据来解决这个问题。