SonataAdminBundle : 显示非 crud (统计) [英] SonataAdminBundle : display non crud (statistics)

查看:29
本文介绍了SonataAdminBundle : 显示非 crud (统计)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Sonata 管理包来生成我的后端,我对它非常满意,我也想使用我的后端来显示统计数据.

I'm using sonata admin bundle to generate my backend, I'm so happy with it that I would like to use my backend to display statistics as well.

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

I guess I can do that by tweaking bundle's views, "standard_layout.html.twig" maybe.

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

Problem is, I can't find examples or even people speaking about it, so I'm wondering, is that possible ? Aren't people speaking about it because it's too simple ? Did you do it ?

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

I really would like to have a single backend, so pls enlighten me !

谢谢,科普

推荐答案

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

Since pulzarraider explained us one way to do this I'll explain the other.

块包的方式允许以非常强大的方式自定义仪表板.您可以同时关注Block bundle doc

The block bundle's way allow to custom the dashboard in a pretty powerful way. You can follow Block bundle doc at the same time

1.在 CopndzMyBundleBlockService

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

I want to display stats by doing maths with data stored : I need to

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

namespace CopndzMyBundleBlockService;
use SymfonyComponentHttpFoundationResponse;
use SonataAdminBundleFormFormMapper;
use SonataAdminBundleValidatorErrorElement;
use SonataBlockBundleModelBlockInterface;
use SonataBlockBundleBlockBaseBlockService;
use DoctrineORMEntityManager;

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.在 MyBundleRessourcesconfigservices.yml

 sonata.block.service.statistics:
      class: CopndzMyBundleBlockServiceStatisticsBlockService
      tags:
        - { name: sonata.block }
      arguments:
        - "sonata.block.service.statistics"
        - @templating
        - @doctrine.orm.entity_manager

3.将此服务添加到我的 config.yml 中的 Sonata_block

sonata_block:
    default_contexts: [cms]
    blocks:
        sonata.admin.block.admin_list:
            contexts:   [admin]

        sonata.block.service.text:
        sonata.block.service.rss:
        sonata.block.service.statistics:

4.在 CopndzMyBundleRessourcesviewsBlock

{% extends sonata_block.templates.block_base %}

{% block block %}
    {{ myentity.name }}
{% endblock %}

5.最后调用config.yml中admin bundle配置中的服务

sonata_admin:
    dashboard:
        blocks:
            # display a dashboard block
            - { position: left, type: sonata.admin.block.admin_list }
            - { position: right, type: sonata.block.service.statistics }

这篇关于SonataAdminBundle : 显示非 crud (统计)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆