简体   繁体   English

SonataAdmin完成

[英]SonataAdmin onComplete

It's posible to handle the postFlush event or something like that? 是否可以处理postFlush事件或类似事件? I need to access to some data of the new register to generate another things, but must be after flush, because im using Gedmo Slug and one of data i need is the slug. 我需要访问新寄存器的某些数据以生成其他内容,但必须在刷新后进行,因为即时通讯使用Gedmo Slug,而我需要的数据之一是Slug。

Yes, create a listener in your services.yml/xml file and then the listener itself to change the code you need. 是的,在您的services.yml / xml文件中创建一个侦听器,然后在侦听器本身中更改所需的代码。

#src/Acme/Bundle/YourBundle/Resources/config/services.yml
services:
    contact_onflush.listener:
        class: Acme\Bundle\YourBundle\Listener\YourListener
        arguments: [@request_stack]
        tags:
            - { name: doctrine.event_listener, event: onFlush }
    contact_postflush.eventlistener:
        class: Acme\Bundle\YourBundle\Listener\YourListener
        tags:
            -  { name: doctrine.event_listener, event: postFlush}

In the listener class: 在侦听器类中:

<?php

namespace Acme\YourBundle\YourListener;

use Doctrine\Common\EventArgs;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\OnFlushEventArgs;
use Doctrine\ORM\Event\PostFlushEventArgs;
use Symfony\Component\HttpFoundation\RequestStack;

class YourListener implements EventSubscriber
{
    private $requestStack;
    private $needsFlush;

    public function __construct(Request $requestStack)
    {
        $this->requestStack= $requestStack;
        $this->needsFlush= false;
    }

    public function onFlush(OnFlushEventArgs $args)
    {
        $em = $args->getEntityManager();
        $uow = $em->getUnitOfWork();

        // we would like to listen on insertions and updates events
        $entities = array_merge(
            $uow->getScheduledEntityInsertions(),
            $uow->getScheduledEntityUpdates()
    );

    foreach ($entities as $entity) {
        // every time we update or insert a new [Slug entity] we do the work
        if ($entity instanceof Slug) {
            //modify your code here
            $x = new SomeEntity();
            $em->persist($x);
            //other modifications
            $this-needsFlush  = true;
            $uow->computeChangeSets();
        }
    }
}

public function postFlush(PostFlushEventArgs $eventArgs) {
    if ($this->needsFlush) {
        $this->needsFlush = false;
        $eventArgs->getEntityManager()->flush();
    }
}

You may be able to use computeChangeSet (singular) but I had problems using it. 您也许可以使用computeChangeSet(单数),但是在使用它时遇到了问题。 Instead of using onFlush, you can use preUpdate to find the fields that changed, but this event has restrictions when trying to persist and you need to pair it with something like the needFlush to trigger a postFlush. 您可以使用preUpdate来查找已更改的字段,而不是使用onFlush,但是此事件在尝试保留时会受到限制,您需要将其与needFlush之类的东西配对以触发postFlush。

If you still have errors, can you post more of you code to show what you are modifying? 如果仍然有错误,是否可以发布更多代码以显示正在修改的内容?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM