#178·orm

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

Author: Jay-L1Created Sep 14, 2020Updated Sep 15, 2020

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 '';
    }