简体   繁体   中英

Symfony2 Displaying SQL result with JOIN in Twig

I'm starting developing with symfony. I've learned how to write simple query and then display retrieved data in Twig. Then I added a JOIN statement to my query so it looks like this:

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

In my template I have code which looping each row and display data I want:

    {% 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 %}

After modifying the query I see an error:

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

Is the item in list divided now somehow for two Entities? I don't know...

您必须一一选择要在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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM