如何在for循环中打印所有结果?

时间:2019-10-04 14:25:21

标签: php symfony doctrine twig symfony4

在我的代码中,我尝试打印“ montag”和“ geschlossen”的所有结果。它可以工作,但是我必须在var_dump()中打印它,否则会出现错误。

我的问题是,如何循环返回结果?我想用Doctrine,Symfony和twig制作这个(PDO示例):

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;
    }
}

在树枝上,我尝试将其打印如下:

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

它打印:

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 ...等等。

但是我无法将其“重新格式化”为普通数组或循环

我遇到了错误

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").

如果我在没有var_dump()的情况下尝试过

我希望这个问题是可以理解的,并且您可以帮助我!谢谢!

2 个答案:

答案 0 :(得分:2)

您的错误表明无法将对象转换为字符串。也许您应该使用to string casting吗?

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

将其添加到您的实体。 或使用

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

在您的控制器中,您应该发送$ products,而不需要使用var_dump()

$products = $repository->findAll();

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

答案 1 :(得分:-1)

尝试在symfony中使用转储方法

dump(var)

它正确呈现有关对象/变量的信息。

更多信息https://symfony.com/doc/current/components/var_dumper.html