Symfony 类型为“的预期参数”;字符串“&引用;“应用\实体”;鉴于
我试图插入一个又一个关系的记录,但出现了错误Symfony 类型为“的预期参数”;字符串“&引用;“应用\实体”;鉴于,symfony,doctrine-orm,orm,one-to-many,symfony4,Symfony,Doctrine Orm,Orm,One To Many,Symfony4,我试图插入一个又一个关系的记录,但出现了错误 Expected argument of type "string", "App\Entity\Question" given. 我有下一个实体问题和答案 class Question { /** * @ORM\Id() * @ORM\GeneratedValue() * @ORM\Column(type="integer") */ private $id; /** * @ORM\Colum
Expected argument of type "string", "App\Entity\Question" given.
我有下一个实体问题
和答案
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;
}
}
实体答案
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);
我知道我的控制器有问题,但我不知道如何解决
我不明白如何正确地插入关系记录。您能帮助我吗?我认为您看到此错误的原因是因为您在问题
类中将标题
字段定义为文本类型(@ORM\Column(type=“Text”)
)
但是,在表单中,您将表单字段title
定义为EntityType
,这就是我认为您看到此错误的原因
您可以通过更改问题中标题字段的数据库映射来解决此问题类或您可以将表单更改为使用文本类型而不是实体类型
希望这有帮助您必须在两个地方进行更改
1) 首先改为“问题”类
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 ...
}
我把实体的类型改成了“字符串”,但仍然有错误。你们应该看看Kapil的答案,我认为这是正确的方法。我明白了。但我需要使用EntityType。EntityType仅在您与其他表有关系时使用。这里的“title”字段与任何一个字段都没有关联,它是一个简单的文本类型。我在表question和answer之间有很多关系。True。为此,您使用了“私人$answers;”。“标题”列与其他表没有关联。它是独立的和列的。希望你得到它。:)我必须使用什么来代替EntityType的标题?
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', TextType::class, array(
'label' => 'Question'
));
..... your other code ...
}