Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何遍历symfony(条令)结果对象_Php_Arrays_Symfony_Doctrine Orm - Fatal编程技术网

Php 如何遍历symfony(条令)结果对象

Php 如何遍历symfony(条令)结果对象,php,arrays,symfony,doctrine-orm,Php,Arrays,Symfony,Doctrine Orm,我有以下疑问: function indexAction() { $u = $this->getDoctrine()->getRepository("AppBundle:Users")->findAll( \Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY ); return $this->render("default/test.html.twig", ["users" => $u]); } 我用这个循环在细枝

我有以下疑问:

function indexAction()
{
    $u = $this->getDoctrine()->getRepository("AppBundle:Users")->findAll( \Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY );

    return $this->render("default/test.html.twig", ["users" => $u]);
}
我用这个循环在细枝模板中循环:

{%  for item in users %}
<li>{{ item.email }} : </li>
{%  endfor %}

有点不对劲。您将
users
变量传递给模板,但您正在
for
循环中使用
myItems

试试这个:

{% for item in users %}
    {{ dump(item) }}<br />
{% endfor %}
{%用于用户%中的项目]
{{dump(item)}}
{%endfor%}
您的类是否在实体定义中实现了ArrayAccess

class Foo implements ArrayAccess
您必须在类中添加多个方法才能使其生效:

要实现ArrayAccess,您需要实现四种方法:offsetExists、offsetGet、offsetSet和offsetUnset。ArrayAccess::offsetExists必须返回布尔值,offsetGet可以返回任何有效的PHP类型,而offsetSet和offsetUnset不应返回任何值。一旦实现了这些方法,就可以将对象视为数组来保存和检索属性


更多详细信息。

您是否也尝试了{{item.property}}?因为在我看来,你是在试图呼应一个反对意见是的。我更新了问题的代码。事实上,我只是在这里粘贴代码时犯了一个错误。请尝试省略
findAll
方法的“\doctor\ORM\AbstractQuery::hydroge_ARRAY”参数,并确保在
Users
实体中有一个公共getEmail()getter。我更新了问题。我粘贴了一个错误的代码。对不起,我错了。仍然认为您发布的代码和错误消息不匹配。错误消息表示您正试图访问名为“name”(而不是“email”)的属性,如您的细枝代码所示。确保定义了访问器(getName()、getEmail()等)。正如在评论中提到的,findAll不接受任何参数,所以这个数组的东西没有做任何事情。
class Foo implements ArrayAccess