author:咔咔
wechat:fangkangfk
案例:专题添加视频不可重复
注释写的这么清晰就不需要在解释了
/** * author:咔咔 * * 批量给专题添加视频 */ public function batchAddVideo() { $param = $this->request->param(); if(empty($param)){ return ajaxReturn(PARAM_ERROR); } // 已存在的视频 $existingData = $this->subjectListModel->existingData($param); // 批量添加的视频id $addVideoId = $param['ids']; // 将批量添加的视频索引与值相等 foreach($addVideoId as $k=>$v){ $datas[$v] = $v; } if(empty($existingData)){ foreach($datas as $key=>$val){ $data['vs_id'] = $param['vs_id']; $data['vi_id'] = $val; $result = $this->subjectListModel->insert($data); } if($result){ return ajaxReturn(SUCCESS); } } // 将已将存在的视频索引与值相等 foreach($existingData as $k=>$v){ $existencetVideoId[$v] = $v; } // 移除已经存在的视频id foreach($datas as $key => $val){ if(array_key_exists($key,$existencetVideoId)){ unset($datas[$key]); } } // 容错当全部数据都存在的时候,datas为空 if(empty($datas)){ return ajaxReturn(EXISTENCE); } foreach($datas as $key=>$val){ $data['vs_id'] = $param['vs_id']; $data['vi_id'] = $val; $result = $this->subjectListModel->insert($data); } if($result){ return ajaxReturn(SUCCESS); } }