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:
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
{
/**
/**
/**
/**
/**
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;
}
/**
public function addProposition(Proposition $proposition): self
{
if (!$this->propositions->contains($proposition)) {
$this->propositions[] = $proposition;
$proposition->setSurvey($this);
$this->propositions->add($proposition);
}
}
/**
public function addUser(User $user): self
{
if (!$this->users->contains($user)) {
$this->users[] = $user;
$user->setSurvey($this);
}
return $this;
}
}
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
{
/**
/**
/**
/**
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;
}
/**
public function addUser(User $user): self
{
if (!$this->users->contains($user)) {
$this->users[] = $user;
$user->addProposition($this);
}
return $this;
}
}
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
@ORM\Entity(repositoryClass="App\Repository\UserRepository")
*/
class User
{
/**
/**
/**
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;
}
/**
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:
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
{
/**
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,