easy-swoole / orm

31 stars 27 forks source link

获取器无法默认设置额外字段 #178

Open Seele9 opened 3 years ago

Seele9 commented 3 years ago

easyswoole 3.3.7、orm组件版本号 [1.4.26]

设置了获取器,但是数据中没有额外的字段

排查情况和最小复现脚本


    /**
     * 控制器
     * @return bool
     * @throws Throwable
     */
    public function test()
    {
        $list = TestAModel::create()->all();
        foreach ($list as &$v) {
            $v->easyswoole = $v->easyswoole; // 有额外字段
            $v->toArray(); //没有额外字段
        }
        unset($v);
        $this->response()->write(json_encode($list, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
        $this->response()->withHeader('Content-type', 'application/json;charset=utf-8');
        $this->response()->withStatus(200);
        return true;
    }

    /**
     * 模型
     * @return array|mixed|null
![没有额外字段](https://user-images.githubusercontent.com/46363764/93038848-9edccb80-f678-11ea-9425-a59a5baf1b4e.png)
![有额外字段](https://user-images.githubusercontent.com/46363764/93038856-a13f2580-f678-11ea-9769-fdb0713b6011.png)

     * @throws Throwable
     */
    protected function getEasySwooleAttr($value, $data)
    {
        if ($data['uid']) {
            return TestModel::create()->where('id', $data['uid'])->scalar('name');
        }
        return '';
    }
Seele9 commented 3 years ago

TestModel

CREATE TABLE test ( id int(11) NOT NULL AUTO_INCREMENT, name varchar(255) DEFAULT NULL, age int(11) DEFAULT NULL, PRIMARY KEY (id), KEY name (name(191)) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4;

TestAModel

CREATE TABLE test_a ( id int(11) unsigned NOT NULL AUTO_INCREMENT, uid int(11) DEFAULT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4;