2017-09-13 6 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ライン1747 {"例外": "[オブジェクト] (Symfony \ Component \ Cache \ Exception \ InvalidArgumentException(コード: 0):キャッシュキーの長さ で0より大きくなければなりません/home/ruslan/Projects/partners_prod/cache/prod/classes.php:1747) "} []

これは私が環境に慣れている場合にのみ起こります。 devのすべてでうまくいく!最初のページは正常に読み込まれますが、次にいくつかの余分なデータ例外のためのajaxリクエストが発生します。私がデバッガでコードに沿って行ったとき$ form-> isSubmited()ポイントで失敗します。 $ form-> handelRequest()メソッドの後。この例外の意味は何ですか?私はキーをキャッシュに追加していない!使い方?私は強くこの例外の基本的な問題を知る必要があります。私に助言してください。助けをありがとう!

答えて

0

最後に私は2日後に解決策を見つけました。多分将来、誰かを助けるでしょう。問題は形であった。厳密に制約があります。フォーム提出のようでしたが、フォームはまったく有効ではありませんでした。どのフィールドでも、symfonyは何とかキャッシュにキーを持っていますが、キャッシュ値ではありません。フィールドが必要だが存在しない場合は、例外がトリガーされます。この場合、キーは空文字列です。私はすべての珍しい畑を選んだ後、うまくいった。

関連する問題