Bonjour,
Je débute sur Symfony et je rencontre depuis quelques jours un problèmes.
Mon projet est le suivant: créer un utilitaire pour administrer des sondages.
Voci mes règles de gestion:

  • Chaque sondage a plusieurs propositions de réponse.
  • Chaque utilisateur répondre à un seul sondage.
  • Chaque utilisateur peut choisir une ou plusieurs propositions de réponse (attention: il est possible que le sondage ait plusieurs réponses possibles).
    Voici mes trois entités:
    <code>
    1. survey.php

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**

  • @ORM\Entity(repositoryClass="App\Repository\SurveyRepository")
    */
    class Survey
    {
    /**

    • @ORM\Id()
    • @ORM\GeneratedValue()
    • @ORM\Column(type="integer")
      */
      protected $id;

    /**

    • @ORM\Column(type="string", length=500)
      */
      protected $question;

    /**

    • @ORM\Column(type="boolean")
      */
      protected $multiple;

    /**

    • @ORM\OneToMany(targetEntity="App\Entity\Proposition", mappedBy="survey", cascade={"persist"}, orphanRemoval=true)
      */
      protected $propositions;

    /**

    • @ORM\OneToMany(targetEntity="App\Entity\User", mappedBy="survey", cascade={"persist"}, orphanRemoval=true)
      */
      protected $users;

    public function __construct()
    {
    $this->propositions = new ArrayCollection();
    $this->users = new ArrayCollection();
    }

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

    public function getQuestion(): ?string
    {
    return $this->question;
    }

    public function setQuestion(string $question): self
    {
    $this->question = $question;

    return $this;

    }

    public function getMultiple(): ?bool
    {
    return $this->multiple;
    }

    public function setMultiple(bool $multiple): self
    {
    $this->multiple = $multiple;

    return $this;

    }

    /**

    • @return Collection|Proposition[]
      */
      public function getPropositions(): Collection
      {
      return $this->propositions;
      }

    public function addProposition(Proposition $proposition): self
    {
    if (!$this->propositions->contains($proposition)) {
    $this->propositions[] = $proposition;
    $proposition->setSurvey($this);
    $this->propositions->add($proposition);
    }
    }

    /**

    • @return Collection|User[]
      */
      public function getUsers(): Collection
      {
      return $this->users;
      }

    public function addUser(User $user): self
    {
    if (!$this->users->contains($user)) {
    $this->users[] = $user;
    $user->setSurvey($this);
    }

    return $this;

    }
    }

  1. proposition.php
    <?php

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**

  • @ORM\Entity(repositoryClass="App\Repository\PropositionRepository")
    */
    class Proposition
    {
    /**

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

    /**

    • @ORM\ManyToOne(targetEntity="App\Entity\Survey", inversedBy="propositions")
    • @ORM\JoinColumn(nullable=false)
      */
      private $survey;

    /**

    • @ORM\Column(type="string", length=255)
      */
      private $wording;

    /**

    • @ORM\ManyToMany(targetEntity="App\Entity\User", mappedBy="propositions")
      */
      private $users;

    public function __construct()
    {
    $this->users = new ArrayCollection();
    }

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

    public function getSurvey(): ?survey
    {
    return $this->survey;
    }

    public function setSurvey(?survey $survey): self
    {
    $this->survey = $survey;

    return $this;

    }

    public function getWording(): ?string
    {
    return $this->wording;
    }

    public function setWording(string $wording): self
    {
    $this->wording = $wording;

    return $this;

    }

    /**

    • @return Collection|User[]
      */
      public function getUsers(): Collection
      {
      return $this->users;
      }

    public function addUser(User $user): self
    {
    if (!$this->users->contains($user)) {
    $this->users[] = $user;
    $user->addProposition($this);
    }

    return $this;

    }
    }

  1. user.php
    namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**

  • @ORM\Entity(repositoryClass="App\Repository\UserRepository")
    */
    class User
    {
    /**

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

    /**

    • @ORM\ManyToOne(targetEntity="App\Entity\Survey", inversedBy="users")
    • @ORM\JoinColumn(nullable=false)
      */
      private $survey;

    /**

    • @ORM\ManyToMany(targetEntity="App\Entity\Proposition", inversedBy="users")
      */
      protected $propositions;

    public function __construct()
    {
    $this->propositions = new ArrayCollection();
    }

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

    public function getSurvey(): ?survey
    {
    return $this->survey;
    }

    public function setSurvey(?survey $survey): self
    {
    $this->survey = $survey;

    return $this;

    }

    /**

    • @return Collection|proposition[]
      */
      public function getPropositions(): Collection
      {
      return $this->propositions;
      }

    public function addProposition(proposition $proposition): self
    {
    if (!$this->propositions->contains($proposition)) {
    $this->propositions[] = $proposition;
    }

    return $this;

    }
    }
    </code>
    Ma relation ManyToMany a créé une table user_proposition dans ma BDD.
    Quand je crée un sondage:

    1. S'il y a plusieurs réponses possibles: des checkbox sont présentées à l'utilisateur.
    2. S'il y a une seule réponse possible: des boutons radios sont présentés à l'utilisateur.
      <code>
      Voci le formulaire UserType:

namespace App\Form;

use App\Entity\User;
use App\Entity\Proposition;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\AbstractType;
use App\Repository\PropositionRepository;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormInterface;

class UserType extends AbstractType
{

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $survey = $options['survey'];
    $multiple = $options['multiple'];

    if ($multiple == 1)
        $param = "true";
    else
        $param = "false";

    $choices = array();
    foreach ($options['propositions'] as $proposition) {
        $wording = $proposition->getWording();
        $choices[$wording] = $wording;
    }

        //...

    if ($param == "false") {
        $builder->add('propositions', ChoiceType::class, array(
            'choices' => $choices,
            'expanded' => true,
            'multiple' => false,
        ));
    } else {
        $builder->add('propositions', EntityType::class, [
            'class' => Proposition::class,
            'expanded' => true,
            'multiple' => true,
            'query_builder' => function (EntityRepository $proposition) use ($survey) {
                return $proposition->createQueryBuilder('u')
                    ->where('u.survey = :survey')
                    ->setParameter('survey', $survey);
            },
            'choice_label' => 'wording',
        ]);
    }    

}

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
        'data_class' => User::class,
        'survey' => null,
        'multiple' => null,
        'propositions' => null,
    ]);
}

}

Voici le SurveyController:

namespace App\Controller;

use App\Entity\User;
use App\Entity\Survey;
use App\Form\UserType;
use App\Form\SurveyType;
use App\Entity\Proposition;
use App\Repository\PropositionRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class SurveyController extends AbstractController
{
/**

  • @Route("/answer/{id}", name="survey_answer")
    */
    public function survey_answer($id, Request $request, EntityManagerInterface $manager)
    {
    $survey = $this->getDoctrine()->getRepository(Survey::class)->find($id);
    $user = new User();
    $form = $this->createForm(UserType::class, $user, array(
    'survey' => $survey->getId(),
    'multiple' => $survey->getMultiple(),
    'propositions' => $survey->getPropositions(),
    ));
    $form->handleRequest($request);
    if ($form->isSubmitted() && $form->isValid()) {
    $user->setSurvey($survey);
    foreach($form["propositions"]->getData() as $proposition) {
    $user->addProposition($proposition);
    }
    $manager->persist($user);
    $manager->flush();
    return $this->redirectToRoute('survey_list');
    }
    return $this->render('survey/answer.html.twig', [
    'controller_name' => 'SurveyController',
    'form' => $form->createView(),
    'survey' => $survey,
    ]);
    }
    }
    </code>
    Mon problème :
    Quand j'ai plusieurs réponses possibles et que je valide mon formulaire, je n'ai aucun problème.
    en revanche, quand j'ai qu'une seule réponse possible, j'ai une erreur : Could not determine access type for property "propositions" in class "App\Entity\User": The property "propositions" in class "App\Entity\User" can be defined with the methods "addProposition()"" but the new value must be an array or an instance of \Traversable, "string"

J'ai cherché sur internet. Malheureusement, ne n'ai rien trouvé de probant.
Auriez-vous une idée s'il-vous-plaît?
Espérant avoir été assez clair,
Vous en remerciant par avance,

Aucune réponse