简体   繁体   English

树枝不工作与PHP相同

[英]Twig not working the same as php

Why is it that when I write my test in php… 为什么当我用php编写测试时…

foreach ($rounds as $round){
    $assignment = $em->getRepository(‘WorkBundle:Doc’)->findOneBy(array(
        ‘user’ => $user->getId(),
        ’whichRound’ => $round,
    ));

   if (!$assignment){
    echo “assign ”.$round. “ to user”;
   }else{
    echo “already assigned to ”.$round. “ to user”;
   }
 }

return array (
    'user' =>  $user,
    'assignment' => $assignment,
    'rounds' => $rounds,
);

…it works correctly. …它正常工作。 When assignment is null, it will output the “assign ”.$round. “ to user”; 当assignment为null时,将输出“assign ”.$round. “ to user”; “assign ”.$round. “ to user”; and when it's not null it will output “already assigned to ”.$round. “ to user”; 当它不为null时,将输出“already assigned to ”.$round. “ to user”; “already assigned to ”.$round. “ to user”; .

However when I go in my twig template with the returned variables above and do… 但是,当我使用上面返回的变量进入树枝模板并执行...

{% for round in rounds %}
    {% if assignment is null %}
        <h2>{{ user }} successfully added to {{ round }}</h2>
    {% else %}
        <h2>{{ user }} has already been assigned to the {{ round }}</h2>
    {% endif %}
{% endfor %}

…it does not work correctly? …无法正常工作? It will instead output the same message twice…in an example, if the first round is null and the second one it not null, it will output the second message {{ user }} has already been assigned to the {{ round }} twice. 相反,它将输出两次相同的消息…在一个示例中,如果第一轮为null,第二轮不为null,它将输出第二条消息{{ user }} has already been assigned to the {{ round }}两次。 。

What am I messing up? 我搞砸了吗?

When you go through the foreach loop in your code, you're setting $assignment each time. 当您遍历代码中的foreach循环时,每次都在设置$assignment When you return the array, you're only returning the last time $assignment is set. 返回数组时,仅返回最后一次设置$assignment时间。

It looks like $rounds is an array of numbers and you'd like to associate the round with an assignment result. 看起来$rounds是一个数字数组,您想将回合与赋值结果相关联。 Based on that, I'd advise building a new array like this: 基于此,我建议构建一个像这样的新数组:

$results = array();

foreach ($rounds as $round) {
    $row = array(
        'round' => $round,
        'assignment' => $em->getRepository('WorkBundle:Doc')->findOneBy(array(
            'user' => $user->getId(),
            'whichRound' => $round,
        ))
    );

    if ($row['assignment']) {
        echo "Already assigned $round to user.";
    } else {
        echo "Assign $round to user.";
    }

    $results[] = $row;
}

return array(
    'user' => $user,
    'results' => $results,
);

Your Twig template would then look like this: 您的Twig模板将如下所示:

{% for row in results %}
    {% if row.assignment is null %}
        <h2>{{ user }} successfully added to {{ row.round }}</h2>
    {% else %}
        <h2>{{ user }} has already been assigned to the {{ row.round }}</h2>
    {% endif %}
{% endfor %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM