我想在NumberType
字段中输入数字,但是当出现此异常时:
关联字段“ GestionBundle \ Entity \ Intervention#$ materialCost”的类型为“ GestionBundle \ Entity \ MaterialCost”的预期值改为为“ double”。
干预
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;
}
}
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'
));
}
}
答案 0 :(得分:1)
很明显,您应该将一个对象传递给materialCost
实体对象的Intervention
属性,但是您给它提供了一个数字,这是错误的。
我认为您最好完全放弃MaterialCost
实体类,并像这样更改Intervention
的{{1}}属性映射:
干预
materialCost
但是,如果您确定要拥有该/**
* Intervention has a materialCost value.
* @ORM/Column(type="float", name="material_cost")
*/
private $materialCost;
实体类,则可以采用以下方法:
InterventionType
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
对象,则可以使用另一种称为 prototype 的方法