I'm trying to use Twig to display a simple list of Doctrine 2 entity objects. The objects are fetched with the untouched results from a DQL query. The PHP code is like this:
/*
* 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]));
The Twig template contains this:
{% for item in received %}
<li>Item: {{ item.id }}, {{ first.id }}</li>
{% endfor %}
The strange thing is that twig iterates through the received array correctly, there are three objects in the array and there are three lines output but the id (or any other member) doesn't come out when accessed as item.id but the second output, first.id, comes out correctly.
What I should be seeing is this:
Item 1, 1
Item 2, 1
Item 3, 1
but instead I'm seeing:
Item , 1
Item , 1
Item , 1
I've tried different variations to get the output but nothing has worked so far, item.getId, item.getId().
Any ideas what I am missing here?
Edit dump output
{{ dump(received) }} returns the following:
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 ...
I've cut it down, these are three full blown Doctrine entity objects so they are in the array and look correct.
dump(received)
? – james_tookeygetId()
function? Your members are defined private, so twig cannot access them directly, but will try to call the getter. – Maerlyn