SonataAdminBundle 中的 Symfony preUpdate 事件

Posted

技术标签:

【中文标题】SonataAdminBundle 中的 Symfony preUpdate 事件【英文标题】:Symfony preUpdate event in SonataAdminBundle 【发布时间】:2016-12-28 19:06:27 【问题描述】:

我有带有字段的 Products 实体

姓名, purch_price_net purch_price_gross purch_vat_value purch_vat_rate_id [来自其他表]

我需要功能,当用户在 SonataAdminBundle purch_price_net 字段中编辑时,purch_price_gross [和其他字段] 会自动更改它们的值。

所以我创建了PreUpdateProducts 监听器:

    <?php

    namespace AppBundle\EventListener;

    use Doctrine\ORM\Events;
    use AppBundle\Entity\Products;

    // echo Events::preUpdate;
    class PreUpdateProducts 
        public function preUpdate(PreUpdateEventArgs $eventArgs) 
            if ($eventArgs->getEntity () instanceof Products) 
                if ($eventArgs->hasChangedField ( 'purchPriceNet' )) 
                    $newPurchPriceNet = $eventArgs->getNewValue ( 'purchPriceNet' );


                    $eventArgs->setNewValue ( 'purchPriceGross', $newPurchPriceNet * 1.23 );
                    $eventArgs->setNewValue ( 'name', 'changedName' ); // for tests
                
            
        
    

并在 services.yml 中添加:

services:
[...]

    my.listener:
        class: AppBundle\EventListener\PreUpdateProducts
        tags:
            -  name: doctrine.event_listener, event: PreUpdateProducts 

不幸的是,它不起作用,按下“更新”后没有任何变化 [除了 purchPriceNet]。 我怎样才能让它工作?

【问题讨论】:

如何触发事件PraUpdateProducts?因为在 Symfony/Doctrine 中没有这样的事件。生命周期事件应该在实体的映射中定义。 docs.doctrine-project.org/projects/doctrine-orm/en/latest/… (可能)跑题了,但在实体名称中使用复数是自找麻烦。 【参考方案1】:

好的,谢谢。

我是这样做的:

在 Products 类中添加注解:

* @ORM\EntityListeners("AppBundle\EventListener\PreUpdateProduct")

我的 PreUpdateProduct 类看起来像:

<?php

namespace AppBundle\EventListener;

use Doctrine\ORM\Event\PreUpdateEventArgs;
use Doctrine\ORM\Mapping as ORM;
use AppBundle\Entity\Products;
class PreUpdateProduct 

    /**
     * @ORM\PreUpdate
     */
    public function preUpdate(Products $product, PreUpdateEventArgs $event) 
        if ($event->getEntity () instanceof Products) 
            if ($event->hasChangedField ( 'purchPriceNet' )) 
                $newPurchPriceNet = $event->getNewValue ( 'purchPriceNet' );
                $purchVatRateObj=$product->getPurchVatRate();
                $purchVatRate=$purchVatRateObj->getVatRate();
                $purchVatValue=$newPurchPriceNet*$purchVatRate;
                $product->setPurchVatValue($purchVatValue);
                $product->setPurchPriceGross ( $newPurchPriceNet +$purchVatValue );
            
            if ($event->hasChangedField ( 'sellPriceGross' )) 
                $newSellPriceGross = $event->getNewValue ( 'sellPriceGross' );
                $sellVatRateObj=$product->getSellVatRate();
                $sellVatRate=$sellVatRateObj->getVatRate();
                $sellPriceNet=$newSellPriceGross/(1+$sellVatRate);
                $sellVatValue=$newSellPriceGross-$sellPriceNet;
                $product->setSellVatValue($sellVatValue);
                $product->setSellPriceNet ( $sellPriceNet);
            
        
    

现在可以了。

【讨论】:

以上是关于SonataAdminBundle 中的 Symfony preUpdate 事件的主要内容,如果未能解决你的问题,请参考以下文章

SonataAdminBundle 自定义呈现列表中的文本字段

SonataAdminBundle 中的 Symfony preUpdate 事件

在没有 SonataUserBundle 的情况下访问 SonataAdminBundle 中的管理面板

如何添加链接以在 SonataAdminBundle 中的关系字段上显示关系实体的操作

SonataAdminBundle 如何获取当前的管理类?

如何在 SonataAdminBundle 中使用角色