JustName
JustName

Reputation: 111

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

I try to insert records with relations OneToMany-ManyToOne, but I got error.

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

I have next entities question and answer.

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;
}
}

Entity 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;
}

}

My form

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
    ]);
}

My fragment of controller

 $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();
    }

The error pointer at next line

 $form1->handleRequest($request);

I know I have problem with my controller, but I don't know how resolve it.

I don't get it how right insert records with relations OneToMany-ManyToOne. Could you help me?

Upvotes: 2

Views: 3286

Answers (3)

gerMdz
gerMdz

Reputation: 71

In class Question __toString function is missing

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

Upvotes: 0

Kapil
Kapil

Reputation: 896

You have to do changes into two places.

1) First change into "Question" class

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

2) Second into your form class replace "EntityType::class" with "TextType::class" and remove 'class' and 'choice_label' attribute from title

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

Upvotes: 0

temp
temp

Reputation: 46

I think the reason that you are seeing this error is due to the fact that in your Question class you have defined the title field as a Text type (@ORM\Column(type="text")).

However in your Form you have defined the form field title as an EntityType this is the reason I think why you are seeing this error.

You can fix this by changing the database mapping of the title field in your Question class or you could change your Form to use a TextType instead of an EntityType

Hope this helps

Upvotes: 2

Related Questions