Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comparison of the diff from decimal fields are being applied even when value is the same #299

Open
pedrocasado opened this issue Mar 7, 2022 · 4 comments
Labels
bug Something isn't working

Comments

@pedrocasado
Copy link

pedrocasado commented Mar 7, 2022

Q A
auditor-bundle version 5.0.0
PHP version 7.4
Database MySQL

Summary

I have an entity with a column of type decimal. When i save the entity, the values from the decimal type does diff even if they haven't changed because the comparison it not working properly.

It compares 60 with 60.00 from database and then it diffs. I'm using the Symfony MoneyType.

Any suggestion how to prevent this?

Current behavior

How to reproduce

Create a property with decimal type.

/*
* @ORM\Column(name="value", type="decimal", precision=12, scale=2, nullable=true)
*/
private $value;

Try to use in any form.

$builder->add('value', MoneyType::class, [
    'currency' => false,
]);

Expected behavior

The diff should not be saved as the value is the same.

@pedrocasado pedrocasado added the bug Something isn't working label Mar 7, 2022
@DamienHarper
Copy link
Owner

DamienHarper commented Mar 8, 2022

@pedrocasado Thanks for the report, could you please tell me what scale and divisor value do you use with your MonyType objects?

Edit: it seems that you use default values (scale = 2 and divisor = 1). I need to have a deeper look at the problem.

@pedrocasado
Copy link
Author

@DamienHarper , yes. I'm not passing then, so i'm using the default ones.

$resolver->setDefaults([
    'scale' => 2,
    'grouping' => false,
    'rounding_mode' => \NumberFormatter::ROUND_HALFUP,
    'divisor' => 1,
    'currency' => 'EUR',
    'compound' => false,
    'html5' => false,
    'invalid_message' => function (Options $options, $previousValue) {
        return ($options['legacy_error_messages'] ?? true)
            ? $previousValue
            : 'Please enter a valid money amount.';
    },
]);

@tuxes3
Copy link

tuxes3 commented Mar 14, 2022

It seems that this bug is coming from the Doctrine Bundle. I found this stackoverflow link referencing the same issue: https://stackoverflow.com/questions/33773066/symfony-doctrine-detects-change-in-money-field-even-when-no-change-has-occurr

A quick fix would be creating an onAuditEvent-listener and remove the payload if they are the same.

namespace App\EventSubscriber;

use DH\Auditor\Event\LifecycleEvent;
use JsonException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class AuditSubscriber implements EventSubscriberInterface
{

    public static function getSubscribedEvents()
    {
        return [
            LifecycleEvent::class => 'onAuditEvent',
        ];
    }

    public function onAuditEvent(LifecycleEvent $event)
    {
        if (isset($event->getPayload()['entity'], $event->getPayload()['diffs'])) {
            try {
                $payload = $event->getPayload();
                $diffs = json_decode($payload['diffs'], true, 512, JSON_THROW_ON_ERROR);
                foreach ($diffs as $key => $values) {
                    if ((is_array($values) && array_key_exists('new', $values) && array_key_exists('old', $values))
                        && (is_int($values['new']) || is_float($values['new']))
                            && (is_string($values['old'])
                                && ((float) $values['new']) === ((float) $values['old']))
                    ) {
                        unset($diffs[$key]);
                    }
                }
                if (empty($diffs)) {
                    $event->stopPropagation();
                }
                $payload['diffs'] = json_encode($diffs, JSON_THROW_ON_ERROR);
                $event->setPayload($payload);
            } catch (JsonException $e) {
            }
        }
    }
}

@HrjSnz
Copy link

HrjSnz commented May 9, 2022

This subscriber not working.. Always if diff is empty still log with bad values. If i changed it to something else, it log always something else. But can not get result log nothing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants