【问题标题】:Expected argument of type "string", "App\Entity" given给定“字符串”、“App\Entity”类型的预期参数
【发布时间】:2019-01-30 16:56:57
【问题描述】:

我尝试插入具有 OneToMany-ManyToOne 关系的记录,但出现错误。

Expected argument of type "string", "App\Entity\Question" given.

我有下一个实体 questionanswer

class Question
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;
/**
 * @ORM\Column(type="text")
 */
private $title;

/**
 * @ORM\OneToMany(targetEntity="App\Entity\Answer", mappedBy="question", orphanRemoval=true)
 */
private $answers;

public function __construct()
{
    $this->answers = new ArrayCollection();
}

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

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

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

    return $this;
}

/**
 * @return Collection|Answer[]
 */
public function getAnswers(): Collection
{
    return $this->answers;
}

public function addAnswer(Answer $answer): self
{
    if (!$this->answers->contains($answer)) {
        $this->answers[] = $answer;
        $answer->setQuestion($this);
    }

    return $this;
}

public function removeAnswer(Answer $answer): self
{
    if ($this->answers->contains($answer)) {
        $this->answers->removeElement($answer);
        if ($answer->getQuestion() === $this) {
            $answer->setQuestion(null);
        }
    }

    return $this;
}
}

实体Answer

class Answer
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;
/**
 * @ORM\Column(type="text")
 */
private $text;

/**
 * @ORM\Column(type="boolean")
 */
private $is_correct;

/**
 * @ORM\ManyToOne(targetEntity="App\Entity\Question", inversedBy="answers")
 * @ORM\JoinColumn(nullable=false)
 */
private $question;

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

public function getText(): ?string
{
    return $this->text;
}

public function setText(string $text): self
{
    $this->text = $text;

    return $this;
}

public function getIsCorrect(): ?bool
{
    return $this->is_correct;
}

public function setIsCorrect(bool $is_correct): self
{
    $this->is_correct = $is_correct;

    return $this;
}

public function getQuestion(): ?question
{
    return $this->question;
}

public function setQuestion(?Question $question): self
{
    $this->question = $question;

    return $this;
}

}

我的表格

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('title', EntityType::class, array(
            'class' => Question::class,
            'choice_label' => 'title',
            'label' => 'Question'
        ));
    $builder
        ->add('answers', CollectionType::class, array(
        'entry_type' => AnswerType::class,
        'entry_options' => array('label' => false),
        'allow_add' => true,
        'by_reference'  => false,
));
    $builder
        ->add('create', SubmitType::class, ['label' => 'Add', 'attr' => ['class' => 'btn btn-primary']]);


}

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
        'data_class' => Question::class
    ]);
}

我的控制器片段

 $question = new Question();
 $answer = new Answer();
 $question->addAnswer($answer);
 $form1 = $this->createForm(QuestionAnswerType::class, $question);
 $form1->handleRequest($request);
    if ($form->isSubmitted() && $form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($question);
        $em->flush();
    }

下一行的错误指针

 $form1->handleRequest($request);

我知道我的控制器有问题,但我不知道如何解决。

我不明白如何正确插入具有 OneToMany-ManyToOne 关系的记录。你能帮帮我吗?

【问题讨论】:

    标签: symfony doctrine-orm orm one-to-many symfony4


    【解决方案1】:

    我认为您看到此错误的原因是因为在您的 Question 类中您已将 title 字段定义为文本类型 (@ORM\Column(type="text"))。

    但是,在您的表单中,您已将表单字段 title 定义为 EntityType,这就是我认为您看到此错误的原因。

    您可以通过更改 Question 类中标题字段的数据库映射来解决此问题您可以更改表单以使用 TextType 而不是 EntityType

    希望对你有帮助

    【讨论】:

    • 我将实体类型更改为“字符串”,但仍然出现错误。
    • 你应该看看卡皮尔的回答我认为这是要走的路。
    【解决方案2】:

    您必须在两个地方进行更改。

    1) 先改成“问题”类

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

    2) 第二个进入表单类,将“EntityType::class”替换为“TextType::class”,并从标题中删除“class”和“choice_label”属性

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('title', TextType::class, array(
                'label' => 'Question'
            ));
        ..... your other code ...   
    }
    

    【讨论】:

    • 我明白了。但我需要使用 EntityType。
    • EntityType 仅在与其他表有关系时使用。这里的“title”字段不与任何人关联,它是一种简单的文本类型。
    • 我在表格问题和答案之间存在 OneToMany-ManyToOne 关系。
    • 是的。为此,您使用了“私人 $answers;” . “标题”列与其他表没有关联。它是独立的专栏。希望你能明白。 :)
    • 我必须使用什么来代替 EntityType 的标题?
    【解决方案3】:

    课堂上的问题__toString函数丢失

    public function __toString()
    {
        return $this->property-to-show;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多