Home > OS >  In Synfony how do I divide a list according to the statut of his creator ? Error : Key for array wit
In Synfony how do I divide a list according to the statut of his creator ? Error : Key for array wit

Time:10-07

I'd like to display two lists of events : one list with events created by organizers with organizer status and a list with events created by organizers with member status. My conditions are like this : "if some event exist" and "if the organizer who created the event is an organizer (or a regular member)". For information, "organizer" and "member" depend on the status of the organizer. Because of the second condition, I had this error :

Key "organizer" for array with keys "0" does not exist.

I don't understand why... What needs to be corrected ?

Thank you very much for your help.

My twig file :

{% extends 'base.html.twig' %}

{% block title %}Liste des activités{% endblock %}

{% block main %}

<div >

    <div ></div>

    <div >

        <div >

            <h2 >
                <img src="{{ asset('img/titres/zpeak-sorties.svg') }}" alt="Les Zpeak Sorties !">
            </h2>

            <ul >

                {% if events and events.organizer.status == 'organizer' %}
                    
                    {% for event in events %}

                    <a >
                        <img src="{{ asset('img/flag_images/' ~ event.spokenlanguage.image) }}" alt="Drapeau {{ event.spokenlanguage.name }}" > {{ event.title }}
                    </a>

                    {% endfor %}

                {% else %}

                    <p>Il n'y a pas de zpeak sortie organisée.</p>

                {% endif %}

            </ul>

        </div>

        <div >

            <h2 >
                <img src="{{ asset('img/titres/zpeak-idees.svg') }}" alt="Les Zpeak Idées !">
            </h2>

            <ul >

            {% if events and events.organizer.status == 'member' %}
                    
                {% for event in events %}
                    
                <a >
                    <img src="{{ asset('img/flag_images/' ~ event.spokenlanguage.image) }}" alt="Drapeau {{ event.spokenlanguage.name }}" > {{ event.title }}
                </a>

                {% endfor %}

            {% else %}

                <p>Il n'y a pas de zpeak idée proposée.</p>

            {% endif %}

            </ul>
            
        </div>
    
    </div>

</div>

{% endblock %}

My EventsController.php file :

<?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]);
    }
}

dd($events) in EventsController.php :

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 ▶}
  }
]

CodePudding user response:

In your case, you can have many Events, so in your twig, you need to use a for loop. Place your for loop before the if.

Something like this :

<ul >

{% for event in events %}

    {% if event and event.organizer.status == 'organizer' %}

        <a >
            <img src="{{ asset('img/flag_images/' ~ event.spokenlanguage.image) }}" alt="Drapeau {{ event.spokenlanguage.name }}" > {{ event.title }}
        </a>


    {% else %}

        <p>Il n'y a pas de zpeak sortie organisée.</p>

    {% endif %}

 {% endfor %}

</ul>

In case you are sure to have only one item, you can use events[0].organizer.status == 'organizer'

Regards,

  • Related