我想顯示兩個事件串列:一個包含由具有組織者狀態的組織者創建的事件的串列和一個包含由具有成員狀態的組織者創建的事件的串列。我的條件是這樣的:“如果某個活動存在”和“如果創建該活動的組織者是組織者(或普通成員)”。有關資訊,“組織者”和“成員”取決于組織者的狀態。由于第二個條件,我有這個錯誤:
Key "organizer" for array with keys "0" does not exist.
我不明白為什么......需要糾正什么?
非常感謝您的幫助。
我的樹枝檔案:
{% extends 'base.html.twig' %}
{% block title %}Liste des activités{% endblock %}
{% block main %}
<div class="events">
<div class="vr fixed-top start-50"></div>
<div class="row">
<div class="col-12 col-md-6">
<h2 class="text-center my-4">
<img src="{{ asset('img/titres/zpeak-sorties.svg') }}" alt="在 Synfony 中,我如何根據其創建者的法規劃分串列?錯誤:鍵為“0”的陣列的鍵不存在">
</h2>
<ul hljs-keyword">list-group">
{% if events and events.organizer.status == 'organizer' %}
{% for event in events %}
<a hljs-keyword">list-group-item list-group-item-action">
<img src="{{ asset('img/flag_images/' ~ event.spokenlanguage.image) }}" alt="在 Synfony 中,我如何根據其創建者的法規劃分串列?錯誤:鍵為“0”的陣列的鍵不存在" hljs-number">2"> {{ event.title }}
</a>
{% endfor %}
{% else %}
<p>Il n'y a pas de zpeak sortie organisée.</p>
{% endif %}
</ul>
</div>
<div hljs-number">12 col-md-6">
<h2 hljs-number">4">
<img src="{{ asset('img/titres/zpeak-idees.svg') }}" alt="在 Synfony 中,我如何根據其創建者的法規劃分串列?錯誤:鍵為“0”的陣列的鍵不存在">
</h2>
<ul hljs-keyword">list-group">
{% if events and events.organizer.status == 'member' %}
{% for event in events %}
<a hljs-keyword">list-group-item list-group-item-action">
<img src="{{ asset('img/flag_images/' ~ event.spokenlanguage.image) }}" alt="在 Synfony 中,我如何根據其創建者的法規劃分串列?錯誤:鍵為“0”的陣列的鍵不存在" hljs-number">2"> {{ event.title }}
</a>
{% endfor %}
{% else %}
<p>Il n'y a pas de zpeak idée proposée.</p>
{% endif %}
</ul>
</div>
</div>
</div>
{% endblock %}
我的EventsController.php檔案:
<?php
namespace App\Controller\Front;
use App\Form\SearchType;
use App\Repository\EventsRepository;
use App\Repository\CategoriesRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class EventsController extends AbstractController
{
#[Route('/search', name: 'search')]
public function search(Request $request, SessionInterface $sessionInterface)
{
$data = $request->request->all();
$sessionSearchFormData = $sessionInterface->get('searchFormData');
$form = $this->createForm(SearchType::class, ['data' => $sessionSearchFormData]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$sessionInterface->set('searchFormData', $data);
return $this->redirectToRoute('events', [$data]);
}
return $this->renderForm('front/search.html.twig', [
'form' => $form
]);
}
#[Route('/events', name: 'events')]
public function events(
EventsRepository $eventsRepository,
CategoriesRepository $categoriesRepository
){
$events = $eventsRepository->findAll();
dd($events);
$categories = $categoriesRepository->findAll();
return $this->render("front/events.html.twig", ['events' => $events, 'categories' => $categories]);
}
}
EventsController.php 中的dd($events):
EventsController.php on line 45:
array:1 [▼
0 => App\Entity\Events {#1026 ▼
-id: 1
-title: "Soirée à Belleville"
-description: "Viens boire un verre, rencontrer des natifs et pratiquer l'espagnol !"
-category: Proxies\__CG__\App\Entity\Categories {#933 ?}
-spokenlanguage: Proxies\__CG__\App\Entity\Language {#981 ?}
-address: Proxies\__CG__\App\Entity\Location {#744 ?}
-date: DateTime @1665446400 {#1022 ?}
-starttime: DateTime @72000 {#1023 ?}
-endtime: DateTime @86340 {#1024 ?}
-organizer: Proxies\__CG__\App\Entity\User {#996 ▼
-id: 1
-email: null
-roles: []
-password: null
-gender: null
-lastname: null
-firstname: null
-birthdate: null
-occupation: null
-photo: null
-status: null
-nationality: null
-nativelanguage: null
__isInitialized__: false
…2
}
-participations: Doctrine\ORM\PersistentCollection {#1002 ?}
}
]
uj5u.com熱心網友回復:
在你的情況下,你可以有很多事件,所以在你的樹枝中,你需要使用一個 for 回圈。將您for loop的放在if.
像這樣的東西:
<ul class="list-group">
{% for event in events %}
{% if event and event.organizer.status == 'organizer' %}
<a class="list-group-item list-group-item-action">
<img src="{{ asset('img/flag_images/' ~ event.spokenlanguage.image) }}" alt="在 Synfony 中,我如何根據其創建者的法規劃分串列?錯誤:鍵為“0”的陣列的鍵不存在" hljs-number">2"> {{ event.title }}
</a>
{% else %}
<p>Il n'y a pas de zpeak sortie organisée.</p>
{% endif %}
{% endfor %}
</ul>
如果您確定只有一件物品,您可以使用events[0].organizer.status == 'organizer'
問候,
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/511270.html
標籤:数组交响乐变量钥匙
上一篇:我應該在函式中輸入什么引數
下一篇:如何糾正因子變數水平的錯誤?
