1

I want to put a number into a NumberType field, but when I get this exception:

Expected value of type "GestionBundle\Entity\MaterialCost" for association field "GestionBundle\Entity\Intervention#$materialCost", got "double" instead.

Intervention

namespace GestionBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\OneToOne;

/**
 * @ORM\Entity
 */
class Intervention
{
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="comments", type="text", nullable=true)
     */
    private $comments;

    /**
     * One Intervention has One materialCost.
     * @OneToOne(targetEntity="MaterialCost", inversedBy="intervention", cascade={"persist"}) 
     */
    private $materialCost;

    public function __toString() {
        if (is_null($this->comments)) {
            return '';
        }
        return $this->comments;
    }
}

MaterialCost

namespace GestionBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 */
class MaterialCost
{
    /*
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var float
     * @ORM\Column(name="material_cost", type="float")
     */
    private $materialCost;

    /**
     * @ORM\OneToOne(targetEntity="Intervention", mappedBy="materialCost") 
     */
    private $intervention;

    public function __toString() {
        return (string) $this->materialCost;  
    }
}

InterventionType

namespace GestionBundle\Form;

use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class InterventionType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('materialCost',  NumberType::class, ['required' => true])
                ->add('comments', TextareaType::class, ['required' => false])
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'GestionBundle\Entity\MaterialCost'
        ));
    }
}
4

1 回答 1

1

很明显,您应该将对象传递给实体对象的materialCost属性Intervention,但您却给了它一个数字,这是错误的。

我认为您最好完全丢弃MaterialCost实体类并像这样更改Intervention属性materialCost映射:

干涉

/**
 * Intervention has a materialCost value.
 * @ORM/Column(type="float", name="material_cost") 
 */
private $materialCost;

但是,如果您确定要使用该 MaterialCost实体类,则可以这样做:

干预类型

namespace GestionBundle\Form;

use Doctrine\ORM\EntityRepository;
use GestionBundle\Entity\Intervention;
use GestionBundle\Entity\MaterialCost;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class InterventionType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('materialCost',  EntityType::class, [
                'required' => true,
                'class' => MaterialCost::class,
                'choice_label' => 'materialCost',
                ])
            ->add('comments', TextareaType::class, ['required' => false])
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => Intervention::class
        ));
    }
}

这样,您可以选择一些实例MaterialCost并将其分配给Intervention. 如果您希望能够输入一个新数字,同时在表单MaterialCost内部创建一个对象InterventionType,您需要使用另一个名为原型的方法。

考虑阅读如何嵌入表单集合

于 2018-10-27T07:44:10.743 回答