1

I try to make a classic comment-multilevel replies system :

Comment 1
   Reply 1-1
     Reply 1-2
        Reply 1-3

So I have created 2 entities Comment and Reply defined by :

class Comment
{
[...]
/**
 * @ORM\OneToMany(targetEntity="CommentBundle\Entity\Reply", mappedBy="comment")
 */
protected $replies;
[...]


class Reply
{
[...]
/**
 * @ORM\ManyToOne(targetEntity="CommentBundle\Entity\Comment", inversedBy="replies")
 */
protected $comment;

/**
 * children replies
 * @ORM\OneToMany(targetEntity="CommentBundle\Entity\Reply", mappedBy="reply")
 */
protected $replies;

/**
 * parent reply
 * @ORM\ManyToOne(targetEntity="CommentBundle\Entity\Reply", inversedBy="replies")
 * @ORM\JoinColumn(name="reply_id", referencedColumnName="id", nullable=true)
 */
protected $reply;
[...]

And associated queries :

$qrycomment = "SELECT a, b, c FROM CommentBundle:Comment a LEFT JOIN a.replies b LEFT JOIN b.replies c ";

$qryreply = "SELECT a, b FROM CommentBundle:Reply a LEFT JOIN a.replies b";  

My problem :

CASE 1 : execute qryreplies => 1 query executed => OK

query 1 : get reply1-1, reply1-2 and reply1-3

CASE 2 : execute qrycomment => multi queries executed => KO

query 1 : get comment1-1, reply 1-1

query 2 : get reply1-2

query 3 : get reply1-3

Note :

comment.twig :

{% for key,item in items %}
    {{ item.description }}<br>
    {% include "CommentBundle:Comment:Reply.twig" with {'items': item.getReplies } %}
{% endfor %}

Reply.twig

   {% for key,item in items %}
        Reply :
        {{ item.description }}
        {% include "CommentBundle:Comment:Reply.twig" with {'items': item.getReplies } %}
    {% endfor %}

I hope that my explanations are sufficiently clear. Thanks for your help.

Kyoshi
  • 11
  • 2

0 Answers0