更新:正如 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/