I have two PHP model classes named Question and SolutionMethod. A Category may have many Items and an Item may belong to many Categories. I have created a ManyToMany 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 with a multi-select with required attribute, but I have some Question without SolutionMethod, but I can not save, and the required message appears.
Solved!
The solution is so simple to make me feel like an idiot
class QuestionType extends AbstractType {
public function buildForm(FormBuilder $builder, array $options) {
$builder
->add('solutions', null, array('required' => false))
[...]
;
}
}