Twig を使用して、Doctrine 2 エンティティ オブジェクトの単純なリストを表示しようとしています。オブジェクトは、DQL クエリからのそのままの結果でフェッチされます。PHPコードは次のようになります。
/*
* I know the query in the Repository class works as I get the correct objects
* from the method and can iterate through them with foreach in PHP, displaying
* data from the object through getters, like ->getId()
*/
$received = $repo->getReceived($id);
/*
* This appears to work, the variables are assigned correctly.
*/
echo $template->render(array("received" => $received, "first" => $received[0]));
Twig テンプレートには以下が含まれます。
{% for item in received %}
<li>Item: {{ item.id }}, {{ first.id }}</li>
{% endfor %}
奇妙なことに、小枝は受け取った配列を正しく反復処理し、配列には 3 つのオブジェクトがあり、3 行の出力がありますが、item.id としてアクセスすると id (または他のメンバー) は出力されませんが、2 番目の出力は、first.id、正しく出てきます。
私が見るべきものはこれです:
Item 1, 1
Item 2, 1
Item 3, 1
しかし、代わりに私は見ています:
Item , 1
Item , 1
Item , 1
出力を取得するためにさまざまなバリエーションを試しましたが、これまでのところ、item.getId、item.getId() は何も機能していません。
私がここで見逃しているアイデアはありますか?
ダンプ出力の編集
{{ ダンプ (受信) }} は次を返します。
array(3) {
[0]=>
object(Received)#219 (6) {
["id":"Received":private]=>
int(1)
... snip ...
}
[1]=>
object(Received)#208 (6) {
["id":"Received":private]=>
int(2)
... snip ...
}
[2]=>
object(Received)#210 (6) {
["id":"Received":private]=>
int(3)
... snip ...
これらは 3 つの本格的な Doctrine エンティティ オブジェクトであるため、配列内にあり、正しいように見えます。