在我的代码中,我尝试打印出所有针对“ 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;
}
}
在树枝上,我尝试像这样打印:
{% 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] =>字符串(11)“ Geschlossen” [“ dienstag”:“ App \ Entity \ Offnungszeiten”:private] => string(22)“ 09.00 Uhr-18.00 Uhr” [“ mittwoch “:” App \ Entity \ Offnungszeiten“:private] =>字符串(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] =>字符串(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] =>字符串(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").
``` if I tried it without var_dump()
I hope that the problem was understandable and that you can help me! Thank you!
尝试在symfony中使用转储方法
dump(var)
它正确呈现有关对象/变量的信息。
更多信息https://symfony.com/doc/current/components/var_dumper.html