php - 如何正确地以Symfony形式

时间:2018-03-28 18:20:25

标签: php symfony doctrine-orm

我正在创建一个生成测试问题的应用程序,我可以在其中为单个问题添加N个答案。 (我可以通过jQuery添加这些答案) 我应该如何正确发布这个?我已经阅读过Form Events,但我不知道如何在这种情况下实现它。

这是我目前的代码(我正在使用Symfony 4):
问题实体

/**
 * @ORM\Entity(repositoryClass="App\Repository\QuestionRepository")
 * @ORM\Table(name="question")
 */
class Question
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="questions")
     * @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
     */
    private $user;

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

    /**
     * @ORM\Column(type="text", length=2000)
     */
    private $content;

    /**
     * @ORM\Column(type="string", length=45, options={"default": "single"})
     */
    private $type;

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

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

    /**
     * @return User
     */
    public function getUser(): User
    {
        return $this->user;
    }

    /**
     * @param User $user
     */
    public function setUser(User $user): void
    {
        $this->user = $user;
    }

    /**
     * @return string
     */
    public function getLabel(): string
    {
        return $this->label;
    }

    /**
     * @param string $label
     */
    public function setLabel(string $label): void
    {
        $this->label = $label;
    }

    /**
     * @return string
     */
    public function getContent(): string
    {
        return $this->content;
    }

    /**
     * @param string $content
     */
    public function setContent(string $content): void
    {
        $this->content = $content;
    }

    /**
     * @return string
     */
    public function getType(): string
    {
        return $this->type;
    }

    /**
     * @param mixed $type
     */
    public function setType($type): void
    {
        $this->type = $type;
    }

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

QuestionAnswer 实体

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

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

    /**
     * @ORM\Column(type="text")
     */
    private $content;

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

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

    public function getContent(): ?string
    {
        return $this->content;
    }

    public function setContent(string $content): self
    {
        $this->content = $content;

        return $this;
    }

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

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

        return $this;
    }

    /**
     * @return self
     */
    public function getQuestion(): self
    {
        return $this->question;
    }

    /**
     * @param Question $question
     */
    public function setQuestion(Question $question): void
    {
        $this->question = $question;
    }
}

2 个答案:

答案 0 :(得分:0)

您需要遵循文档:

How to Embed a Collection of Forms

在您的案例中,Question = Task,QuestionAnswer = Tag。

答案 1 :(得分:0)

尝试将您的代码更改为此并查看您的自我差异:

问题实体中的

use Doctrine\Common\Collections\ArrayCollection;
//....

/**
* @ORM\Entity(repositoryClass="App\Repository\QuestionRepository")
* @ORM\Table(name="question")
*/
class Question
{
//....

/**
 * @ORM\ManyToMany(targetEntity="App\Entity\QuestionAnswer")
 * @var answers
 */
private $answers;


public function setAnswers(answers $answers)
{
    $this->answers = $answers;
}

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

然后从 QuestionAnswer 实体中删除问题字段并更新数据库架构