Thinkphp中模型关联骆峰法 及自动转JSON 数组 对象 自动转换

在model里面关联

    public function startTopics(){
        //只对应实际开会的议题
        return $this->hasMany(TopicsModel::class,'start_meeting_id','id')->where('del_time',0);
        
    }
    public function topics(){
        //只对应实际开会的议题
        return $this->hasMany(TopicsModel::class,'meeting_id','id')->where('del_time',0);
    }
    

上面真实情况下 public function startTopics (){}是可以显示 前端模板中也可以直接调用
但是public function strat_topics(){}来关联是不可以的不能有 下划线!必须驼峰法显示,坑了我一天!!!

mysql里面的含有JSON字符串的键可以自动转为对象 数组用来简化取出是少用一次json_decode() 自动转换

转化为Object

protected $json = ['score_text']; 
#转为object

转化为数组 额外获取器

protected $json = ['score_text']; 
#转为object

    // 强制转数组(TP6 唯一有效方案)
    public function getScoreTextAttr($value)
    {
        return json_decode($value, true);
    }
此条目发表在None分类目录。将固定链接加入收藏夹。

发表回复