2
votes

In my code I tried to print all results for "montag" IS "geschlossen". It works, but I must print it in var_dump(), otherwise I get an error.

My question is, how can I return the results in a loop? I want to make this (PDO Example) with Doctrine, Symfony and twig:

foreach ($pdo->query($sql) as $row) {
   echo $row['value1']."<br />";
   echo $row['value2']."<br />";
}
<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;
use App\Entity\Offnungszeiten;
use Doctrine\ORM\EntityManagerInterface;

class OffnungsController extends AbstractController
{
    /**
     * @Route("/")
     */
    public function index(EntityManagerInterface $em)
    {
        $repository = $em->getRepository(Offnungszeiten::class);
        /** @var Offnungszeiten $open */
        $open = $repository->findOneBy(['id' => 3]);
        if (!$open) {
            throw $this->createNotFoundException(sprintf('Not Found Exception - SQL findOneBy() is undefined'));
        }


        $repository = $this->getDoctrine()->getRepository(Offnungszeiten::class);

        $products = $repository->findBy(
            ['montag' => 'Geschlossen'],
            ['id' => 'ASC']
        );

        $products = $repository->findAll();

    $result = var_dump($products);



        $response = $this->forward('App\Controller\IndexController::index', [
            'open' => $open,
            'productsrt' => $result
        ]);
        return $response;
    }
}

In twig I tried print it like these:

    {% for productsrt in productsrt %}
        {{ productsrt }}
    {% endfor %}

IndexController.php

namespace App\Controller;

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

class IndexController extends AbstractController
{
    public function index($open, $productsrt)
    {
        return $this->render('index/index.html.twig', [
            'controller_name' => 'IndexController',
            'pagetype' => 'index',
            'pageurl' => "https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]",
            'open' => $open,
            'productsrt' => $productsrt
        ]);
    }

It prints:

array(2) { [0]=> object(App\Entity\Offnungszeiten)#490 (8) { ["id":"App\Entity\Offnungszeiten":private]=> int(3) ["montag":"App\Entity\Offnungszeiten":private]=> string(11) "Geschlossen" ["dienstag":"App\Entity\Offnungszeiten":private]=> string(22) " 09.00 Uhr - 18.00 Uhr" ["mittwoch":"App\Entity\Offnungszeiten":private]=> string(22) " 09.00 Uhr - 18.00 Uhr" ["donnerstag":"App\Entity\Offnungszeiten":private]=> string(22) " 09.00 Uhr - 20.00 Uhr" ["freitag":"App\Entity\Offnungszeiten":private]=> string(22) " 09.00 Uhr - 18.00 Uhr" ["samstag":"App\Entity\Offnungszeiten":private]=> string(21) "09.00 Uhr - 13.00 Uhr" ["sonntag":"App\Entity\Offnungszeiten":private]=> string(11) "Geschlossen" } [1]=> object(App\Entity\Offnungszeiten)#493 ...and so on.

but I can't "reformat" it to an normal array or in a loop

I got the error

An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Object of class App\Entity\Offnungszeiten could not be converted to string").

if I tried it without var_dump()

I hope that the problem was understandable and that you can help me! Thank you!

2

2 Answers

2
votes

Your error says that object can't be converted to string. May be you should use to string casting ?

     /**
     * To string casting
     *
     * @return string
     */
    public function __toString(): string
    {
        return (string)$this->name;
    }

add it to your entity. Or use

    {% for productsrt in productsrt %}
        {{ productsrt.name }}
    {% endfor %}

in your controller you should send $products, you need not to use var_dump()

$products = $repository->findAll();

        $response = $this->forward('App\Controller\IndexController::index', [
            'open' => $open,
            'productsrt' => $products
        ]);
        return $response;
-1
votes

Try to use dump method in symfony

dump(var)

It renders informations about objects/vars properly.

More informaiton https://symfony.com/doc/current/components/var_dumper.html