top-think / top-think/think-orm

设定$jsonType 后出现update()方法更新字段不存在的问题

Open
#334 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
437
Forks
188
PR merge metrics
No merged PRs in 30d

Description

设定了 JsonJob 类 JsonJob.php

namespace app\common\model;

use think\Model;

class JsonJob extends Model
{

    protected $jsonAssoc = true;
    protected $json = ['data'];
    // 设置JSON字段的类型
    protected $jsonType = [
        'data->vod_id'	=>	'int'
    ];
}

然后更新操作使用

    JsonJob::where('id', $findNewJob['id'])->update([
        'run_time' => $time,
        'from' => $from,
    ]);

这时候会提示

#0 [10500]DbException in [Builder.php line 164]()
数据表字段不存在:[run_time]
                $val = json_encode($val);
            }

            if (false !== strpos($key, '->')) {
                [$key, $name]  = explode('->', $key, 2);
                $item          = $this->parseKey($query, $key);
                $result[$item] = 'json_set(' . $item . ', \'$.' . $name . '\', ' . $this->parseDataBind($query, $key . '->' . $name, $val, $bind) . ')';
            } elseif (false === strpos($key, '.') && !in_array($key, $fields, true)) {
                if ($options['strict']) {
                    throw new Exception('fields not exists:[' . $key . ']');
                }
            } elseif (is_null($val)) {
                $result[$item] = 'NULL';
            } elseif (is_array($val) && !empty($val) && is_string($val[0])) {
                switch (strtoupper($val[0])) {
                    case 'INC':
                        $result[$item] = $item . ' + ' . floatval($val[1]);
                        break;
                    case 'DEC':

数据表字段 使用 $jsonJob->getTableFields()获取到字段为

array:8 [[▼]()
  0 => "id"
  1 => "type"
  2 => "data"
  3 => "deal_status"
  4 => "error_msg"
  5 => "run_time"
  6 => "from"
  7 => "ip"
]

如果JsonJob.php 类 注释掉

    // 设置JSON字段的类型
    protected $jsonType = [
        'data->vod_id'	=>	'int'
    ];

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with Builder.php around line 164 and the JsonJob.php model shown in the report. Reproduce the update using jsonType and compare the fields returned by getTableFields() with the fields accepted by the update path. Done means the reported run_time and from update no longer fail when jsonType is configured, while JSON field handling still works.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.