From 5d74fb978078cb6dd7e3eac2bb6654cbccc752d0 Mon Sep 17 00:00:00 2001 From: Baptiste Lemoine Date: Wed, 27 Nov 2019 17:07:11 +0100 Subject: [PATCH] vote stack with pseudo --- src/DataFixtures/AppPollFixtures.php | 52 +++++++++++++++++++++++----- src/Entity/StackOfVotes.php | 12 +++++-- src/Entity/Vote.php | 30 +++++----------- 3 files changed, 63 insertions(+), 31 deletions(-) diff --git a/src/DataFixtures/AppPollFixtures.php b/src/DataFixtures/AppPollFixtures.php index ae5c00f..8558f8e 100644 --- a/src/DataFixtures/AppPollFixtures.php +++ b/src/DataFixtures/AppPollFixtures.php @@ -6,6 +6,7 @@ use App\Entity\Choice; use App\Entity\Comment; use App\Entity\Owner; use App\Entity\Poll; +use App\Entity\StackOfVotes; use App\Entity\Vote; use DateTime; use Doctrine\Bundle\FixturesBundle\Fixture; @@ -43,15 +44,50 @@ class AppPollFixtures extends Fixture { $poll ->addChoice( $choiceA ) ->addChoice( $choiceB ); - - $voteA = new Vote(); - $voteA->setPseudo( 'chuck norris' ) - ->setChoice( $choiceA ); - $voteA = new Vote(); - $voteA->setPseudo( 'Néo' ) - ->setChoice( $choiceB ); - $manager->persist( $poll ); + $manager->persist( $owner ); + $manager->persist( $voter ); + $manager->flush(); + + $stack1 = new StackOfVotes(); + $stack1 + ->setPoll( $poll ) + ->setOwner( $voter ); + + $voteA = new Vote(); + $voteA + ->setPoll( $poll ) + ->setStackOfVotes( $stack1 ) + ->setValue( [ "yes" ] ) + ->setChoice( $choiceA ); + $voteB = new Vote(); + $voteB + ->setPoll( $poll ) + ->setStackOfVotes( $stack1 ) + ->setValue( [ "yes" ] ) + ->setChoice( $choiceB ); + + $stack1->setPseudo( 'chuck norris' ); + + + $manager->persist( $stack1 ); + + // voter guy votes again with an other pseudo + + $stack2 = new StackOfVotes(); + $stack2->setPseudo( 'Jean indécis' ); + $stack2 + ->setPoll( $poll ) + ->setOwner( $voter ); + + $voteA = new Vote(); + $voteA + ->setPoll( $poll ) + ->setStackOfVotes( $stack2 ) + ->setValue( [ "maybe" ] ) + ->setChoice( $choiceA ); + + $manager->persist( $stack2 ); $poll = new Poll(); diff --git a/src/Entity/StackOfVotes.php b/src/Entity/StackOfVotes.php index 1fa7daa..f3027f1 100644 --- a/src/Entity/StackOfVotes.php +++ b/src/Entity/StackOfVotes.php @@ -5,6 +5,7 @@ namespace App\Entity; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; +use JMS\Serializer\Annotation as Serializer; /** * contains the votes for one answer to a poll @@ -17,7 +18,12 @@ class StackOfVotes { * @ORM\Column(type="integer") */ private $id; - + /** + * @ORM\Column(type="string", length=255) + * @Serializer\Type("string") + * @Serializer\Expose() + */ + public $pseudo; /** * @ORM\OneToMany(targetEntity="App\Entity\Vote", mappedBy="stacksOfVotes", cascade={"persist","remove"}) */ @@ -28,7 +34,7 @@ class StackOfVotes { private $poll; /** - * @ORM\ManyToOne(targetEntity="App\Entity\Owner", inversedBy="stackOfVotes") + * @ORM\ManyToOne(targetEntity="App\Entity\Owner", inversedBy="stackOfVotes", cascade={"persist"}) */ private $owner; @@ -49,6 +55,8 @@ class StackOfVotes { public function addVote( Vote $vote ): self { if ( ! $this->votes->contains( $vote ) ) { + $vote->setPoll( $this->getPoll() ); + $this->votes[] = $vote; $vote->setStackOfVotes( $this ); } diff --git a/src/Entity/Vote.php b/src/Entity/Vote.php index 023e094..443cb18 100644 --- a/src/Entity/Vote.php +++ b/src/Entity/Vote.php @@ -10,20 +10,15 @@ use JMS\Serializer\Annotation as Serializer; * @ORM\Entity(repositoryClass="App\Repository\VoteRepository") */ class Vote { - /** - * @ORM\Column(type="string", length=255, nullable=true) - * @Serializer\Type("string") - */ - public $pseudo; /** * for a text kind of choice: could be "yes" "no" "maybe" and emptu. * for a date kind, the choice linked is equivalent to the value selected - * @ORM\Column(type="string", length=255, nullable=true) - * @Serializer\Type("string") + * @ORM\Column(type="array", length=255, nullable=true) + * @Serializer\Type("array") */ - public $value; + public $value = []; /** - * @ORM\Column(type="datetime") + * @ORM\Column(type="datetime" , options={"default"="CURRENT_TIMESTAMP"}) * @Serializer\Type("datetime") */ public $creationDate; @@ -47,7 +42,7 @@ class Vote { */ private $poll; /** - * @ORM\ManyToOne(targetEntity="App\Entity\StackOfVotes", inversedBy="votes") + * @ORM\ManyToOne(targetEntity="App\Entity\StackOfVotes", inversedBy="votes", cascade={"persist"}) * @ORM\JoinColumn(nullable=false) * @Serializer\Type("App\Entity\StackOfVotes") */ @@ -67,16 +62,9 @@ class Vote { public function setPoll( ?Poll $poll ): self { $this->poll = $poll; - - return $this; - } - - public function getPseudo(): ?string { - return $this->pseudo; - } - - public function setPseudo( ?string $pseudo ): self { - $this->pseudo = $pseudo; + if ( $poll ) { + $poll->addVote( $this ); + } return $this; } @@ -95,7 +83,7 @@ class Vote { return $this->value; } - public function setValue( ?string $value ): self { + public function setValue( ?array $value ): self { $this->value = $value; return $this;