Sébastien
Sébastien

Reputation: 2394

Symfony2 display related entity property in Twig template

Let's say I have a Product class and a Category class. Each Product has 1 Category. On the list of products (generated by calling php app/console doctrine:generate:crud), I would like to display the category name for each product. However everything I've tried so far doesn't work.

I'm using the KnpPaginatorBundle (don't know if it makes a difference or not).

Here's the code inside the controller:

// ProductController.php

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

    $query = $em->createQuery(
        'SELECT p
        FROM MyMainBundle:Product p
        ORDER BY p.name'
    );

    $paginator = $this->get('knp_paginator');
    $pagination = $paginator->paginate($query, $request->query->get('page', 1), 10);

    return $this->render('MyMainBundle:Product:index.html.twig', array(
        'pagination' => $pagination,
    ));
}

Here's the code inside the template:

{# index.html.twig #}
{% extends 'MyMainBundle::layout.html.twig' %}

{% block body -%}
    <table>
        <thead>
            <tr>
                <th>Name</th>
                <th>Category</th>
            </tr>
        </thead>
        <tbody>
        {% for entity in pagination %}
            <tr>
                <td>{{ entity.name }}</td>
                <td>{{ entity.category }}</td>
            </tr>
        {% endfor %}
        </tbody>
    </table>
    {{ knp_pagination_render(pagination) }}
{% endblock %}

I've added a __toString() method to the Category class:

public function __toString()
{
    return $this->getName();
}

But I get the following error:

Method "category " for object "My\MainBundle\Entity\Product" does not exist in MyMainBundle:Product:index.html.twig at line 16

I've tried adding LEFT JOIN p.category category to my query, to no avail.

I've tried replacing {{ entity.category }} by {{ entity.category.name }}, in which case I get:

Method "name " for object "Proxies__CG__\My\MainBundle\Entity\Category" does not exist in MyMainBundle:Product:index.html.twig at line 16

I've tried replacing {{ entity.category }} by {{ entity.getCategory }} in which case I get:

Method "getCategory " for object "My\MainBundle\Entity\Product" does not exist in MyMainBundle:Product:index.html.twig at line 16

I've tried replacing {{ entity.category }} by {{ entity.getCategory() }} in which case I get:

Unexpected token "name" of value " " ("end of print statement" expected) in MyMainBundle:Product:index.html.twig at line 16

How am I supposed to display the category name?

Upvotes: 1

Views: 2120

Answers (1)

Johann
Johann

Reputation: 758

I noticed that in both your error messages:

Method "category " for object "My\MainBundle\Entity\Product" does not exist in MyMainBundle:Product:index.html.twig at line 16

and

Method "name " for object "Proxies_CG_\My\MainBundle\Entity\Category" does not exist in MyMainBundle:Product:index.html.twig at line 16

you have a space between the method name and the following ".

This happened to me before when typing my code too quickly: I pressed the key to make the Twig }} before typing the space after the property name, so I ended up having a non-breaking space between the property and the }}, which Twig doesn't like and takes as part of the property name.

Check if you don't have a non-breaking space in your code before the }}.

Upvotes: 2

Related Questions