/** * 删除指定用户发布的社区资讯信息. * * @access public * @param integer $userId 用户 ID. * @param array $ids 分类信息 ID. * @param boolean $deletedCollect 删除收藏, 默认 FALSE. * @return boolean 删除成功返回 TRUE, 删除失败返回 FALSE; 出错时抛异常. */ public function remove($userId, array $ids, $deletedCollect = FALSE) { if (empty($ids) || empty($userId)) { throw new \Exception(Message::get(Message::PARAM_ERROR), Message::PARAM_ERROR); } // 查询出用户发布的社区资讯信息 ID. $map = ['userID' => $userId, 'id' => ['IN', $ids]]; $ids = $this->field('id')->where($map)->select(); $ids = NULL === $ids || FALSE === $ids ? [] : $ids; if (empty($ids)) { throw new \Exception(Message::get(Message::PARAM_ERROR), Message::PARAM_ERROR); } // 将待删除的 id 转换成一维数组. $deleteIds = []; foreach ($ids as $v) { $deleteIds[] = $v['id']; } unset($ids); $res1 = TRUE; $modelUserCollect = new UserCollectModel(); $this->startTrans(); $res1 = $this->where($map)->delete() !== FALSE ? TRUE : FALSE; // 删除社区资讯. $sql = $this->getLastSql(); $resDetail = $resCollect = TRUE; // 删除副表数据. $tableName = $this->tablePrefix . 'sq_fdata'; $resDetail = $this->table($tableName)->where(['dataID' => ['IN', $deleteIds]])->delete() === FALSE ? FALSE : TRUE; $sql1 = $this->getLastSql(); // 删除收藏. $resCollect = $modelUserCollect->where(['type' => 1, 'dataID' => ['IN', $deleteIds]])->delete() === FALSE ? FALSE : TRUE; $sql2 = $this->getLastSql(); if ($res1 && $resDetail && $resCollect) { $this->commit(); return TRUE; } else { $this->rollback(); return FALSE; } }
/** * 删除指定用户发布的分类信息. * * @access public * @param integer $userId 用户 ID. * @param array $ids 分类信息 ID. * @param boolean $deletedCollect 删除收藏, 默认 FALSE. * @return boolean 删除成功返回 TRUE, 删除失败返回 FALSE; 出错时抛异常. */ public function remove($userId, array $ids, $deletedCollect = FALSE) { if (empty($ids) || empty($userId)) { throw new \Exception(Message::get(Message::PARAM_ERROR), Message::PARAM_ERROR); } // 查询出用户发布的分类信息 ID. $map = ['userID' => $userId, 'dataID' => ['IN', $ids]]; $deleteIds = $this->field(['dataID' => 'id', 'modID' => 'module_id'])->where($map)->select(); $deleteIds = NULL === $deleteIds || FALSE === $deleteIds ? [] : $deleteIds; if (empty($deleteIds)) { throw new \Exception(Message::get(Message::PARAM_ERROR), Message::PARAM_ERROR); } $res1 = TRUE; $modelUserCollect = new UserCollectModel(); $this->startTrans(); $res1 = $this->where($map)->delete() !== FALSE ? TRUE : FALSE; // 删除主表数据. $detailResult = $collectResult = []; foreach ($deleteIds as $r) { // 删除副表数据. $tableName = $this->tablePrefix . 'ctg_data' . $r['module_id']; if ($this->table($tableName)->where(['dataID' => $r['id']])->delete() === FALSE) { $detailResult[] = FALSE; break; } if ($deletedCollect) { // 删除收藏. if ($modelUserCollect->where(['type' => 0, 'dataID' => $r['id']])->delete() === FALSE) { $collectResult[] = FALSE; break; } } } if ($res1 && count($detailResult) < 1 && count($collectResult) < 1) { $this->commit(); return TRUE; } else { $this->rollback(); return FALSE; } }