darrylkuhn/dialect 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

darrylkuhn/dialect

Composer 安装命令:

composer require darrylkuhn/dialect

包简介

Provides JSON datatype support for the Eloquent ORM

README 文档

README

Build Status Code Climate Test Coverage

Dialect provides JSON datatype support for the Eloquent ORM. At this point this implementation is pretty bare bones and has been demonstrated to work with PostgreSQL and MySQL. There are lots of opportunities to enhance and improve. If you're interested in contributing please submit merge/pull requests.

Installation

Require this package in your composer.json file:

"darrylkuhn/dialect": "dev-master"

...then run composer update to download the package to your vendor directory.

Usage

The Basics

The feature is exposed through a trait called which allows you to define attributes on the model which are of the json datatype. When the model is read in it will parse the JSON document and set up getters and setters for each top level attribute making it easy to interact with the various attributes within the document. For example we could create a Photos model like this:

class Photo extends Eloquent
{
    use Eloquent\Dialect\Json;
    protected $jsonColumns = ['json_data'];
}

And then this:

$attr = json_decode($photo->json_data);
$attr->key = $value;
$photo->json_data = json_encode($attr);

becomes this:

$photo->key = value;

Also when calling the toArray() method the attributes are moved to the top level and the 'json_attributes' column is hidden. This essentially hides away the fact that you're using the json datatype and makes it look like we're working with attributes directly.

Relations

You can also establish relationships on a model like this (only supported in PostgreSQL):

public function user()
{
    return $this->hasOne( 'User', 'id', "json_data->>'user_id'" );
}

Structure Hinting

Sometimes you may have an empty or partially populated record in which case the trait cannot automatically detect and create getters/setters, etc... When getting or setting an attribute not previously set in the JSON document you'll get an exception. You have two choices to deal with this. You can hint at the full structure as in the example below:

class Photo extends Eloquent
{
    use Eloquent\Dialect\Json;
    protected $jsonColumns = ['json_data'];

    public function __construct()
    {
        parent::__construct();
        $this->hintJsonStructure( 'json_data', '{"foo":null}' );
    }
}

Once you create a hint you will be able to make calls to get and set json attributes e.g. $photo->foo = 'bar'; regardless of whether or not they are already defined in the underlying db record. Alternatly if you prefer not to hint structures then you may call setJsonAttribute(). For example if you defined a json column called "json_data" and wanted to set an attribute called 'fizz' so you could call:

$photo->setJsonAttribute( 'json_data', 'fizz', 'buzz' );

Showing/Hiding Attributes

One of the aims of the project is to make json attributes "first class" citizens of the model. This means by default we add the attributes to the models appends array so that when you call $model->toArray() or $model->toJson() the attribute shows up as a part of the structure like a normal attribute. By default we also hide away the json column holding the underlying data. Both of these settings can be changed using the showJsonColumns() and showJsonAttributes() as shown below:

class Photo extends Eloquent
{
    use Eloquent\Dialect\Json;
    protected $jsonColumns = ['json_data'];

    public function __construct()
    {
        parent::__construct();
        $this->showJsonColumns(true);
        $this->showJsonAttributes(false);
    }
}

darrylkuhn/dialect 适用场景与选型建议

darrylkuhn/dialect 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 245.28k 次下载、GitHub Stars 达 126, 最近一次更新时间为 2015 年 02 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 darrylkuhn/dialect 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 245.28k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 130
  • 点击次数: 15
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 126
  • Watchers: 12
  • Forks: 25
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-02-22