PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, January 31, 2022

[FIXED] Twig for loop can't find record in MySQL database

 January 31, 2022     symfony, twig     No comments   

Issue

My for loop doesnt seem to be able to find any records in my database.

My loop:

            {% for school in scholen %}
                <li>{{ scholen.naam|e }}</li>
            {% else %}
                <li>no user found</li>
            {% endfor %}

My result.

If I remove the else it's just 'empty' underneath "Scholen"

This is my controller

namespace App\Controller;


use App\Entity\School;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;

class SchoolController extends AbstractController
{
    /**
     * @Route ("/scholen", name="scholen")
     * Method ({"GET", "POST"})
     */

    public function schoolgegevens()
    {
        $scholen = $this->getDoctrine()
            ->getRepository(School::class);


        return $this->render("security/school.html.twig", ["scholen" => $scholen]);
    }

} 

My database table "Scholen".


Solution

Look at this code :

        $scholen = $this->getDoctrine()
            ->getRepository(School::class);

$scholen contains your repository, not your data. You must do something like

    public function schoolgegevens(SchoolRepository $schoolRepository)
    {
        $scholen = $schoolRepository->findAll();

        return $this->render("security/school.html.twig", ["scholen" => $scholen]);
    }


Answered By - shaax
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing