gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 08:13:49 24 4
gpt4 key购买 nike

在我的代码中,我尝试打印“montag”IS“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;
}
}

在 twig 中,我尝试像这样打印它:

    {% 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]=> 对象(App\Entity\Offnungszeiten)#490 (8) { ["id":"App\Entity\Offnungszeiten":private]=> int(3 ) ["montag":"App\Entity\Offnungszeiten":private]=> 字符串(11) "Geschlossen"["dienstag":"App\Entity\Offnungszeiten":private]=> 字符串(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]=> 对象(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()的情况下尝试过

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

最佳答案

您的错误表明该对象无法转换为字符串。也许您应该使用进行字符串转换

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

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

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

在你的 Controller 中你应该发送$products,你不需要使用var_dump()

$products = $repository->findAll();

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

关于php - 如何在for循环中打印所有结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58238300/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com