symfony - 如何将数据库中的所有数据显示到twig中?

标签 symfony doctrine symfony-2.1

更新:正如 Gerry 建议的那样,我解决了问题,但我无法使用 twig 显示任何内容。我转储变量,这就是我得到的:

object(Acme\Bundle\NewBundle\Entity\database)#275 (10) {
 ["id":"Acme\Bundle\NewBundle\Entity\database":private]=> int(2) 
["username":"Acme\Bundle\WebBundle\Entity\database":private]=> string(4) "almighty" 
["password":"Acme\Bundle\NewBundle\Entity\database":private]=> string(7) "blabla" 
["from":"Acme\Bundle\NewBundle\Entity\database":private]=> string(6) "bitola" ....

我尝试过:

{{% for i in user %}}
{{i.username}} - username
{{i.password}} - password
{{% endfor %}}

仍然不走运..变量中有数据,但它是一个对象而不是数组。那么我如何显示 is ?

谢谢。

最佳答案

您只需将 find() 方法的结果分配给您的模板即可。 find() 返回结果对象(如果匹配)。

$user = $this->getDoctrine()
    ->getRepository('AcmeNewBundle:database')
    ->find($username);

return $this->render('AcmeNewBundle:Default:hello.html.twig',array('user' => $user));

如果您的用户对象具有属性 username,您可以将其显示在模板中,如下所示:

{{ user.username }}

关于symfony - 如何将数据库中的所有数据显示到twig中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15753291/

相关文章:

symfony - 具有多个 symfony2 应用程序的 nginx

php - Symfony - 找不到 "GET/home"的路由

php - 自动 Getter/Setter 函数

php - Symfony 2.1 跨子域安全

php - Symfony2 之上的多站点 SaaS CMS

php - Symfony 依赖注入(inject) : Maximum function nesting level

php - 我如何在 doctrine 2 实体的存储库中使用复杂的标准?

php - 选择标签更新另一个

symfony - 如何在 Bundle 中正确声明对另一个 Bundle 的依赖?

locale - 检测语言环境并更改 url(重定向)以包含语言环境