Search code examples
arraysdatabasesymfonykey

Key "dest_ville" for array with keys "0, 1, 2, 3, 4" does not exist


In a project, I have a table with data, an edit button that links to the form that I want to prefill with the data from the table. Here is my button:

newmodif.html.twig

<div id="div_24">
    <div class="align-inputs-dest">
        <b>Nom :</b>
        <input type="text" id="24" class="input_list input_uppercase input_color" maxlength="35" name="dest_nom" value="{{ info.dest_ville }}">  
    </div>
</div>

In ExpeditionController.php :

public function newmodif(request $request, int $id = null, EntityManagerInterface $entityManager, ExpeditionRepository $expeditionRepository) : Response
    {   
        $info = $expeditionRepository->findAll();
        $pays = $this->paysRepository->findAll();
        $user = $this->getUser();
        $comptesUtilisateur = $user->getComptes();
        $clientUser = $user->getClient();
        $clientUse = $user->getEmail();

        $array_val = [];
        $arr_new_exp[] = $request->request->get('az');


        $exp_mail = $entityManager->getRepository(Expedition::class)->findOneBy(['id' => $id]);


        return $this->render('pages/expedition/newmodif.html.twig', [
            'comptes' => $comptesUtilisateur,
            'pays' => $pays,
            'clientUser' => $clientUser,
            'clientUse' => $clientUse,
            'array_val' => $array_val,
            'exp_mail' => $exp_mail,
            'info' => $info,
        ]);
    }

I want to fetch data from my database with the id in the url but I got an error : Key "dest_ville" for array with keys "0, 1, 2, 3, 4"

Actually, I tried to call the data in a different way, with a loop it works with another field in same page:

{% for country in pays %}
    <option>{{ country.intitule }}</option>
{% endfor %}

Solution

  • $expeditionRepository->findAll() returns an array of multiple resources, 5 in total in your case (0,1,2,3,4). That means that $info contains multiple "Expedition" resources. Try dumping $info and check your profiler to see what items it contains!

    In newmodif.html.twig, you first need to loop over each element of $info, before you can call properties, like dest_ville (assuming this is a property of Expedition).

    You would need something like the code below. This will show all info results, so this might not fit your use case if you're trying to show one specific Expedition item. So use the code as a guidance, don't copy paste.

    {% for item in info %}
        <div id="div_24">
            <div class="align-inputs-dest">
                <b>Nom :</b>
                <input type="text" id="24" class="input_list input_uppercase input_color" maxlength="35" name="dest_nom" value="{{ item.dest_ville }}">  
            </div>
        </div>
    {% endfor %}
    

    Depending on your implementation, you might need to change findAll to find or findOneBy, to get a single result. Something like $info = $expeditionRepository->find($id) could do the trick aswell! This fetches one Expedition entity by the provided ID, so you won't need to loop in your view like the example above.

    The doctrine docs should help you further in finding the correct method for your use-case.