Jaeger Jaeger - 6 months ago 51
Twig Question

Can't exploit properly an array with twig

I created an entity that I want to use to save events such as "[this user] accepted / declined the mission". So far I have no problem to save the event, but I'm having issues to display it as a list in the mission's view

I created a loop so that every line from the log's table should show if the the mission's id saved with the rest of the log's line match with the current mission

$history = array();
$log = $this->getDoctrine()->getRepository('MissionBundle:Log')->findAll();
foreach($log as $l){
if($h->getMission() === $mission->getId()){
$history['user'] = $l->getUsername();
$history['action'] = $l->getAction();
$history['day'] = $l->getDay();

At first, instead of using
$history['action'] = $l->getAction();
, I tried with
array_push($history, $l->getUser(), $l->getAction(), $l->getDay());
, but the result i got was unusable, since The twig dump I get in result that looked like the following:

0 => "Isitech"
1 => "defined the mission as filled"
2 => DateTime {#1421 ▶}
3 => "Isitech"
4 => "defined the mission as declined"
5 => DateTime {#1426 ▶}

So with the
thing that I used, I now have the following twig dump:

array:3 [▼
"user" => "Isitech"
"action" => "defined the mission as declined"
"day" => DateTime {#1426 ▶}

I currently have two issues: first, I'd like to create a loop so that I can display every log's line as following:

{% for h in history %}
{{ h.user }} {{ h.action }} on {{ h.day | date }}
{{ endfor }}

For an unknown, with this loop and how it's displayed in twig, I can't call anything by using {{ h.user }} without getting a message telling me that "user doesn't exist. Moreover, if I do {{ dump(history) }}, I get (i.e.) "isitech" and not [user] => "Isitech", so I can't use the data

Moreover, I currently have two entries in my logs about the same user, but I manage to have only one.

I think I'm missing out something but I can't find where, how and why..

Any idea?


Actually, I went too far in my loop, and this is why I couldn't have more than one result.

Instead of setting each column by their name and hoping that I'll get my column time the number of existing logs, I simply recreated my loop as following:

$log = $this->getDoctrine()->getRepository('MissionBundle:Log')->findAll();
foreach($log as $l){
    if($l->getMission() === $mission->getId()){
            array_push($history, $l) 
            //So we're putting an array result directly in an other array

With this way, I can now use $history to loop on 'h' and show what I want with 'h.day' for example. Thank you for your help, it helped me finding what was wrong in my code !