SonataAdminBundle:显示非增删改查(统计)

问题描述 投票:0回答:2

我正在使用sonata管理包来生成我的后端,我对此非常满意,所以我也想使用我的后端来显示统计数据。

我想我可以通过调整包的视图来做到这一点,也许是“standard_layout.html.twig”。

问题是,我找不到例子,甚至找不到人谈论它,所以我想知道,这可能吗?人们谈论它不是因为它太简单了吗?你做到了吗?

我真的很想要一个后端,所以请赐教!

谢谢你, 科普德兹

php symfony sonata-admin symfony-2.2
2个回答
38
投票

是的,这是可能的。可以使用 Sonata Block 或使用您自己的控制器来完成。

如果您使用控制器,您可以从默认 CRUD 控制器重载(一个或多个)操作,渲染结果的外观取决于您。

  1. 将默认控制器

    SonataAdminBundle:CRUD
    替换为您的控制器AcmeDemoAdminBundle:管理服务定义中的ProductStatisticsAdmin并删除实体,因为我们将尝试在不进行CRUD操作的情况下呈现统计数据。

    <service id="acme_demo_admin.product_statistics" class="Acme\Bundle\DemoAdminBundle\Admin\ProductStatisticsAdmin">
        <tag name="sonata.admin" manager_type="orm" group="statistics_group" label_catalogue="admin" label="Product Statistics" />
        <argument />
        <argument />
        <argument>AcmeDemoAdminBundle:ProductStatisticsAdmin</argument>
    </service>
    
  2. ProductStatisticsAdmin

    中创建管理服务
    Acme/Bundle/DemoAdminBundle/Admin/ProductStatisticsAdmin.php
    。该类将非常简单,因为我们只需要
    list
    操作,不需要其他 CRUD 操作。

    <?php
    namespace Acme\Bundle\DemoAdminBundle\Admin;
    
    use Sonata\AdminBundle\Admin\Admin;
    use Sonata\AdminBundle\Route\RouteCollection;
    
    class ProductStatisticsAdmin extends Admin
    {
        protected $baseRoutePattern = 'product-statistics';
        protected $baseRouteName = 'productStatistics';
    
        protected function configureRoutes(RouteCollection $collection)
        {
            $collection->clearExcept(array('list'));
        }
    }
    
  3. Acme/Bundle/DemoAdminBundle/Controller/ProductStatisticsAdminController.php

     中创建控制器
    ProductStatisticsAdminController 并从 Sonata 的 CRUDController 重载
    listAction()
    。在此操作中,您可以调用数据库并检索统计信息,然后使用模板呈现它们。

    <?php
    
    namespace Acme\Bundle\DemoAdminBundle\Controller;
    
    use Sonata\AdminBundle\Controller\CRUDController as Controller;
    use Symfony\Component\Security\Core\Exception\AccessDeniedException;
    
    class ProductStatisticsAdminController extends Controller
    {
        public function listAction()
        {
            if (false === $this->admin->isGranted('LIST')) {
                throw new AccessDeniedException();
            }
    
            //... use any methods or services to get statistics data
            $statisticsData = ...
    
           return $this->render('AcmeDemoAdminBundle:ProductStatistics:product_statistics.html.twig', array(
                        'statistics_data'  => $statisticsData,
                    ));
        }
    }
    
  4. 创建模板

    product_statistics.html.twig
    生成图表并在
    Acme/Bundle/DemoAdminBundle/Resources/views/ProductStatistics/product_statistics.html.twig

    中显示统计数据
    {% extends base_template %}
    
    {% block javascripts %}
        {{ parent() }}
        {# put links to javascript libraries here if you need any #}
    {% endblock %}
    
    {% block content %}
        {# put some html code to display statistics data or use some javascript library to generate cool graphs #}
    {% endblock %}
    

20
投票

既然 pulzarraider 向我们解释了一种方法,我将解释另一种方法。

块捆绑的方式允许以非常强大的方式自定义仪表板。 您可以同时关注区块捆绑文档

1。在Copndz\MyBundle\Block\Service

中创建StatisticsBlockService.php

我想通过对存储的数据进行数学运算来显示统计数据:我需要

  • 导入EntityManager
  • 将属性 $em 添加到服务
  • 添加构造函数 __construct ,它将调用其父构造函数并使用传入参数的 EntityManager 设置 $em

namespace Copndz\MyBundle\Block\Service;
use Symfony\Component\HttpFoundation\Response;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Validator\ErrorElement;
use Sonata\BlockBundle\Model\BlockInterface;
use Sonata\BlockBundle\Block\BaseBlockService;
use Doctrine\ORM\EntityManager;

class StatisticsBlockService extends BaseBlockService
{
    private $em;
    
    /**
     * {@inheritdoc}
     */
    public function execute(BlockInterface $block, Response $response = null)
    {
        $settings = array_merge($this->getDefaultSettings(), $block->getSettings());
        
        $myentityrepository = $this->em->getRepository('CopndzMyBundle:MyEntity');
        $myentity = $myentityrepository->find('5');
        
        return $this->renderResponse('CopndzMyBundle:Block:block_statistics.html.twig', array(
            'block'     => $block,
            'settings'  => $settings,
            'myentity' => $myentity,   
        ), $response);
    }

    /**
     * {@inheritdoc}
     */
    public function validateBlock(ErrorElement $errorElement, BlockInterface $block)
    {
        // TODO: Implement validateBlock() method.
    }

    /**
     * {@inheritdoc}
     */
    public function buildEditForm(FormMapper $formMapper, BlockInterface $block)
    {
        $formMapper->add('settings', 'sonata_type_immutable_array', array(
            'keys' => array(
                array('content', 'textarea', array()),
            )
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function getName()
    {
        return 'Text (core)';
    }

    /**
     * {@inheritdoc}
     */
    public function getDefaultSettings()
    {
        return array(
            'content' => 'Insert your custom content here',
        );
    }
    
    public function __construct($name, $templating, EntityManager $entityManager)
    {
            parent::__construct($name, $templating);
            $this->em = $entityManager;
    }
}

2。在 MyBundle\Ressource 中创建服务

© www.soinside.com 2019 - 2024. All rights reserved.