How to declare a Many-to-Many relationship is not required with Symfony2 and Doctrine2?

I have two classes of the PHP model called Question and SolutionMethod. A category can have many elements, and an element can belong to many categories. I created ManyToMany's relation to both classes:

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; } [...] } 

and

 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; } [...] } 

In my form, this association is rendered using multi-select with the required attribute, but I have some kind of question without SolutionMethod, but I cannot save, and the required message appears.

Solved!

The solution is so simple that I feel like an idiot

 class QuestionType extends AbstractType { public function buildForm(FormBuilder $builder, array $options) { $builder ->add('solutions', null, array('required' => false)) [...] ; } } 
+4
source share

Source: https://habr.com/ru/post/1390471/


All Articles