PHP语言ORM类库ThinkORM玩转JSON数据类型

ThinkORM是一个基于PHP和PDO的数据库中间层和ORM类库,以优异的功能和突出的性能著称,现已经支持独立使用,并作了升级改进,提供了更优秀的性能和开发体验,最新版本要求PHP8.0+。

安装

composer require topthink/think-orm

数据库 JSON

如果你的user表有一个info字段是JSON类型的(或者说你存储的是JSON格式,但并非是要JSON字段类型),你可以使用下面的方式操作数据。

JSON 数据写入

数据库写入json字段,直接通过数据的方式插入即可完成.

$data = [
    \\\'id\\\' => 2024,
    \\\'username\\\' => \\\'Tinywan\\\',
    \\\'info\\\' => [
        \\\'email\\\' => \\\'756684177@qq.com\\\', 
        \\\'gender\\\' => \\\'女\\\',
        \\\'age\\\' => 24
    ]
];

$res = think\\\\facade\\\\Db::table(\\\'user\\\')->json([\\\'info\\\'])->insert($data);

JSON 数据查询

查询整个JSON数据

如果要查询数据时,正确转换json数据,也需要设置json方法

$user = think\\\\facade\\\\Db::table(\\\'user\\\')->json([\\\'info\\\'])->find(1);
dump($user);

查询条件为JSON数据

如果将json字段数据作为查询条件,可以通过如下方式实现

$user = think\\\\facade\\\\Db::table(\\\'user\\\')
->json([\\\'info\\\'])
->where(\\\'info->email\\\',\\\'756684177@qq.com\\\')
->find();
dump($user);

由于JSON字段的属性类型并不会自动获取,所以,如果是整型数据查询的话,可以设置JSON字段类型,例如:

$user = think\\\\facade\\\\Db::name(\\\'user\\\')
 ->json([\\\'info\\\'])
    ->where(\\\'info->user_id\\\', 10)
 ->setFieldType([\\\'info->user_id\\\' => \\\'int\\\'])
 ->find();
dump($user);

JSON 数据更新

完整JSON数据更新

$data[\\\'info\\\'] = [
    \\\'email\\\' => \\\'tinywan@qq.com\\\',
    \\\'nickname\\\' => \\\'Tinywan\\\',
];
think\\\\facade\\\\Db::table(\\\'user\\\')
    ->json([\\\'info\\\'])
    ->where(\\\'id\\\', 2024)
    ->update($data);

单个JSON数据更新

$data[\\\'info->nickname\\\'] = \\\'Tinywan\\\';
think\\\\facade\\\\Db::name(\\\'user\\\')
    ->json([\\\'info\\\'])
    ->where(\\\'id\\\',1)
    ->update($data);

模型 JSON

这里指的JSON数据字段包括JSON类型以及JSON格式数据(但并不是JSON类型字段)。

User模型类

<?php
namespace app\\\\model;

use think\\\\Model;
class User extends Model
{
 // 设置json类型字段
 protected $json = [\\\'info\\\'];
}

定义后,可以进行如下JSON数据操作。

写入JSON数据

使用数组方式写入JSON数据:

$user = new User;
$user->name = \\\'Tinywan\\\';
$user->info = [
    \\\'email\\\'    => \\\'Tinywan@qq.com\\\',
    \\\'nickname \\\'=> \\\'阿克苏\\\',
];
$user->save();

使用对象方式写入JSON数据

$user = new User;
$user->name = \\\'Tinywan\\\';
$info = new \\\\StdClass();
$info->email = \\\'Tinywan@qq.com\\\';
$info->nickname = \\\'阿克苏\\\';
$user->info = $info;
$user->save();

查询JSON数据

$user = User::find(1);
echo $user->name; // Tinywan
echo $user->info->email; // Tinywan@qq.com
echo $user->info->nickname; // 阿克苏

查询条件为JSON数据

$user = User::where(\\\'info->nickname\\\', \\\'阿克苏\\\')->find();
echo $user->name; // Tinywan
echo $user->info->email; // Tinywan@qq.com
echo $user->info->nickname; // 阿克苏

更新JSON数据

$user = User::find(1);
$user->name = \\\'Tinywan\\\';
$user->info->email = \\\'Tinywan@qq.com\\\';
$user->info->nickname = \\\'Tinywan\\\';
$user->save();

如果设置模型的JSON数据返回数组,那么更新操作需要调整如下。

$user = User::find(1);
$user->name = \\\'Tinywan\\\';
$info[\\\'email\\\'] = \\\'Tinywan@qq.com\\\';
$info[\\\'nickname\\\'] = \\\'Tinywan\\\';
$user->info = $info;
$user->save();

更多:https://blog.csdn.net/mayidream/article/details/105083871

JSON 对象查询

rest_day_rule 字段存储数据结构如下

{\\\"start_time\\\": \\\"09:00\\\"}

原生SQL查询

SELECT * FROM attendance_group WHERE rest_day_rule->\\\'$.start_time\\\' = \\\'09:00\\\'

ORM模型查询

$user = think\\\\facade\\\\Db::table(\\\'attendance_group\\\')
->json([\\\'rest_day_rule\\\'])
->where(\\\'rest_day_rule->start_time\\\',\\\'09:00\\\')
->find();

JSON 数组查询

config 字段存储数据结构如下

[
    {
        \\\"shift_id\\\": 1,
        \\\"is_checked\\\": 1,
        \\\"shift_name\\\": \\\"通用班次\\\",
        \\\"shift_time\\\": \\\"09:00-18:00\\\",
        \\\"worker_name\\\": \\\"周一\\\"
    },
    {
        \\\"shift_id\\\": 2,
        \\\"is_checked\\\": 1,
        \\\"shift_name\\\": \\\"通用班次\\\",
        \\\"shift_time\\\": \\\"09:00-18:00\\\",
        \\\"worker_name\\\": \\\"周二\\\"
    }
]

原生SQL查询

SELECT * FROM attendance_group_time WHERE JSON_CONTAINS(config, JSON_OBJECT(\\\'shift_id\\\',1))

ORM模型查询

$shiftId = 1;
$attendanceGroupTime = AttendanceGroupTimeModel::json([\\\'config\\\'])
->whereRaw(\\\'JSON_CONTAINS(config, JSON_OBJECT(\\\"shift_id\\\",\\\' . $shiftId . \\\'))\\\')
->findOrEmpty();

AttendanceGroupTimeModel 模型类

class AttendanceGroupTimeModel extends BaseModel
{
    /** 设置当前模型对应的完整数据表名称 */
    protected $table = \\\'attendance_group_time\\\';
    
    /** 设置当前模型对应的JSON字段 */
    protected $json = [\\\'config\\\'];
}

原创文章,作者:网络技术联盟站,如若转载,请注明出处:https://www.sudun.com/ask/49743.html

(0)
网络技术联盟站的头像网络技术联盟站
上一篇 2024年5月7日
下一篇 2024年5月7日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注