求助 版主:官方研发技术组
模块表单中些批量审核通过方法,怎么关联主题中length的字段值
类型:迅睿CMS 更新时间:2021-12-10 17:18:20 模块表单 return
//我在模块表单中些批量审核通过方法,审核完后更新关联主题中length的字段值,现在测试完,数据表只更新一个所选内容的字数,不知道哪里除了问题。。




// 后台批量审核
protected function _Admin_Status() {
 $tid = intval(\Phpcmf\Service::L('input')->get('tid'));
 $ids = \Phpcmf\Service::L('input')->get_post_ids();
 if (!$ids) {
  $this->_json(0, dr_lang('所选数据不存在'));
 }
 
 // 格式化
 $in = [];
 foreach ($ids as $i) {
  $i && $in[] = intval($i);
 }
 if (!$in) {
  $this->_json(0, dr_lang('所选数据不存在'));
 }
 
 $rows = \Phpcmf\Service::M()->db->table($this->init['table'])->whereIn('id', $in)->get()->getResultArray();
 if (!$rows) {
  $this->_json(0, dr_lang('所选数据不存在'));
 }
 
 foreach ($rows as $row) {
  if ($row['status'] != 1) {
   if ($tid) {
    // 拒绝
    $this->_verify_refuse($row);
   } else {
    // 通过
    
    //本章字数
    $slength = $row['length'];
    //书籍原字数
    $clength = \Phpcmf\Service::L('vwbook',APP_DIR)->cdata($row['cid'],'length');
    
    //|--更新书籍字数
    ///*
    \Phpcmf\Service::M()->table(dr_module_table_prefix(APP_DIR))->update($row['cid'], [
     'length' => $slength + $clength
    ]);
    //*/
    var_dump($row);
    //$this->_verify($row);
   }
   $this->content_model->update_form_total($row['cid'], $this->form['table']);
  }
 }
 
 //$this->_json(1, dr_lang('操作成功'));
}


vwbook->cdata方法(此项数据无误,我一并贴出来方便寻找错误根源):

// 用于列表关联字段
 public function cdata($cid, $field = 'id') {
  if (!$cid) {
   return dr_lang('未关联');
  }
  $mid = defined('MOD_DIR') ? MOD_DIR : '';
  $this->cid_data[$cid] = isset($this->cid_data[$cid]) && $this->cid_data[$cid] ? $this->cid_data[$cid] : \Phpcmf\Service::M()->table_site($mid)->get($cid);
  return $this->cid_data[$cid] ? $this->cid_data[$cid][$field] : dr_lang('关联主题不存在');
 }
回帖
  • 天天向上
    #1楼    天天向上
    2021-12-10 15:26:35
    Chrome 0
    思路有问题,你这种就不是很准确了,先查询出全部的子内容的列表,然后统一计算相加,然后在更新到主表中,不要你这样一条条的加了
  • zooxen
    #2楼    zooxen
    2021-12-10 15:31:37
    Edge 0
    天天向上 我是直接复制官方模块表单的方法过来修改的
  • zooxen
    #3楼    zooxen
    2021-12-10 15:33:03
    Edge 0
    天天向上 所以我是想弄明白,现在更新为啥出现问题,这个批量其实用的很少,审核都需要进去看内容的。这个功能不需要考虑性能问题
  • zooxen
    #4楼    zooxen
    2021-12-10 16:03:31
    Edge 0
    天天向上
    	foreach ($rows as $row) {
    		
    		if ($row['status'] != 1) {
    			if ($tid) {
    				// 拒绝
    				
    				$this->_verify_refuse($row);
    			} else {
    				// 通过
    				
    				$vwsons = [
    					$row['cid'] => $vwson = [
    						$row['id'] => $row['length']
    					],
    				];
    			}
    			$this->content_model->update_form_total($row['cid'], $this->form['table']);
    		}
    	}
    我组合数组,但是得到的结果还是一条。所以跟你说的这个问题不相干啊。官方还给你点赞。。。。问题的点没有找到不是?
  • zooxen
    #5楼    zooxen
    2021-12-10 17:18:06
    Edge 0
    过来结帖,但是那个foreach为啥没起作用还是没搞明白哪里除了问题,换成以下代码可以解决。
    	//组合数组 [cid]=>提交字数
    	foreach ($rows as $row) {
    		
    		if ($row['status'] != 1) {
    			if ($tid) {
    				// 拒绝
    				$this->_verify_refuse($row);
    			} else {
    				// 通过
    				$vwsons[$row['cid']] = $clengths[$row['cid']] = $clengths[$row['cid']] + $row['length'];
    				$this->_verify($row);
    			}
    			$this->content_model->update_form_total($row['cid'], $this->form['table']);
    		}
    	}
    	
    	//获取cid数据
    	$vwkeys = array_keys($vwsons);
    	
    	//根据cid分别累加字数
    	foreach ($vwkeys as $vwkey) {
    		
    		//书籍原字数
    		$clength = \Phpcmf\Service::L('vwbook',APP_DIR)->cdata($vwkey,'length');
    		//更新书籍字数
    		\Phpcmf\Service::M()->table(dr_module_table_prefix(APP_DIR))->update($vwkey, [
    		'length' => $clength + $vwsons[$vwkey]
    		]);
    		
    	}
    	
    	$this->_json(1, dr_lang('操作成功'));
    满意答案
  • zooxen
    #6楼    zooxen
    2021-12-10 17:18:20
    Edge 0
    @zooxen:完结之后本帖将不再提供回复