如何声明Symfony2和Doctrine2不需要多对多关系?

时间:2012-01-11 16:23:47

标签: forms symfony many-to-many doctrine-orm required-field

我有两个名为Question和SolutionMethod的PHP模型类。类别可能包含许多项目,而项目可能属于许多类别。我已经为这两个类创建了ManyToMany关系:

class Question {
    /**
     * @ORM\ManyToMany(targetEntity="SolutionMethod", mappedBy="questions", cascade={"persist"})
     */
    private $solutions;

    /**
     * Add items
     *
     * @param Acme\MyBundle\Entity\SolutionMethod $solution
     */
    public function addItems(\Acme\MyBundle\Entity\SolutionMethod $solution) {
        $this->solutions[] = $solutions;
    }

    /**
     * Get solutions
     *
     * @return Doctrine\Common\Collections\Collection 
     */
    public function getSolutions() {
        return $this->solutions;
    }

    [...]

}

class SolutionMethod {
    /**
     * @ORM\ManyToMany(targetEntity="Question", inversedBy="solutions", cascade={"persist"})
     * @ORM\JoinTable(name="question_solution")
     */
    private $questions;

    /**
     * Add questions
     *
     * @param Acme\MyBundle\Entity\Question $question
     */
    public function addCategories(\Acme\MyBundle\Entity\Question $question) {
        $this->questions[] = $question;
    }

    /**
     * Get questions
     *
     * @return Doctrine\Common\Collections\Collection 
     */
    public function getQuestions() {
        return $this->questions;
    }

    [...]

}

在我的表单中,此关联使用带有必需属性的多选项进行呈现,但我有一些没有SolutionMethod的问题,但我无法保存,并且显示所需的消息。

解决!

解决方案很简单,让我觉得自己像个白痴

class QuestionType extends AbstractType {

    public function buildForm(FormBuilder $builder, array $options) {
        $builder
            ->add('solutions', null, array('required' => false))
            [...]
        ;
    }
}

0 个答案:

没有答案