简体   繁体   English

Symfony2-主义-循环形式

[英]Symfony2 - Doctrine - form in loop

I want create a list of comments from database. 我想从数据库创建评论列表。 For each of them I want to add form with like/dislike option 对于他们每个人,我想添加带有“喜欢/不喜欢”选项的表格

Issue: I know how to create one form, but there I need to use twig to iterate collection of comments and there put a forms inside for each comment. 问题:我知道如何创建一个表单,但是我需要使用细枝来迭代评论的集合,并在其中为每个评论放置一个表单。

Any idea how to handle this problem? 任何想法如何处理这个问题? Couldnt find anything about this in documentation. 在文档中找不到关于此的任何内容。 Maybe I am looking bad way. 也许我看起来很糟糕。 Please for help 请帮忙

Just create buttons like and dislike on you blogpage and use AJAX that calls the likeAction in your controller. 只需在您的博客页面上创建“喜欢”和“不喜欢”按钮,然后使用AJAX在您的控制器中调用likeAction。 The route to this controller needs to have a variabele $id pointing to the comment that has to be liked or disliked. 到该控制器的路由需要有一个变量$ id,该变量指向必须喜欢或不喜欢的评论。 In this controller you retrieve the user that is logged in (if possible) and you store the like with the user_id and comment_id into the database if the same like isnt available yet. 在该控制器中,您检索已登录的用户(如果可能的话),并且将诸如user_id和comment_id之类的内容存储到数据库中(如果尚未提供相同的like)。

<button data-id="{{comment.id}}" class="btn-like">Like</button>

$(function() {
  $(".btn-like").click(function() {
    var comment_id = $(this).attr('data-id');
    var url = "{{ path('app_comment_like', {'id': 'comment_id'}) }}";
    url = url.replace("comment_id", comment_id);
    $.ajax({
      url: url,
      type: "post"
    });
  });
});

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

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