承接 mmanos/laravel-metable 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mmanos/laravel-metable

Composer 安装命令:

composer require mmanos/laravel-metable

包简介

A meta package for Laravel 4 models.

README 文档

README

This package adds meta support to your Laravel application. You can configure it to attach meta to any of your existing Eloquent models.

Installation

Composer

Add this to your composer.json file, in the require object:

"mmanos/laravel-metable": "dev-master"

After that, run composer install to install the package.

Service Provider

Register the Mmanos\Metable\MetableServiceProvider in your app configuration file.

Configuration

Metas Migration and Model

First you'll need to publish a metas table and a Meta model. This table will hold a summary of all meta created by your metable models.

$ php artisan laravel-metable:metas metas

Note: Modify the last parameter of this call to change the table/model name.

Note: You may publish as many meta tables as you need, if you want to keep the meta separate for different types of content, for example.

Metable Migration

Next, publish a migration for each type of content you want to attach meta to. You may attach meta to as many types of content as you wish. For example, if you want to be able to add meta to both a users table and a blog_posts table, run this migration once for each table.

$ php artisan laravel-metable:metable user_metas

Run Migrations

Once the migration has been created, simply run the migrate command.

Model Setup

Next, add the Metable trait to each metable model definition:

use Mmanos\Metable\Metable;

class User extends Eloquent
{
	use Metable;
}

Then you need to specify the meta model as well as the metable table to use with your model:

class User extends Eloquent
{
	protected $meta_model = 'Meta';
	protected $metable_table = 'user_metas';
}

Syncing Custom Attributes

Sometimes you will want to have some of the same fields in your content table synced to the metable table records. This will allow you to filter and sort by these attributes when querying the metable table. Luckily this system will automatically sync any fields you define to the metable table records any time there are changes.

To get started, modify the metable migration file to include your additional fields.

Then, tell your model which fields it needs to sync:

class User extends Eloquent
{
	protected $metable_table_sync = ['company_id', 'created_at', 'updated_at', 'deleted_at'];
}

Now every time you create or update a model, these fields will by synced to all metable table records for the piece of content.

Syncing Deleted Content

This package will automatically delete all metable table records for a piece of content when that piece of content is deleted.

If you are using the SoftDeletingTrait and you are syncing the deleted_at column to your metable table records, this package will automatically soft-delete all metable table records for a piece of content when that piece of content is deleted. If the content is restored, then the metable table records are restored as well.

Working With Meta

Setting Content Meta

To set a meta value on an existing piece of content:

$user->setMeta('employer', 'Company, Inc.');

Or set multiple metas at once:

$user->setMeta([
	'employer' => 'Company, Inc.',
	'employed_for_years' => 10,
]);

Note: If a piece of content already has a meta the existing value will be updated.

Unsetting Content Meta

Similarly, you may unset meta from an existing piece of content:

$user->unsetMeta('employer');

Or unset multiple metas at once:

$user->unsetMeta('employer', 'employed_for_years');
// or
$user->unsetMeta(['employer', 'employed_for_years']);

Note: The system will not throw an error if the content does not have the requested meta.

Checking for Metas

To see if a piece of content has a meta:

if ($user->hasMeta('employer')) {
	
}

Retrieving Meta

To retrieve a meta value on a piece of content, use the meta method:

$employer = $user->meta('employer');

Or specify a default value, if not set:

$employer = $user->meta('employer', 'Unemployed');

You may also retrieve more than one meta at a time and get back an array of values:

$employer = $user->meta(['employer', 'employed_for_years']);

Retrieving All Metas

To fetch all metas associated with a piece of content, use the metas relationship:

$metas = $user->metas;

Retrieving an Array of All Metas

To fetch all metas associated with a piece of content and return them as an array, use the metasArray method:

$metas = $user->metasArray();

Querying for Content from Meta

Performing Queries

Now let's say you want to query for all content that has a given meta:

$users = User::withMeta('employer')->take(10)->get();

Or optionally specify the meta value to match:

$users = User::whereMeta('employer', 'Company, Inc.')->take(10)->get();

Or optionally specify the meta value and operator to match:

$users = User::whereMeta('employed_for_years', '>', '5')->take(10)->get();

These queries extend the same QueryBuilder class that you are used to working with, so all of those methods work as well:

$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);

Note: The update and delete methods on a QueryBuilder object do not work for these queries.

You may query for content that has any of the given meta:

$users = User::withAnyMeta('company', 'employed_for_years')->get();
// or
$users = User::withAnyMeta(['company', 'employed_for_years'])->get();

Or query for content that has any of the given meta that matches the given values:

$users = User::whereAnyMeta([
	'company' => 'Company, Inc.',
	'employed_for_years' => '10'
])->get();

Note: Query performance can be reduced for the withAnyMeta and whereAnyMeta queries if your queries match thousands of records or more.

And you may combine multiple filters:

// 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();

Meta Contexts

Sometimes you might want to associate your metas (summary) table records with some custom context for your application. For example, say you have a companies table and a users table and each user belongs to a company. And now you also want to associate each meta record with a company allowing you to fetch all meta used by each individual company. In order to do so, we have to tell this package to be aware of this company context and modify it's queries accordingly.

To get started, make sure you modify your metas migration to include any context fields (company_id, in this case). You might also need to update the unique index, if necessary.

Then modify your metable model by adding a metaContext method:

class User extends Eloquent
{
	public function metaContext()
	{
		return $this->company;
	}
}

Next modify your Meta model (or whatever name you specified during configuration) to apply any contexts:

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;
	}
}

The applyQueryContext method will adjust any meta queries used by this package to filter on company_id.

The applyModelContext method is called when creating a new Meta record and should set any required context fields.

Finally, when performing queries, specify the context to apply:

$users = User::withMeta('employer')->withMetaContext($company)->take(10)->get();

mmanos/laravel-metable 适用场景与选型建议

mmanos/laravel-metable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.31k 次下载、GitHub Stars 达 14, 最近一次更新时间为 2015 年 01 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「model」 「laravel」 「meta」 「eloquent」 「metas」 「metable」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 mmanos/laravel-metable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 mmanos/laravel-metable 我们能提供哪些服务?
定制开发 / 二次开发

基于 mmanos/laravel-metable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2.31k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 14
  • 点击次数: 22
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 14
  • Watchers: 3
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-01-25