我在数据库中有一个int(1)的实体字段 实体定义:
class myEntity {
/**
* @var int|null
* @ORM\Column(name="ASSURANCE", type="integer", nullable=true)
*/
private $assurance ;
public function getAssurance(): ?int
{
return $this->assurance;
}
public function setAssurance(?int $assurance): self
{
$this->assurance = $assurance;
return $this;
}
}
我建立了这样的编辑表单:
class MatriceType extends AbstractType
{ public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('assurance',CheckboxType::class,[
'label' => 'Assurance',
'required' => false,
]);
}
}
我的控制器:
class MatriceController extends AbstractController
{
public function editMatrice($id, Request $request, EntityManagerInterface $em){
$matrice = $em->getRepository(Matrice::class)->find($id);
$form = $this->createForm( MatriceType::class,$matrice);
$form->handleRequest($request);
if($form->isSubmitted() $form->isValid()) {
return $this->redirectToRoute('editMatrice',[
'id' =>$matrice->getIdMatrice(),
]);
}
return $this->render('matrice/edit.html.twig', [
'form' => $form->createView()
]);
}
保证在MySQL中的$ matrice中存储为int(1),它作为布尔值检索,但是在createForm(MatriceType :: class,$ matrice)中,相同的属性变为0!
消息: “无法转换属性路径“保证”的值:预期为布尔值。 “