0

我最近在学习 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']
            ])
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Category::class,
            'required' => false

        ]);
    }
}

我的实体文件的内容如下

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/**
 * @ORM\Entity(repositoryClass="App\Repository\CategoryRepository")
 */
class Category
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $name;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $slug;

    /**
     * @ORM\Column(type="integer", nullable=true)
     */
    private $parent_id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $title;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $description;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(?string $name): self
    {
        $this->name = $name;

        return $this;
    }

    public function getSlug(): ?string
    {
        return $this->slug;
    }

    public function setSlug(?string $slug): self
    {
        $this->slug = $slug;

        return $this;
    }

    public function getParentId(): ?int
    {
        return $this->parent_id;
    }

    public function setParentId(?int $parent_id): self
    {
        $this->parent_id = $parent_id;
        return $this;
    }

    public function getTitle(): ?string
    {
        return $this->title;
    }

    public function setTitle(string $title): self
    {
        $this->title = $title;

        return $this;
    }

    public function getDescription(): ?string
    {
        return $this->description;
    }

    public function setDescription(?string $description): self
    {
        $this->description = $description;

        return $this;
    }

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

    public static function loadValidatorMetadata(ClassMetadata $metadata)
    {
        $metadata->addPropertyConstraint('name', new Assert\NotBlank());
        $metadata->addPropertyConstraint('title', new Assert\NotBlank());
        $metadata->addPropertyConstraint('description', new Assert\NotBlank());
        $metadata->addPropertyConstraint('slug', new Assert\NotBlank());
        $metadata->addConstraint(new UniqueEntity([
            'fields' => 'slug',
        ]));
    }
}

如果您能帮助我,我将不胜感激。方便...

4

1 回答 1

3

问题是您没有正确配置您的实体或表单。目前,在您的实体parent_id中定义如下:

/**
 * @ORM\Column(type="integer", nullable=true)
 */
private $parent_id;

在您的表单中,您期望Category那里有另一个对象:

->add('parent_id', EntityType::class, [
    'class' => Category::class,

您需要parent_id在您的实体中建立一对多关系(其中一个Category可以是另一个的父类别)或使表单中的该字段成为常规ChoiceType和手动提供的选择(通过从数据库中获取可能的类别 ID)。您将在控制器操作中获取这些 id,并将它们$this->createForm作为第三个参数提供给options.

于 2020-03-22T23:26:39.273 回答