Bonjour,
Je m'appelle Julien.
Je suis dévellopeur à mis temps.
L'un de mes clients m'a demandé de lui faire une application de quizz.
J'ai donc choisi Symfony 4 pour le faire.
J'ai une un controller nommé RiddleController qui doit servir a enregistrer la reponse de l'utilisateur et la mettre dans une table.
Pour ce faire j'ai choisi de faire un peu de relation.
Ma table response_user a donc 3 champs qui sont les objets du contenu qu'elle désigne.
user est l'utilisateur qui a répondu, la question posé est celle de question_id et sa réponse est response_id.
Voici mon code:
Le controlleur RiddleController
<?php
namespace App\Controller;
use App\Entity\ResponseUser;
use App\Entity\Riddle;
use App\Entity\RiddleResponse;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\ButtonType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\Request;
class RiddleController extends AbstractController
{
public function riddle(string $course, int $id, Request $request)
{
$em = $this->getDoctrine()->getManager();
$Course = $em->getRepository('App:Course')->findOneBy(array('Name' => $course));
if(is_null($Course))
{
return $this->redirectToRoute("riddle_show");
}
$Question = $em->getRepository('App:Riddle')->findOneBy(array('id' => $id, 'enabled' => 1, 'Course' => $Course->getId()));
if(is_null($Question))
{
$Question = $em->getRepository('App:Riddle')->findOneBy(array('enabled' => 1, 'Course' => $Course->getId()));
return $this->redirectToRoute('riddle', array('id' => $Question->getId()));
}
$response = new ResponseUser();
$form = $this->createFormBuilder($response)
->add('response_id', ChoiceType::class, array(
'label' => "Choisissez une réponse:",
'expanded' => true,
'data_class' => RiddleResponse::class ,
'choices' => $Question->getAnswers(),
'choice_label' => "response"
))
->add('Valider', SubmitType::class, array(
'attr' => array('class' => 'save')
))
->getForm();
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid())
{
$ResponseUser = new ResponseUser();
$em = $this->getDoctrine()->getManager();
$security = $this->get('security.token_storage')->getToken();
$user = $security->getUser();
$ResponseUser->setQuestion($Question);
$ResponseUser->setResponseId($response->getResponseId());
$ResponseUser->setUser($this->getUser());
$em->persist($ResponseUser);
$em->flush();
}
return $this->render('riddle/index.html.twig', [
'id' => $id,
'question' => $Question,
'form' => $form->createView()
]);
}
public function showRiddle()
{
$em = $this->getDoctrine()->getManager();
$Courses = $em->getRepository('App:Course')->findAll();
return $this->render('riddle/show.html.twig', [
'Courses' => $Courses
]);
}
}
Mon entité User:
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
*/
class User implements UserInterface, \Serializable
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $username;
/**
* @ORM\Column(type="string", length=255)
*/
private $password;
/**
* @var array
*
* @ORM\Column(type="json")
*/
private $roles = [];
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\Column(type="string", length=255)
*/
private $surname;
/**
* Retourne les rôles de l'user
*/
public function getRoles(): array
{
$roles = $this->roles;
// Afin d'être sûr qu'un user a toujours au moins 1 rôle
if (empty($roles)) {
$roles[] = 'ROLE_USER';
}
return array_unique($roles);
}
public function setRoles(array $roles): void
{
$this->roles = $roles;
}
public function getId(): ?int
{
return $this->id;
}
public function getUsername(): ?string
{
return $this->username;
}
public function setUsername(string $username): self
{
$this->username = $username;
return $this;
}
public function getPassword(): ?string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
public function getSalt()
{
// TODO: Implement getSalt() method.
return null;
}
public function eraseCredentials()
{
// TODO: Implement eraseCredentials() method.
}
public function serialize()
{
return serialize([
$this->id,
$this->username,
$this->password
]);
}
public function unserialize($serialized)
{
list (
$this->id,
$this->username,
$this->password
) = unserialize($serialized,['allowed_classes' => false]);
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $Name): self
{
$this->name = $Name;
return $this;
}
public function getSurname(): ?string
{
return $this->surname;
}
public function setSurname(string $Surname): self
{
$this->surname = $Surname;
return $this;
}
}
mon entité Riddle:
<?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\ResponseUserRepository")
*/
class ResponseUser
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\OneToOne(targetEntity="App\Entity\User", fetch="EAGER")
*/
private $User;
/**
* @ORM\OneToOne(targetEntity="App\Entity\Riddle", fetch="EAGER")
*/
private $Question;
/**
* @ORM\OneToOne(targetEntity="App\Entity\RiddleResponse", fetch="EAGER")
*/
private $Response;
public function getId(): ?int
{
return $this->id;
}
public function getResponseId()
{
return $this->Response;
}
public function setResponseId(RiddleResponse $riddleResponse): RiddleResponse
{
$this->Response = $riddleResponse;
return $this->Response;
}
public function getUser()
{
return $this->User;
}
public function setUser(User $user): User
{
$this->User = $user;
return $this->User;
}
public function getQuestion()
{
return $this->Question;
}
public function setQuestion(Riddle $riddle): Riddle
{
$this->Question = $riddle;
return $this->Question;
}
}
Mon entité RiddleResponse
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\RiddleResponseRepository")
*/
class RiddleResponse
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="text")
*/
private $Response;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Riddle", inversedBy="answers")
*/
private $Question;
public function getId(): ?int
{
return $this->id;
}
public function getResponse(): ?string
{
return $this->Response;
}
public function setResponse(string $Response): self
{
$this->Response = $Response;
return $this;
}
public function getQuestion(): ?Riddle
{
return $this->Question;
}
public function setQuestion(?Riddle $Question): self
{
$this->Question = $Question;
return $this;
}
}
Et mon entité qui lie tout ensemble ResponseUser:
<?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\ResponseUserRepository")
*/
class ResponseUser
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\OneToOne(targetEntity="App\Entity\User", fetch="EAGER")
*/
private $User;
/**
* @ORM\OneToOne(targetEntity="App\Entity\Riddle", fetch="EAGER")
*/
private $Question;
/**
* @ORM\OneToOne(targetEntity="App\Entity\RiddleResponse", fetch="EAGER")
*/
private $Response;
public function getId(): ?int
{
return $this->id;
}
public function getResponseId()
{
return $this->Response;
}
public function setResponseId(RiddleResponse $riddleResponse): RiddleResponse
{
$this->Response = $riddleResponse;
return $this->Response;
}
public function getUser()
{
return $this->User;
}
public function setUser(User $user): User
{
$this->User = $user;
return $this->User;
}
public function getQuestion()
{
return $this->Question;
}
public function setQuestion(Riddle $riddle): Riddle
{
$this->Question = $riddle;
return $this->Question;
}
}
Actuellement mon code marche, mais on ne peux repondre qu'une fois a une question. Et un utilisateur ne peux pas répondre à 2 questions.
Je voudrais que l'on m'aide à corrigé cela pour faire en sorte qu'un utilisateur puisse répondre à une liste de question infini et aussi qu'une question puisse être répondu par une infinité d'utilisateur.
Un grand merci d'avance à celles et ceux qui m'apporterons leur aide.