boldtrn/jsonb-bundle
Composer 安装命令:
composer require boldtrn/jsonb-bundle
包简介
A library adding the support for JSONB columns of PostgreSQL to Doctrine.
README 文档
README
Doctrine implemented the jsonb datatype with Doctrine DBAL 2.6. I recommend using the official Doctrine implementation. If you cannot upgrade feel free to use this bundle. It still works for me in my current production setting. I will upgrade to the doctrine implementation at some point in time, as well.
Doctrine Mapping Matrix
This bundle extends Doctrine to use the jsonb datatype that ships with Postgresql 9.4.
This bundle is fully compatible with Symfony, but you do not have to use Symfony (see the composer.json for dependencies).
Please make sure you have Postgresql with a version of at least 9.4 installed before using this bundle.
The Bundle allows to create Jsonb fields and use the @>,? and the #>> operator on the Jsonb field.
Other Operations can be easily added.
I recently discovered the power of NativeQueries (http://doctrine-orm.readthedocs.org/en/latest/reference/native-sql.html). Right now I only use NativeQueries when querying. An example is shown below.
Installation
Step 1: Download the Bundle
Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:
$ composer require "boldtrn/jsonb-bundle:~1.1"
Step 2: Add the new Types and Functions to the Config
# config.yml doctrine: dbal: types: jsonb: Boldtrn\JsonbBundle\Types\JsonbArrayType mapping_types: jsonb: jsonb orm: dql: string_functions: JSONB_AG: Boldtrn\JsonbBundle\Query\JsonbAtGreater JSONB_HGG: Boldtrn\JsonbBundle\Query\JsonbHashGreaterGreater JSONB_EX: Boldtrn\JsonbBundle\Query\JsonbExistence
Note: There were people having issues with the above configuration. They had the following exception:
[Symfony\Component\Config\Definition\Exception\InvalidConfigurationException]
Unrecognized options "dql" under "doctrine.orm"
This was fixed by changing the dql part in the following (add the entity_managers between orm and dql):
doctrine: orm: entity_managers: dql:
Step 3: Create a Entity and Use the Jsonb Type
/** * @Entity */ class Test { /** * @Id * @Column(type="string") * @GeneratedValue */ public $id; /** * @Column(type="jsonb") * * Usually attrs is an array, depends on you * */ public $attrs = array(); }
Step 4.1: Write a Repository Method using a NativeQuery
$q = $this ->entityManager ->createNativeQuery( " SELECT t.id, t.attrs FROM Test t WHERE t.attrs @> 'value' " , $rsm);
You only need to setup the $rsm ResultSetMapping according to the Doctrine documentation.
Step 4.2: Write a Repository Method that queries for the jsonb using the custom JSONB_FUNCTIONS
This example shows how to use the contains statement in a WHERE clause.
The = TRUE is a workaround for Doctrine that needs an comparison operator in the WHERE clause.
$q = $this ->entityManager ->createQuery( " SELECT t FROM E:Test t WHERE JSONB_AG(t.attrs, 'value') = TRUE " );
This produces the following Query:
SELECT t0_.id AS id0, t0_.attrs AS attrs1 FROM Test t0_ WHERE (t0_.attrs @> 'value') = true
This example shows how to query for a value that is LIKE %d%
The result could be data like:
id | attrs
----+--------------------------------------
4 | {"a": 1, "b": {"c": "abcdefg", "e": true}}
$q = $this ->entityManager ->createQuery( " SELECT t FROM E:Test t WHERE JSONB_HGG(t.attrs , '{\"b\",\"c\"}') LIKE '%d%' " );
This produces the following Query:
SELECT t0_.id AS id0, t0_.attrs AS attrs1 FROM Test t0_ WHERE (t0_.attrs #>> '{\"object1\",\"object2\"}') LIKE '%a%'
Further Information
The ? operator is implemented by calling its function jsonb_exists(column_name, value) since Doctrine will consider it a parameter placeholder otherwise. The same must be done if you want to implement ?| and ?& operators, using jsonb_exists_any(column_name, value) and jsonb_exists_all(column_name, value) respectively
boldtrn/jsonb-bundle 适用场景与选型建议
boldtrn/jsonb-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 823.77k 次下载、GitHub Stars 达 57, 最近一次更新时间为 2015 年 04 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「bundle」 「doctrine」 「postgresql」 「jsonb」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 boldtrn/jsonb-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 boldtrn/jsonb-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 boldtrn/jsonb-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
2lenet/EasyAdminPlusBundle
The bundle for easy using json-rpc api on your project
Provide a way to secure accesses to all routes of an symfony application.
DMS Meetup API Bundle, enables Meetup API clients in services
Make pimcore migration simple
Bundle Symfony DaplosBundle
统计信息
- 总下载量: 823.77k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 57
- 点击次数: 33
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-04-14