我有一个非常简单的问题给你(我认为)。Symfony Template(Twig)中的学说子查询
我想要在树枝模板中留言框。
{% for comment in comments %}
... here ist html code for the box
{% endfor%}
怎么可能在每个评论条目中搜索其他内容,如子注释或类似投票的内容?在简单的PHP和HTML(不Symfony的),它会是这个样子:
while ($row = mysqli_fetch_assoc($query)) {
$sub_query= "SELECT for FROM bar WHERE id = $row['id']";
$query = mysqli_query($db, $sub_query);
$sub_row = mysqli_fetch_assoc($query);
?>
<div class="comment_box">
// code for the box like <?= $row['date'] ?> or <?= $row['date'] ?>
// code for the subqueryed data like <?= $sub_row['voting_rate'] ?> or <?= $sub_row['sub_comment'] ?>
</div>
<?php
}
什么是交响乐做到这一点的呢?它可能与Twig宏左右有关吗?
更多信息:
这里是简单的Controler:
/**
* Lists all Comment entries.
*
* @Route("/", name="comment")
* @Method("GET")
* @Template()
*/
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$comments = $em->getRepository('AcmeCommentBundle:Comment')->findAll();
return array(
'comments' => $comments,
);
}
我希望好的答案)。
在此先感谢。 Michael
假设你真的需要从树枝的内部执行查询,然后看看这里:http://symfony.com/doc/current/book/templating.html,向下滚动到嵌入式控制器部分。像@TomToms一样,如果您只需要相关实体(他的解决方案)或新的嵌入式查询。 – Cerad