velvel/report-bundle

Symfony2 的在线数据库报告生成器

安装: 582

依赖者: 0

建议者: 0

安全: 0

星标: 17

关注者: 4

分支: 11

开放问题: 5

类型:symfony-bundle

dev-master 2013-06-04 17:00 UTC

This package is not auto-updated.

Last update: 2024-09-14 13:44:30 UTC


README

VelvelReportBundle 为管理员提供数据库自定义报告页面的支持,同时允许管理员编写自己的 DQL 和 SQL 查询以获取所需的数据。

安装

"velvel/report-bundle": "dev-master" 添加到您的 composer.json 文件中

注册包

<?php

// app/AppKernel.php

public function registerBundles {
    return array(
        // ...
        new Velvel\ReportBundle\VelvelReportBundle(),
        // ...
    );
}

添加路由

# app/config/routing.yml

velvel_report:
    resource: "@VelvelReportBundle/Resources/config/routing.xml"
    prefix: /reports

使用方法

创建一个扩展 Velvel\ReportBuilder\Builder\BaseReportBuilder 的报告文件

<?php

namespace Acme\DemoBundle\Report;

use Velvel\ReportBundle\Builder\BaseReportBuilder;
use Doctrine\ORM\QueryBuilder;

class OrdersReport extends BaseReportBuilder
{
    /**
     * Configures builder
     * 
     * This method configures the ReportBuilder. It has to return
     * a configured Doctrine QueryBuilder.
     *
     * @param \Doctrine\ORM\QueryBuilder $queryBuilder Query builder
     * 
     * @return \Doctrine\ORM\QueryBuilder
     */
    public function configureBuilder(QueryBuilder $queryBuilder)
    {
        $queryBuilder
            ->select('p.name, p.price, c.checkoutDate')
            ->from('Cart', 'c')
            ->join('c.items', 'i')
            ->join('i.product', 'p')
            ->add('where', 'c.checkoutDate > :from AND c.checkoutDate < :to');
        return $queryBuilder;
    }

    /**
     * Configures parameters
     *
     * This method configures parameters, which will be passed to
     * the QueryBuilder and the Form too, so the users (admins) can
     * change them.
     *
     * @return array
     */
    public function configureParameters()
    {
        $parameters = array(
            'from' => array(
                'value'   => new \DateTime('yesterday'), // default value
                'type'    => 'date', // form type
                'options' => array('label' => 'From'), // form options
            ),
            'to'   => array(
                'value'   => new \DateTime('now'),
                'type'    => 'date',
                'options' => array('label' => 'To'),
            ),
        );
        return $parameters;
    }

    /**
     * Configures modifiers
     *
     * If an element in the select statement returns an object without
     * a __toString() method implemented, it needs a modifier to be set.
     *
     * @return array
     */
    public function configureModifiers()
    {
        $modifiers = array(
            'checkoutDate' => array(
                'method' => 'format', // method to be called on the object
                'params' => array('Y/m/d H:i:s'), // method parameters in an array
            ),
        );

        return $modifiers;
    }
}

将您的新的 ReportBuilder 添加到您的 config.yml 文件中

velvel_report:
    reports:
        - { id: 'orders', name: 'Orders', class: 'Acme\DemoBundle\Report\OrdersReport' }

必须保证 id 的唯一性,它将在路由中使用。 name 将在报告列表中显示,而 class 是您的报告类。

配置

您可以重写显示和列表模板。

velvel_report:
    templates:
        show: 'VelvelReportBundle::show.html.twig' # this is the default show template
        list: 'VelvelReportBundle::list.html.twig' # this is the default list template