我最近在学习 Symfony。我遇到了一个错误,我打电话时找不到认真的答案。
错误是这样的: 未捕获的PHP 异常 Symfony \ Component \ PropertyAccess \ Exception \ InvalidArgumentException:“类型的预期参数”int或null“,”对象“在属性路径”parent_id“。” 在 /home/vagrant/code/vendor/symfony/property-access/PropertyAccessor.php 第 198 行
我的控制器文件中有这些代码:
public function new(Request $request): Response
{
$category = new Category();
$form = $this->createForm(CategoryType::class, $category);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($category);
$entityManager->flush();
return $this->redirectToRoute('category_index');
}
return $this->render('category/new.html.twig', [
'category' => $category,
'form' => $form->createView(),
]);
}
我的 FormType 文件如下所示:
<?php
namespace App\Form;
use App\Entity\Category;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class CategoryType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', null, [
'attr' => ['class' => 'form-control'],
])
->add('slug', null, [
'attr' => ['class' => 'form-control'],
])
->add('parent_id', EntityType::class, [
'class' => Category::class,
'choice_label' => 'name',
'attr' => ['class' => 'form-control'],
'placeholder' => 'Üst Kategori Seçiniz',
])
->add('title', null, [
'attr' => ['class' => 'form-control']
])
->add('description', null, [
'attr' => ['class' => 'form-control']
])
;
}
慕神8447489