Symfony2在Twig中使用JOIN显示SQL结果

时间:2014-01-28 23:25:22

标签: php sql symfony twig

我开始用symfony开发。我已经学会了如何编写简单查询,然后在Twig中显示检索到的数据。然后我在查询中添加了一个JOIN语句,所以它看起来像这样:

$query = $em->createQuery('SELECT p, s FROM DemoProductBundle:Product p LEFT JOIN DemoProductBundle:SynchronizationSetting s WITH s.id_product = p.id');

在我的模板中,我有代码循环每一行并显示我想要的数据:

    {% for item in list %}
                    <tr>
                        <td>{{ item.id }}</td>
                        <td>{{ item.eanCode }}</td>
                        <td>{{ item.nameFull }}</td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td><a href="{{ path('webrama_product_category_edit', {'id': item.id}) }}" class="btn btn-outline btn-bg btn-default">Edytuj</a>
                            <a href="{{ path('webrama_product_category_delete', {'id': item.id}) }}" class="btn btn-outline btn-bg btn-default">Usuń</a></td>
                    </tr>
                {% endfor %}

修改查询后,我看到一个错误:

Method "id" for object "Demo\ProductBundle\Entity\SynchronizationSetting" does not exist in DemoProductBundle:Products:index.html.twig at line 35 

列表中的项目现在以某种方式划分为两个实体吗?我不知道......

1 个答案:

答案 0 :(得分:0)

您必须逐个选择要在Twig中显示的列,如下所示:
$query = $em->createQuery('SELECT p.id, p.name, s.color, s.size FROM DemoProductBundle:Product p LEFT JOIN DemoProductBundle:SynchronizationSetting s WITH s.id_product = p.id');