【问题标题】:Symfony preUpdate event in SonataAdminBundleSonataAdminBundle 中的 Symfony preUpdate 事件
【发布时间】: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]。 我怎样才能让它工作?

【问题讨论】:

标签: symfony doctrine-orm sonata-admin sonata


【解决方案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);
            }
        }
    }
}

现在可以了。

【讨论】:

    猜你喜欢
    • 2014-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多