mmanos / laravel-metable
Laravel 4 模型的元包。
Requires
- php: >=5.4.0
- illuminate/support: ~4.1
This package is not auto-updated.
Last update: 2024-09-28 17:12:32 UTC
README
此包为您的 Laravel 应用程序添加了元支持。您可以将其配置为将元附加到任何现有的 Eloquent 模型。
安装
Composer
将此添加到您的 composer.json 文件中的 require 对象
"mmanos/laravel-metable": "dev-master"
之后,运行 composer install 以安装包。
服务提供者
在您的 app 配置文件中注册 Mmanos\Metable\MetableServiceProvider
。
配置
元迁移和模型
首先,您需要发布一个 metas
表和一个 Meta
模型。此表将保存由您的 metable 模型创建的所有元数据的摘要。
$ php artisan laravel-metable:metas metas
注意:修改此调用的最后一个参数以更改表/模型名称。
注意:如果您想为不同类型的内容保留元数据的分离,例如,可以发布所需的元表数量。
可元迁移
接下来,为要附加元数据的内容类型发布迁移。您可以附加元数据到任何类型的内容。例如,如果您想能够为 users
表和 blog_posts
表添加元数据,为每个表运行此迁移一次。
$ php artisan laravel-metable:metable user_metas
运行迁移
一旦创建了迁移,只需运行 migrate
命令。
模型设置
接下来,将 Metable
特性添加到每个 metable 模型定义中
use Mmanos\Metable\Metable; class User extends Eloquent { use Metable; }
然后,您需要指定元模型以及与您的模型一起使用的 metable 表
class User extends Eloquent { protected $meta_model = 'Meta'; protected $metable_table = 'user_metas'; }
同步自定义属性
有时,您可能希望将内容表中的某些相同字段同步到 metable 表记录中。这将允许您在查询 metable 表时根据这些属性进行筛选和排序。幸运的是,此系统将在您定义的字段有任何更改时自动同步这些字段到 metable 表记录。
要开始,请 修改 metable 迁移文件 以包含您额外的字段。
然后,告诉您的模型它需要同步哪些字段
class User extends Eloquent { protected $metable_table_sync = ['company_id', 'created_at', 'updated_at', 'deleted_at']; }
现在,每次创建或更新模型时,这些字段都会同步到内容项的所有 metable 表记录。
同步删除的内容
当删除某项内容时,此包将自动删除 metable 表中该内容的所有记录。
如果您正在使用 SoftDeletingTrait
并且您将 deleted_at
列同步到 metable 表记录中,当删除该内容时,此包将自动软删除该内容的所有 metable 表记录。如果内容被恢复,则 metable 表记录也会被恢复。
处理元数据
设置内容元数据
为现有内容设置元值
$user->setMeta('employer', 'Company, Inc.');
或一次设置多个元数据
$user->setMeta([ 'employer' => 'Company, Inc.', 'employed_for_years' => 10, ]);
注意:如果内容已具有元数据,则现有值将更新。
取消设置内容元数据
同样,您可以从现有内容中取消设置元数据
$user->unsetMeta('employer');
或一次取消设置多个元数据
$user->unsetMeta('employer', 'employed_for_years'); // or $user->unsetMeta(['employer', 'employed_for_years']);
注意:如果内容没有请求的元数据,系统不会抛出错误。
检查元数据
检查内容是否有元数据
if ($user->hasMeta('employer')) { }
检索元数据
要检索内容上的元值,请使用 meta
方法
$employer = $user->meta('employer');
如果没有设置,请指定默认值
$employer = $user->meta('employer', 'Unemployed');
您还可以一次性检索多个元数据,并返回一个值数组
$employer = $user->meta(['employer', 'employed_for_years']);
检索所有元数据
要检索与某个内容相关联的所有元数据,请使用 metas
关系
$metas = $user->metas;
检索所有元数据的数组
要检索与某个内容相关联的所有元数据并将其作为数组返回,请使用 metasArray
方法
$metas = $user->metasArray();
从元数据查询内容
执行查询
现在假设您想查询具有给定元数据的所有内容
$users = User::withMeta('employer')->take(10)->get();
或者可选地指定匹配的元数据值
$users = User::whereMeta('employer', 'Company, Inc.')->take(10)->get();
或者可选地指定匹配的元数据值和运算符
$users = User::whereMeta('employed_for_years', '>', '5')->take(10)->get();
这些查询扩展了您熟悉的 QueryBuilder
类,因此所有这些方法都适用
$users = User::whereMeta('employer', 'Company, Inc.') ->where('meta_created_at', '>', '2015-01-01 00:00:00') ->with('company') ->orderBy('meta_created_at', 'desc') ->paginate(10);
注意:QueryBuilder 对象上的
update
和delete
方法不适用于这些查询。
您可以查询具有给定元数据中的任何内容
$users = User::withAnyMeta('company', 'employed_for_years')->get(); // or $users = User::withAnyMeta(['company', 'employed_for_years'])->get();
或者查询具有给定元数据且与给定值匹配的内容
$users = User::whereAnyMeta([ 'company' => 'Company, Inc.', 'employed_for_years' => '10' ])->get();
注意:如果您的查询匹配数千条或更多记录,则
withAnyMeta
和whereAnyMeta
查询的性能可能会降低。
您还可以组合多个过滤器
// Fetch all users who have the 'agent' meta and who have 'company' or 'employed_for_years'. $users = User::whereMeta('agent', '1')->withAnyMeta('company', 'employed_for_years')->get();
元数据上下文
有时您可能想要将应用程序的一些自定义上下文与元数据(摘要)表记录相关联。例如,假设您有一个 companies
表和一个 users
表,并且每个用户都属于一个公司。现在您还希望将每个元数据记录与公司相关联,以便您可以检索每个公司使用的所有元数据。为了做到这一点,我们必须告诉这个包意识到这个公司上下文并相应地修改其查询。
要开始,请确保您已修改 元数据迁移 以包含任何上下文字段(在这个例子中是 company_id
)。如果需要,您可能还需要更新唯一索引。
然后通过添加 metaContext
方法修改您的 metable 模型
class User extends Eloquent { public function metaContext() { return $this->company; } }
接下来修改您的 Meta
模型(或配置期间指定的任何名称)以应用任何上下文
class Meta extends Eloquent { public static function applyQueryContext($query, $context) { $query->where('company_id', $context->id); } public static function applyModelContext($model, $context) { $model->company_id = $context->id; } }
applyQueryContext
方法将调整此包使用的任何元数据查询,以过滤 company_id
。
在创建新的 Meta
记录时调用 applyModelContext
方法,并应设置任何必需的上下文字段。
最后,在执行查询时,指定要应用的上下文
$users = User::withMeta('employer')->withMetaContext($company)->take(10)->get();