Sonata Admin奇怪的形式行为

问题描述 投票:0回答:1

我已经开始使用Symfony 4.x进行游戏,我决定使用traits来避免代码重复。

namespace App\Traits;

use App\Entity\Brand;

/**
 * Trait ToolTrait
 * @package App\Traits
 */
trait ToolTrait
{
    /**
     * @var null|int
     */
    private $id;

    /**
     * @var null|Brand
     */
    private $brand;

    /**
     * @var null|string
     */
    private $name;

    /**
     * @var null|string
     */
    private $image;

    /**
     * @var float
     */
    private $moneyPrice = 0.00;

    /**
     * @var int
     */
    private $gemsPrice = 0;

    /**
     * @var null|float
     */
    private $maintenance = 0.00;

    /**
     * @var boolean
     */
    private $limited = false;

    /**
     * @var int
     */
    private $leftSales = 0;

    /**
     * @var int
     */
    private $totalSales = 0;

    /**
     * @var bool
     */
    private $enabled = true;

    /**
     * @return int|null
     */
    public function getId(): ?int
    {
        return $this->id;
    }

    /**
     * @param int|null $id
     *
     * @return ToolTrait
     */
    public function setId(?int $id): self
    {
        $this->id = $id;

        return $this;
    }

    /**
     * @return Brand|null
     */
    public function getBrand(): ?Brand
    {
        return $this->brand;
    }

    /**
     * @param Brand|null $brand
     *
     * @return ToolTrait
     */
    public function setBrand(?Brand $brand): self
    {
        $this->brand = $brand;

        return $this;
    }

    /**
     * @return null|string
     */
    public function getName(): ?string
    {
        return $this->name;
    }

    /**
     * @param null|string $name
     *
     * @return ToolTrait
     */
    public function setName(?string $name): self
    {
        $this->name = $name;

        return $this;
    }

    /**
     * @return null|string
     */
    public function getImage(): ?string
    {
        return $this->image;
    }

    /**
     * @param null|string $image
     *
     * @return ToolTrait
     */
    public function setImage(?string $image): self
    {
        $this->image = $image;

        return $this;
    }

    /**
     * @return float|null
     */
    public function getMoneyPrice(): ?float
    {
        return $this->moneyPrice;
    }

    /**
     * @param float|null $moneyPrice
     *
     * @return ToolTrait
     */
    public function setMoneyPrice(?float $moneyPrice): self
    {
        $this->moneyPrice = $moneyPrice;

        return $this;
    }

    /**
     * @return int|null
     */
    public function getGemsPrice(): ?int
    {
        return $this->gemsPrice;
    }

    /**
     * @param int|null $gemsPrice
     *
     * @return ToolTrait
     */
    public function setGemsPrice(?int $gemsPrice): self
    {
        $this->gemsPrice = $gemsPrice;

        return $this;
    }

    /**
     * @return float|null
     */
    public function getMaintenance(): ?float
    {
        return $this->maintenance;
    }

    /**
     * @param float|null $maintenance
     *
     * @return ToolTrait
     */
    public function setMaintenance(?float $maintenance): self
    {
        $this->maintenance = $maintenance;

        return $this;
    }

    /**
     * @return bool
     */
    public function isLimited(): bool
    {
        return $this->limited;
    }

    /**
     * @param bool $limited
     *
     * @return ToolTrait
     */
    public function setLimited(bool $limited): self
    {
        $this->limited = $limited;

        return $this;
    }

    /**
     * @return int
     */
    public function getLeftSales(): int
    {
        return $this->leftSales;
    }

    /**
     * @param int $leftSales
     *
     * @return ToolTrait
     */
    public function setLeftSales(int $leftSales): self
    {
        $this->leftSales = $leftSales;

        return $this;
    }

    /**
     * @return int
     */
    public function getTotalSales(): int
    {
        return $this->totalSales;
    }

    /**
     * @param int $totalSales
     *
     * @return ToolTrait
     */
    public function setTotalSales(int $totalSales): self
    {
        $this->totalSales = $totalSales;

        return $this;
    }

    /**
     * @return bool
     */
    public function isEnabled(): bool
    {
        return $this->enabled;
    }

    /**
     * @param bool $enabled
     *
     * @return ToolTrait
     */
    public function setEnabled(bool $enabled): self
    {
        $this->enabled = $enabled;

        return $this;
    }

    /**
     * @return null|string
     */
    public function __toString()
    {
        return $this->name ?? '-';
    }
}

这是我的trait。我没有在这里添加Doctrine映射。为此,我为每个类创建了一个文件,并在那里设置了映射(XML)。映射很好,因为没有错误,我使用HeidiSQL手动检查它。

Form Validator

问题是我提交表格时。我的一些房产被认为是实体的一部分,有些则不是。这很奇怪,因为我没有任何孩子,实体只实现这个特性。任何的想法?

真正奇怪的是'fileTmp'属性是从另一个特征加载的,但它没有映射(仅用于加载图像)。

我必须说,对于那些字段,我有一个基于Symfony文档实现的自定义字段类型。如果我删除它,一切正常。

<?php

namespace App\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
 * Class HorsePowerType
 * @package App\Form\Type
 */
class HorsePowerType extends AbstractType
{
    /** {@inheritdoc} */
    public function buildView(FormView $view, FormInterface $form, array $options)
    {
        $view->vars['icon'] = 'HP';
    }

    /** {@inheritdoc} */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'error_bubbling' => false
        ]);
    }

    /** {@inheritdoc} */
    public function getBlockPrefix()
    {
        return 'horse_power';
    }
}

和模板:

{% block horse_power_widget %}
    {% spaceless %}
        <div class="input-group">
            {{ block('form_widget_simple') }}
            <span class="input-group-addon">{{- icon|raw -}}</span>
        </div>
    {% endspaceless %}
{% endblock %}

P.S:这个实体中唯一的额外字段是:horsePower,fuelTank,maxSpeed,maxCapacity。

php symfony sonata-admin
1个回答
0
投票

我解决了在我的字段类型中添加compound => falseconfigureOptions

© www.soinside.com 2019 - 2024. All rights reserved.