TP5.0 关联模型新增

浏览:845 发布日期:2018/09/07 分类:求助交流 关键字: 模型,关联
 //一对多关联 每条规则有多个关键字
    public function ruleKeyword(){
        return $this->hasMany('WechatRuleKeyword','rid','id');
    }

    //一对多关联,每条规则有多个关键字回复内容

    public function replyBasic(){
        return $this->hasMany('WechatBasicReply','rid','id');
    }
 public  function addRule($wechat_id,$data=[]){
        try{
            self::startTrans();
            $reply_content = [];
            foreach ($data['newKeywordList'] as $key=>$value){
                $data['newKeywordList'][$key]['uniacid'] = $wechat_id;
                $data['newKeywordList'][$key]['module'] = $data['module']==='keyword'?'reply':$data['module'];
                $data['newKeywordList'][$key]['displayorder'] = $data['keywordInfo']['displayorder'];
                $data['newKeywordList'][$key]['status'] = $data['keywordInfo']['status'];
            }
            foreach ($data['keywordContent'] as $key=>$content){
                switch ($content['type']){
                    case 'basic':
                        $reply_content[]=$content['content'];
                        break;
                    default:
                        break;
                }
            }
            $rule = [
                'uniacid'=>$wechat_id,
                'name'=>$data['keywordInfo']['name'],
                'module'=>$data['module']==='keyword'?'reply':$data['module'],
                'displayorder' =>$data['keywordInfo']['displayorder'],
                'status' =>$data['keywordInfo']['status'],
                'containtype'=>!empty($data['containtype'])?substr($data['containtype'],0,strlen($data['containtype'])-1):''
            ];
            $blog = self::create($rule);
          //  dump($data['newKeywordList']);exit;
            if($blog){
                $blog->ruleKeyword()->saveAll($data['newKeywordList']);
                if(!empty($reply_content)){
                    $blog->replyBasic()->saveAll($reply_content);
                }
            }
            self::commit();
            return ['code'=>1,'msg'=>'添加成功'];
        }catch (ErrorException $e){
            self::rollback();
            return ['code'=>0,'msg'=>$e->getMessage()];
        }
    }
这里我关联新增时报错 :Illegal string offset 'rid'
最佳答案
评论( 相关
后面还有条评论,点击查看>>