imyangjin / yii2-mysql-json
通过使用 MySQL JSON 扩展 yii2-ActiveRecord 和 yii2-ActiveQuery 以简化操作。
Requires
- yiisoft/yii2: ~2.0.0
This package is auto-updated.
Last update: 2024-09-12 21:03:24 UTC
README
通过使用 MySQL JSON 扩展 yii2-ActiveRecord 和 yii2-ActiveQuery 以简化操作。
安装
推荐通过 composer 安装此扩展。
运行以下命令之一:
composer require --prefer-dist imyangjin/yii2-mysql-json
或者在您的 composer.json
文件的要求部分添加:
"imyangjin/yii2-mysql-json": "~1.0"
。
基本用法
您的模型文件必须扩展此扩展类;
use Imyangji\Yii2MysqlJson\ActiveRecordJson;
class YourModel extent ActiveRecordJson
{
}
然后,如果您想用它来搜索具有 JSON 的列。
jsonWhere
此函数类似于使用 Model::find()->where([Query::where()])
;列搜索为 column->"$.jsonColumn1.jsonColumn2..."
;
public function search()
{
YourModel::findJson()
->jsonWhere(['content->"$.en.content"' => 'who'])
->jsonWhere(['>', 'content->"$.en.content"' , 'who'])
}
jsonContainsWhere
此函数支持 MySQL 的 JSON_CONTAINS(target, candidate[, path])
;此查询与查询等效,但不同之处在于查询是包含关系,即字段包含值的值;列支持使用 '.' 分隔的 JSON 字段的多级字段;
public function search()
{
YourModel::findJson()
->jsonContainsWhere('content.en.content', 'who')
}
jsonExtractWhere
此函数支持查询中的 JSON_EXTRACT(json_doc, path[, path] ...); 列支持使用 '.' 分隔的 JSON 字段的多级字段;可以使用操作进行搜索。
public function search()
{
YourModel::findJson()
->jsonContainsWhere('content.en.content', 'who', '>')
}
jsonSelect
此函数支持查询选择中的 JSON_EXTRACT(json_doc, path) AS xx
;列支持使用 '.' 分隔的 JSON 字段的多级字段;
public function search()
{
YourModel::findJson()
->jsonSelect(['content.en.content', 'content.en.text' => 'tt']])
}