Doctrine - 在View中创建动态列表

时间:2017-04-24 14:50:25

标签: php symfony doctrine-orm

我正在尝试创建一个视图,以显示商店所有供应商和客户的总销售额。 客户的总销售很容易获得,问题是当我必须用品牌创建列,我一直在浏览这个网站,学说等,但我找不到任何解决方案。

SaleController.php

/**
 * @Route("/widget/summarize")
 *
 * @return Render
 */
public function widgetSummarizeAction()
{
    $suppliers = $this->getDoctrine()->getEntityManager()->createQueryBuilder()
        ->select(['su.name AS name'])
        ->from('AppBundle:Supplier', 'su')
        ->getQuery()
        ->getArrayResult()
    ;

    $data = $this->getDoctrine()->getManager()->createQueryBuilder()->groupBy('c.name')
        ->select([
            's.id AS id',
            's.invoiceDate AS invoiceDate', 
            'c.name AS customer', 
            'SUM(ROUND(s.amount * s.price, 2)) AS totalSales',
            'su.name AS supplier', 
        ])
        ->from('AppBundle:Sale', 's')
        ->leftJoin('s.supplierCustomer', 'sc')
        ->leftJoin('sc.customer', 'c')
        ->leftJoin('sc.supplier', 'su')
        ->getQuery()
        ->getArrayResult()
    ;

    return $this->render('AppBundle::home_widget.html.twig', [
        'title'     =>  'Total Sales',
        'icon'      =>  'money',
        'urlList'   =>  $this->generateUrl($this->getUrlList()),
        'rowRoute'  =>  $this->getUrlEdit(),
        'data'      =>  $data,
        'columns'   =>  [
            'customer'      =>  'customer',
            'totalSales'    =>  'Total Sales'
        ]
    ]);

home_widget.html.twig

<div class="panel panel-default home-widget">
    <div class="panel-heading">
        <h2 class="panel-title pull-left">
            {% if icon is defined and icon is not empty %}<i class="fa fa-{{ icon }}"></i>{% endif %}
            {{ title }}
        </h2>
        {% if urlList %}
            <a class="btn btn-primary pull-right" href="{{ urlList }}">See All</a>
        {% endif %}
        <div class="clearfix"></div>
    </div>

    <div class="panel-body">
        {% if data is not empty and data is iterable %}
            <table class="table table-bordered table-striped">
                <thead>
                    <tr>
                        {% for column in columns %}
                            <td>{{ column }}</td>
                        {% endfor %}
                    </tr>
                </thead>

                <tbody>
                    {% for row in data %}
                        {% set href = url(rowRoute, {'id': row.id}) %}

                        <tr>
                            {% for key,column in columns %}
                                <td>
                                    <a href="{{ href }}">
                                        {{ row[key] | raw }}
                                    </a>
                                </td>
                            {% endfor %}
                        </tr>
                    {% endfor %}
                </tbody>
            </table>
        {% endif %}
    </div>
</div>

1 个答案:

答案 0 :(得分:1)

也许尝试通过以下方式获取colulmns名称:

$em = $this->getDoctrine()->getManager();
$columns = $em->getClassMetadata(your_entity_name::class)->getFieldNames();

并循环遍历树枝模板中的列。

<thead>
    <tr>
       {% for column in columns %}
           <td>{{ column }}</td>
        {% endfor %}
    </tr>
</thead>