protected function handle() { $board_id = $this->boardId; $board = BoardModel::getBoard($board_id); $moderators = ModeratorModel::allModerator(function (QueryBuilder $qb) use($board) { $qb->andWhere($qb->expr()->eq('board_id', ':board_id'))->setParameter(':board_id', $board->id); $qb->andWhere($qb->expr()->eq('forum_id', ':forum_id'))->setParameter(':forum_id', $board->forumId); }); $session = $this->getSession(); $request = $this->getRequest(); if ($request->getMethod() == 'POST') { $posts = $request->request; $moderator_ids = $posts->get('moderators'); if (!$moderator_ids) { $moderator_ids = array(); } else { $moderator_ids = explode(',', $moderator_ids); } // 先删除所有的管理员, 再逐个添加 $db = ForumDatabase::getDb(); $db->transaction(); try { foreach ($moderators as $k => $moderator) { $moderator_id = $moderator['id']; ModeratorModel::deleteModerator($moderator_id); } foreach ($moderator_ids as $user_id) { $moderator = new ModeratorModel(); $moderator->forumId = $board->forumId; $moderator->boardId = $board->id; $moderator->userId = $user_id; ModeratorModel::createModerator($moderator); } $db->commit(); $session->addFlash('success', '操作成功'); } catch (\Exception $e) { $db->rollback(); $session->addFlash('error', $e->getMessage()); } return new JsonResponse(array()); } $moderator_ids = array(); foreach ($moderators as $k => $moderator) { $moderator_ids[] = $moderator['user_id']; } $moderator_ids = implode(',', $moderator_ids); // 采用GraphQL的形式进行查询 $user_api_url = $this->getContainer()->getParameter('user_url') . '/api/graphql'; return $this->render('moderator/index.html.twig', array('moderators' => $moderator_ids, 'board' => $board, 'api' => $user_api_url)); }
/** * 保存 * @param ModeratorModel $moderator * @return ModeratorModel * @throws \Exception */ public static function saveModerator(ModeratorModel $moderator) { return self::editModerator($moderator->toArray()); }