문제

I currently have a ManyToMany/ManyToMany relationship between a Tag entity and a Blog entity.

The doctrine query I am using now is as follows:

$qb = $this->createQueryBuilder('b')
        ->select('b, c, t')
        ->innerJoin('b.category', 'c')
        ->innerJoin('b.tags', 't')
        ->addOrderBy('b.created', 'DESC');

    return $qb->getQuery()
        ->getResult();

When I attempt to access the tags in twig with the following:

1) (the tags disappear and are not displayed)

{% for tag in blog %}
    <p class="tag-links"><span>Tagged:</span> rel="tag">{{ tag.tags }}</a>, <a href="" rel="tag">Tag 2</p>
{% endfor %}

2) (without the for loop - I get an error (could not be converted to string))

<p class="tag-links"><span>Tagged:</span> rel="tag">{{ tag.tags }}</a>, <a href="" rel="tag">Tag 2</p>

controller

public function indexAction()
{
    $em = $this->getDoctrine()->getManager();

    $blogs = $em->getRepository('AcmeBundle:Blog')
        ->getBlogs();

    return array(
        'blogs' => $blogs,
    );
}
도움이 되었습니까?

해결책

Your loop should point to the tags property of your blog, not the blog entity itself.

{% for tag in blog.tags %}

or

{% for tag in blog.getTags() %}

Furthermore you mustn't join category and tags because you get the related relationship entities via the corresponding properties of your blog entity. they will be proxy-loaded when you access them in your twig template.

I think it should be enough to pass only a BlogCollection to the view and then do

{% for blog in blogs %}
    {% for tag in blog.tags %}
        ...
    {% endfor %}
{% endfor %}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top