2017-09-13 91 views
1

我需要幫助理解symfony緩存組件。我用symfony表單繼承了其他程序員的腳本來創建一個項目。這:Symfony緩存InvalidArgumantException

class OfferController extends Controller 
{ 
    public function createAction(Request $request) { 
     $api_offer = $this->get('gamexp_affiliates.api.admin.offer'); 
     $api_user = $this->get('gamexp_affiliates.api.admin.user'); 
     $api_project = $this->get('gamexp_affiliates.api.admin.project'); 
     $api_goal = $this->get('gamexp_affiliates.api.admin.goal'); 
     $tpl = '@GamexpAffiliates/Admin/Offer/Form/create.html.twig'; 

     $offer_webmasters = $offer_projects = $offer_goals = null; 

     $sendCommonResponse = function(FormInterface $form = null, $code = 200, $headers = []) use ($tpl, &$offer_webmasters, &$offer_projects, &$offer_goals) { 
      $params = [ 
       'offer_webmasters' => $offer_webmasters, 
       'offer_projects' => $offer_projects, 
       'offer_goals' => $offer_goals, 
      ]; 
      $response = $this->render($tpl, $form ? array_merge([ 
       'offer' => $form->createView(), 
      ], $params) : $params, new Response('', $code, $headers)); 
      return $response; 
     }; 

     $selectProject = function($project_id) use ($api_project, &$offer_projects, &$projects_sort, &$projects_count) { 
      $project = null; 
      if ($project_id > 0 && is_numeric($project_id)) { 
       foreach ($offer_projects as $offer_project) { 
        if ($project_id == $offer_project['id']) { 
         $project = $offer_project; 
         break; 
        } 
       } 
       if (!$project) { 

        try { 
         if ($project = $api_project->findProjectByIdWithLandings($project_id, ['id', 'name', 'status'])) { 
          if ($projects_count <= $cnt = count($offer_projects)) { 
           $offer_projects = array_slice($offer_projects, $cnt - $projects_count + 1, $projects_count); 
          } 
          $offer_projects = array_merge($offer_projects, [$project]); 
          if ($projects_sort) { 
           usort($offer_projects, function(array $a, array $b) use ($api_project, $projects_sort) { 
            foreach ($projects_sort as $field => $sort_type) { 

             if ($comp = $sort_type === $api_project::SORT_ASCENDING ? strnatcmp($a[$field], $b[$field]) : strnatcmp($b[$field], $a[$field])) { 
              return $comp; 
             } 
             continue; 
            } 
            return 0; 
           }); 
          } 
         } 
        } catch (\Exception $e) { 
         $this->get('logger')->error("Ошибка: " . $e->getMessage() . "на строке: " . $e->getLine()); 
        } 
       } 
      } 
      return $project; 
     }; 

     $createForm = function($data, $project = null, $landings = null) use ($api_offer, &$offer_webmasters, &$offer_projects, &$offer_goals) { 
      $params = [ 
       'action' => $this->generateUrl('gamexp_affiliates.admin.api.offer.create'), 
       'statuses' => $api_offer->getAvailableStatuses($landings ? array_unique(array_column($landings, 'status')) : null), 
       'offer_webmasters' => $offer_webmasters, 
       'offer_projects' => $offer_projects, 
       'offer_goals' => $offer_goals, 
      ]; 
      if ($project && !empty($project['landings'])) { 
       $params['offer_landings'] = $project['landings']; 
      } 

      return $this->createForm(AdminOfferCreateType::class, $data, $params); 
     }; 

     try { 
      if (!$offer_webmasters = $api_user->findAll(null, 10, 0, ['id', 'email', 'phone'])) { 
       return $sendCommonResponse(); 
      } 
     } catch (\Exception $e) { 
      return $sendCommonResponse(); 
     } 

     try { 
      if (!$offer_projects = $api_project->findAllProjectsWithLandingsAndUser(null, 10, 0, ['id', 'name', 'status'])) { 
       return $sendCommonResponse(); 
      } 
     } catch (\Exception $e) { 
      return $sendCommonResponse(); 
     } 

     try { 
      if (!$offer_goals = $api_goal->findAllItems(null, 10, 0, ['id', 'name', 'revshare', 'status'])) { 
       return $sendCommonResponse(); 
      } 
     } catch (\Exception $e) { 
      return $sendCommonResponse(); 
     } 

     $fast_mode = false; 
     if ($request->isMethod('GET')) { 

      $fast_mode = (0 < $project_id = $request->get('project') and is_numeric($project_id)); 
     } else { 
      $project_id = null; 
     } 

     if ($project = $selectProject($project_id)) { 
      $data = [ 
       'project' => $project['id'], 
      ]; 
     } else { 
      $data = null; 
     } 

     $form = $createForm($data, $project); 

     $form->handleRequest($request); 
     if ($form->isSubmitted()) { 
      if (!$request->isXmlHttpRequest()) { 
       throw $this->createNotFoundException(); 
      } 

      if (!$request->isMethod('POST')) { 
       throw $this->createHttpException(405); 
      } 

      $data = $form->getData(); 


      $landings = []; 
      if ($project = $selectProject(isset($data['project']) ? $data['project'] : null)) { 
       if (!empty($data['landings']) && !empty($project['landings'])) { 
        $data['landings'] = array_intersect($data['landings'], array_column($project['landings'], 'id')); 

        foreach ($project['landings'] as $landing) { 
         if (in_array($landing['id'], $data['landings'])) { 
          $landings[$landing['id']] = $landing; 
         } 
        } 
       } 
      } 
      $form = $createForm($data, $project, $landings); 
      $form->handleRequest($request); 
      $data = $form->getData(); 

      if ($form->isValid()) { 
       if ($res = $this->linkApiResponseToForm($form, function(FormInterface $form, array $params) use ($api_offer, $data, $sendCommonResponse) { 
        if (in_array('project_selection', $params['validation_groups']) || in_array('landings_selection', $params['validation_groups']) || in_array('header_image_upload', $params['validation_groups'])) { 
         if (in_array('header_image_upload', $params['validation_groups'])) { 
          if (isset($data['header_image']) && $data['header_image'] instanceof UploadedFile) { 
           $api_offer->saveHeaderImage($data['header_image']); 
          } 
         } 
         return $sendCommonResponse($form, 300, [ 
          'Location' => $this->generateUrl('gamexp_affiliates.admin.api.offer.create'), 
         ]); 
        } 
        $data['creator'] = $this->getUser()->getId(); 
        if ($id = (int)$api_offer->createOffer($data)) { 
         return new JsonResponse([ 
          'id' => $id, 
          'name' => trim($data['name']), 
         ], 201, [ 
          'Location' => $this->generateUrl('gamexp_affiliates.admin.api.offer.update', ['id' => $id]), 
         ]); 
        } 
       })) { 
        return $res; 
       } 
      } 
      return $sendCommonResponse($form, 400); 
     } elseif ($fast_mode && !$data['project']) { 
      $this->addFlash($form->getName().':warning', 'Проекта, который вы выбрали, не существует. Возможно кто-то другой его только что удалил. Попробуйте выбрать другой проект из списка.'); 
     } 

     return $sendCommonResponse($form); 
    } 
} 

我有這樣的例外:

request.CRITICAL:未捕獲的PHP異常 的Symfony \分量\緩存\異常\ InvalidArgumentException: 「緩存鍵 長度必須大於零」 的 /home/ruslan/Projects/partners_prod/cache/prod/classes.php line 1747 {「exception」:「[object] (Symfony \ Component \ Cache \ Exception \ InvalidArgumentException(code: 0):Cache key length在 處必須大於零/home/ruslan/Projects/partners_prod/cache/prod/classes.php:1747)「} []

而這只是當我在prod環境中發生。在開發一切工作正常!初始化頁面加載正常,但然後我得到一個額外的數據異常發生的Ajax請求。當我用debuger進行代碼時,它在$ form-> isSubmited()點失敗。在$ form-> handelRequest()方法之後。這個例外的含義是什麼?我沒有添加緩存任何鍵!怎麼運行的?我強烈需要知道這個例外的基本問題。幫助我,請將其放下。非常感謝您的幫助!

回答

0

好的。最後我在兩天後找到了解決方案。也許它會在未來幫助別人。問題在於形式。完全受到限制。這就像表單提交一樣,但表單根本無效。對於每個字段,symfony在緩存中有某種關鍵,但它不是緩存值。如果字段是必需的但不存在,則觸發異常。因爲在這種情況下,鍵是空字符串。在我選擇了所有必需的田地之後,它的運作良好。