Nico
Nico

Reputation: 505

Symfony preUpdate event in SonataAdminBundle

I have Products entity with fields

I need feature, that when user editing in SonataAdminBundle purch_price_net field, purch_price_gross [and other fields] automatically change their values.

So I created PreUpdateProducts listener:

    <?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
                }
            }
        }
    }

and in services.yml added:

services:
[...]

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

Unfortunatelly it doesn't work, nothing changes [apart purchPriceNet] after pressing 'Update'. How can I get it to work?

Upvotes: 2

Views: 702

Answers (1)

Nico
Nico

Reputation: 505

OK thanks.

I did it in this way:

in Products class added annotation:

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

and my PreUpdateProduct class look like:

<?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);
            }
        }
    }
}

Now it works.

Upvotes: 1

Related Questions