Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
719 views
in Technique[技术] by (71.8m points)

symfony - Getting instance of container in custom sonata block

I am creating a custom block for dashboard and where I want to display information persisted into the DB. How do I get an instance of the container or doctrine entity manager in the block service?

Tried googling alot but nothing substantial has come out so far

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

When you create a new block in sonata, you have to declare it like a service, so you can inject doctrine.orm.entity_manager. I can show you an example of a block where I injected the entity manager:

//MyBundleBlockMyBlockService

use SymfonyComponentHttpFoundationResponse;
use SonataAdminBundleFormFormMapper;
use SonataAdminBundleValidatorErrorElement;
use SonataBlockBundleModelBlockInterface;
use SonataBlockBundleBlockBaseBlockService;
use SonataBlockBundleBlockBlockContextInterface;

class MyBlockService extends BaseBlockService
{
    protected $em;

    public function __construct($type, $templating, $em)
    {
        $this->type = $type;
        $this->templating = $templating;
        $this->em = $em;
    }

    public function getName()
    {
        return 'MyBlock';
    }

    public function getDefaultSettings()
    {
        return array();
    }

    public function validateBlock(ErrorElement $errorElement, BlockInterface $block)
    {
    }

    public function buildEditForm(FormMapper $formMapper, BlockInterface $block)
    {
    }

    public function execute(BlockContextInterface $blockContext, Response $response = null)
    {
        $settings = array_merge($this->getDefaultSettings(), $blockContext->getBlock()->getSettings());

        $data = count($this->em->getRepository("MyBundle:Entity")->findAll());

        return $this->renderResponse('MyBundle::myblock.html.twig', array(
            'block' => $blockContext->getBlock(),
            'settings' => $settings,
            'data' => $data,
        ), $response);
    }
}

Declare you block in services.yml and inject whatever you need:

//services.yml
sonata.block.service.myblock:
        class: MyBundleBlockMyBlockService
        arguments: [ "sonata.block.service.myblock", @templating, @doctrine.orm.entity_manager ]
        tags:
            - { name: sonata.block }

Declare you block in config.yml: //config.yml

sonata_block:
    default_contexts: [cms]
    blocks:
    # Enable the SonataAdminBundle block
    sonata.admin.block.admin_list:
        contexts:   [admin]
    sonata.block.service.myblock: ~

And then, of course, you have to create the template for block:

{# myblock.html.twig #}
{% extends 'SonataBlockBundle:Block:block_base.html.twig' %}


{% block block %}
   <p>{{ data }}</p>
{% endblock %}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...