简体   繁体   中英

How to prevent numbers from rounding down in Sonata-admin?

Good Morning Everybody. I'm learning development and I have a problem on my first symfony4 project.

It's a wineshop. My aim is to permit the vinegrower to add products data on database through Sonata-admin interface, then render that data on the shop webpage. This process is working correctly.

I have an Entity named 'Product' linked to Sonata-admin with different variables. One is a price for the product ('prix') and another one is the volume in Liters ('contenance'). They are typed as 'float'. In the 'ProductAdmin' page, where the form shown in back-office is constructed, they are Typed as 'Moneytype' and 'NumberType'.

My problem is : in the 'add Product' form, in Sonata-admin interface, when the vinegrower will enter a price and a volume, those numbers will always be rounded as integers (after he sent form)... And written in this format to the database... And rendered in this format to the webpage.

I have tried to add 'scale' option in the 'formMapper'. It's actually working... until I validate the form in Sonata-admin !

I've tried to change Entity variable type to 'decimal', to add 'precision' and 'scale' options... in vain.

in Product Entity :

 /**
     * @ORM\Column(type="decimal", precision=3, scale=2)
     */
    private $contenance;

    /**
     * @ORM\Column(type="decimal", precision=3, scale=2)
     */
    private $prix;

in Product Admin

<?php

namespace App\Admin;

use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\MoneyType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;

final class ProduitAdmin extends AbstractAdmin
{
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper->add('titre', TextType::class);
        $formMapper->add('millesime', TextType::class);
        $formMapper->add('cepage', TextType::class);
        $formMapper->add('appellation', TextType::class);
        $formMapper->add('description', TextType::class);
        $formMapper->add('contenance', NumberType::class, ['scale' => 3]);
        $formMapper->add('prix', MoneyType::class, ['scale' => 3]);
        $formMapper->add('classe', TextType::class);
    }

As none of those tips worked, and as the rounding phase happens in Sonata-admin interface, I think the cause of the problem is part of Sonata-admin. I explored the 'vendor' repertory but didn't find anything about 'types' or 'rounding'...

Any idea on how to solve this ?

Thank you in advance...

Thomas

ANSWER :

Well, SonataAdmin just prepare numbers for database... which can not contain float/decimal numbers !!!

So... everything is normal.

I just divided my numbers by 100 before rendering them... It seems to be the normal way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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