add choices and votes links in DB

This commit is contained in:
Baptiste Lemoine 2019-11-06 14:35:07 +01:00
parent 7340b020ec
commit 9123faf3f1
6 changed files with 264 additions and 25 deletions

View File

@ -126,7 +126,7 @@ class DefaultController extends AbstractController {
return $this->json( [
'message' => 'you created a poll ' . $precision,
// 'data' => $jsonContent,
'data' => $newpoll,
],
203 );

131
src/Entity/Choice.php Normal file
View File

@ -0,0 +1,131 @@
<?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\ChoiceRepository")
*/
class Choice {
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $name;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $dateTime;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Poll", mappedBy="choices")
*/
private $poll;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Choice", mappedBy="choice")
*/
private $votes;
public function __construct()
{
$this->poll = new ArrayCollection();
$this->votes = new ArrayCollection();
}
public function getId(): ?int {
return $this->id;
}
public function getName(): ?string {
return $this->name;
}
public function setName( string $name ): self {
$this->name = $name;
return $this;
}
public function getDateTime(): ?\DateTimeInterface {
return $this->dateTime;
}
public function setDateTime( ?\DateTimeInterface $dateTime ): self {
$this->dateTime = $dateTime;
return $this;
}
/**
* @return Collection|Poll[]
*/
public function getPoll(): Collection
{
return $this->poll;
}
public function addPoll(Poll $poll): self
{
if (!$this->poll->contains($poll)) {
$this->poll[] = $poll;
$poll->setChoices($this);
}
return $this;
}
public function removePoll(Poll $poll): self
{
if ($this->poll->contains($poll)) {
$this->poll->removeElement($poll);
// set the owning side to null (unless already changed)
if ($poll->getChoices() === $this) {
$poll->setChoices(null);
}
}
return $this;
}
/**
* @return Collection|Choice[]
*/
public function getVotes(): Collection
{
return $this->votes;
}
public function addVote(Choice $vote): self
{
if (!$this->votes->contains($vote)) {
$this->votes[] = $vote;
$vote->setChoice($this);
}
return $this;
}
public function removeVote(Choice $vote): self
{
if ($this->votes->contains($vote)) {
$this->votes->removeElement($vote);
// set the owning side to null (unless already changed)
if ($vote->getChoice() === $this) {
$vote->setChoice(null);
}
}
return $this;
}
}

View File

@ -21,14 +21,16 @@ class Owner {
/**
* @ORM\Column(type="string", length=255)
* @Serializer\Type("string")
* @Serializer\Expose()
*/
private $email;
/**
* @ORM\Column(type="string", length=255)
* @Serializer\Type("string")
* @Serializer\Expose()
*/
private $pseudo;
public $pseudo;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Poll", mappedBy="owner",cascade={"persist"})

View File

@ -83,26 +83,31 @@ class Poll {
* self - one can only modify its own vote
* nobody - no one can modify the votes (excepted admin), pray to have it right at first
* @ORM\Column(type="string", length=255)
* @Serializer\Type("string")
*/
public $modificationPolicy;
/**
* send a mail on a new comment
* @ORM\Column(type="boolean", nullable=true)
* @Serializer\Type("boolean")
*/
private $mailOnComment;
/**
* send a mail on a new vote
* @ORM\Column(type="boolean", nullable=true)
* @Serializer\Type("boolean")
*/
private $mailOnVote;
/**
* hide publicly results
* @ORM\Column(type="boolean", nullable=true)
* @Serializer\Type("boolean")
*/
private $hideResults;
/**
* show publicly results even if there is a password to access the vote
* @ORM\Column(type="boolean", nullable=true)
* @Serializer\Type("boolean")
*/
private $showResultEvenIfPasswords;
@ -111,9 +116,15 @@ class Poll {
* @Serializer\Type("App\Entity\Vote")
*/
private $votes;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Choice", mappedBy="poll", orphanRemoval=true,cascade={"persist"})
* @Serializer\Type("App\Entity\Choice")
*/
private $choices;
public function __construct() {
$this->votes = new ArrayCollection();
$this->votes = new ArrayCollection();
$this->choices = new ArrayCollection();
}
public function getId(): ?int {
@ -287,4 +298,32 @@ class Poll {
return $this;
}
/**
* @return Collection|Choice[]
*/
public function getChoices(): Collection {
return $this->choices;
}
public function addChoice( Choice $choice ): self {
if ( ! $this->choices->contains( $choice ) ) {
$this->choices[] = $choice;
$choice->setPoll( $this );
}
return $this;
}
public function removeChoice( Choice $choice ): self {
if ( $this->choices->contains( $choice ) ) {
$this->choices->removeElement( $choice );
// set the owning side to null (unless already changed)
if ( $choice->getPoll() === $this ) {
$choice->setPoll( null );
}
}
return $this;
}
}

View File

@ -39,50 +39,55 @@ class Vote {
* @ORM\JoinColumn(nullable=false)
*/
private $poll;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Choice", inversedBy="votes")
* @ORM\JoinColumn(nullable=false)
*/
private $choice;
public function getId(): ?int {
return $this->id;
}
return $this->id;
}
public function getAnswerTxt(): ?string {
return $this->answerTxt;
}
return $this->answerTxt;
}
public function setAnswerTxt( ?string $answerTxt ): self {
$this->answerTxt = $answerTxt;
$this->answerTxt = $answerTxt;
return $this;
}
return $this;
}
public function getAnswerDate(): ?\DateTimeInterface {
return $this->answerDate;
}
return $this->answerDate;
}
public function setAnswerDate( ?\DateTimeInterface $answerDate ): self {
$this->answerDate = $answerDate;
$this->answerDate = $answerDate;
return $this;
}
return $this;
}
public function getCreationDate(): ?\DateTimeInterface {
return $this->creationDate;
}
return $this->creationDate;
}
public function setCreationDate( \DateTimeInterface $creationDate ): self {
$this->creationDate = $creationDate;
$this->creationDate = $creationDate;
return $this;
}
return $this;
}
public function getPoll(): ?Poll {
return $this->poll;
}
return $this->poll;
}
public function setPoll( ?Poll $poll ): self {
$this->poll = $poll;
$this->poll = $poll;
return $this;
}
return $this;
}
public function getPseudo(): ?string
{
@ -95,4 +100,16 @@ class Vote {
return $this;
}
public function getChoice(): ?Choice
{
return $this->choice;
}
public function setChoice(?Choice $choice): self
{
$this->choice = $choice;
return $this;
}
}

View File

@ -0,0 +1,50 @@
<?php
namespace App\Repository;
use App\Entity\Choice;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;
/**
* @method Choice|null find($id, $lockMode = null, $lockVersion = null)
* @method Choice|null findOneBy(array $criteria, array $orderBy = null)
* @method Choice[] findAll()
* @method Choice[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class ChoiceRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Choice::class);
}
// /**
// * @return Choice[] Returns an array of Choice objects
// */
/*
public function findByExampleField($value)
{
return $this->createQueryBuilder('c')
->andWhere('c.exampleField = :val')
->setParameter('val', $value)
->orderBy('c.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
*/
/*
public function findOneBySomeField($value): ?Choice
{
return $this->createQueryBuilder('c')
->andWhere('c.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
*/
}