Slowwie
Slowwie

Reputation: 1246

Doctrine subquery in Symfony template

I want to have a comment box in a Twig template.

{% for comment in comments %}

... here ist html code for the box

{% endfor%}

How is it possible, to search in every comment entry for other things like sub comments or things like votings and so on? In simple PHP and HTML (not Symfony) it would look something like this:

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
}

What is the way to do this with symphony? Can it be that it has to do with Twig Macros or so on?

More information:

Here is the simple controller:

   /**
 * 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,
    );
}

Upvotes: 0

Views: 608

Answers (2)

Slowwie
Slowwie

Reputation: 1246

What I wanted to have was an embedded Controller. So I can do whatever I want to know about the special piece of information even if I don't have a relationship to it in my database.

Upvotes: 1

Tom Tom
Tom Tom

Reputation: 3698

So what you want is to access the variables of your entity ? You can simply use {{ comment.date }}.

And to access the variables of linked entities you can do {{ comment.subcomment.date }} but you need to have a relationship between your comment and subcomment entities set up.

In case of a ManyToOne or ManyToMany (ex: multiple subcomment for a given comment) relationship you can also do things like

{% for subcomment in comment.subcomments %}
   {{ subcomment.date }}
{% endfor %}

Or if you need a specific subcomment things like

{{ comment.subcomments[1].date }}

Your question is not very clear sorry if I'm off topic. In any case I would recommend reading the doc here: http://symfony.com/doc/master/book/index.html

Upvotes: 3

Related Questions