关联字段类型的期望值,取而代之的是 "double"

Expected value of type for association field , got "double" instead

我想在 NumberType 字段中输入一个数字,但是当我得到这个异常时:

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

干预

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

材料成本

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

干预类型

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

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

我认为你最好完全抛弃 MaterialCost 实体 class 并像这样更改 InterventionmaterialCost 属性 映射:

干预

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

但是,如果您确定 MaterialCost 实体 class 就位,则方法如下:

干预类型

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。如果您希望能够输入一个新数字,同时在 InterventionType 表单中创建一个 MaterialCost 对象,您需要另一个名为 prototype[= 的方法40=].

为此考虑阅读 How to Embed a Collection of Forms