update modeling of entities with stack

This commit is contained in:
Baptiste Lemoine 2019-11-27 11:26:21 +01:00
parent eba4a14ce8
commit 945e677d49
6 changed files with 58 additions and 20 deletions

View File

@ -62,6 +62,9 @@ class AppPollFixtures extends Fixture {
$poll->setModificationPolicy( 'self' ); $poll->setModificationPolicy( 'self' );
$poll->setMailOnComment( true ); $poll->setMailOnComment( true );
$poll->setExpiracyDate( new DateTime() ); $poll->setExpiracyDate( new DateTime() );
$poll->addTextChoiceArray( [ 'un truc', 'deux trucs' ] );
$poll->setOwner( $owner ); $poll->setOwner( $owner );
$owner->addPoll( $poll ); $owner->addPoll( $poll );
@ -70,11 +73,15 @@ class AppPollFixtures extends Fixture {
// voting test with 2 people // voting test with 2 people
$stack1 = new StackOfVotes(); $stack1 = new StackOfVotes();
$vote0 = new Vote();
$vote0->setChoice( $poll->getChoices()[ 0 ] );
$stack1->setOwner( $voter ) $stack1->setOwner( $voter )
->addVote( $poll->getChoices()[ 0 ] ); ->addVote( $vote0 );
$stack2 = new StackOfVotes(); $stack2 = new StackOfVotes();
$stack1->setOwner( $owner )->addVote( $poll->getChoices()[ 1 ] ); $vote1 = new Vote();
$vote1->setChoice( $poll->getChoices()[ 1 ] );
$stack1->setOwner( $owner )->addVote( $vote1 );
$voter->addStackOfVote( $stack1 ); $voter->addStackOfVote( $stack1 );
$owner->addStackOfVote( $stack2 ); $owner->addStackOfVote( $stack2 );

View File

@ -47,12 +47,12 @@ class Choice {
* @ORM\OneToMany(targetEntity="App\Entity\Vote", mappedBy="choice") * @ORM\OneToMany(targetEntity="App\Entity\Vote", mappedBy="choice")
* @Serializer\Type("App\Entity\Vote") * @Serializer\Type("App\Entity\Vote")
*/ */
private $vote; private $votes;
public function __construct() { public function __construct() {
$this->poll = new ArrayCollection(); $this->poll = new ArrayCollection();
$this->stackOfVotes = new ArrayCollection(); $this->stackOfVotes = new ArrayCollection();
$this->vote = new ArrayCollection(); $this->votes = new ArrayCollection();
$this->setDateTime( new \DateTime() ); $this->setDateTime( new \DateTime() );
} }
@ -88,6 +88,12 @@ class Choice {
return $this->poll; return $this->poll;
} }
public function setPoll( Poll $poll ): self {
$this->poll = [ $poll ];
return $this;
}
public function addPoll( Poll $poll ): self { public function addPoll( Poll $poll ): self {
if ( ! $this->poll->contains( $poll ) ) { if ( ! $this->poll->contains( $poll ) ) {
$this->poll[] = $poll; $this->poll[] = $poll;
@ -161,7 +167,7 @@ class Choice {
/** /**
* @return Collection|Vote[] * @return Collection|Vote[]
*/ */
public function getVote(): Collection { public function getVotes(): Collection {
return $this->vote; return $this->votes;
} }
} }

View File

@ -17,7 +17,7 @@ class Comment {
private $id; private $id;
/** /**
* @ORM\ManyToOne(targetEntity="App\Entity\Owner", inversedBy="text") * @ORM\ManyToOne(targetEntity="App\Entity\Owner", inversedBy="comments")
*/ */
private $owner; private $owner;

View File

@ -133,10 +133,6 @@ class Poll {
* @Serializer\Type("string") * @Serializer\Type("string")
*/ */
private $adminKey; private $adminKey;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\StackOfVotes", inversedBy="votes")
*/
private $stacksOfVotes;
public function __construct() { public function __construct() {
$this->votes = new ArrayCollection(); $this->votes = new ArrayCollection();
@ -394,11 +390,10 @@ class Poll {
return $this->choices; return $this->choices;
} }
public function addTextChoiceArray( Array $choiceTextArray, $pseudo = "some body" ): self { public function addTextChoiceArray( Array $choiceTextArray ): self {
foreach ( $choiceTextArray as $text ) { foreach ( $choiceTextArray as $text ) {
$newChoice = new Choice(); $newChoice = new Choice();
$newChoice->setName( $text ) $newChoice->setName( $text );
->setPseudo( $pseudo );
$this->addChoice( $newChoice ); $this->addChoice( $newChoice );
} }

View File

@ -19,9 +19,13 @@ class StackOfVotes {
private $id; private $id;
/** /**
* @ORM\OneToMany(targetEntity="App\Entity\poll", mappedBy="stacksOfVotes", cascade={"persist","remove"}) * @ORM\OneToMany(targetEntity="App\Entity\Vote", mappedBy="stacksOfVotes", cascade={"persist","remove"})
*/ */
private $votes; private $votes;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Poll", inversedBy="stacksOfVotes", cascade={"persist"})
*/
private $poll;
/** /**
* @ORM\ManyToOne(targetEntity="App\Entity\Owner", inversedBy="stackOfVotes") * @ORM\ManyToOne(targetEntity="App\Entity\Owner", inversedBy="stackOfVotes")
@ -43,21 +47,21 @@ class StackOfVotes {
return $this->votes; return $this->votes;
} }
public function addVote( poll $vote ): self { public function addVote( Vote $vote ): self {
if ( ! $this->votes->contains( $vote ) ) { if ( ! $this->votes->contains( $vote ) ) {
$this->votes[] = $vote; $this->votes[] = $vote;
$vote->setStacksOfVotes( $this ); $vote->setStackOfVotes( $this );
} }
return $this; return $this;
} }
public function removeVote( poll $vote ): self { public function removeVote( Vote $vote ): self {
if ( $this->votes->contains( $vote ) ) { if ( $this->votes->contains( $vote ) ) {
$this->votes->removeElement( $vote ); $this->votes->removeElement( $vote );
// set the owning side to null (unless already changed) // set the owning side to null (unless already changed)
if ( $vote->getStacksOfVotes() === $this ) { if ( $vote->getStackOfVotes() === $this ) {
$vote->setStacksOfVotes( null ); $vote->setStackOfVotes( null );
} }
} }
@ -83,4 +87,14 @@ class StackOfVotes {
return $this; return $this;
} }
public function getPoll(): ?Poll {
return $this->poll;
}
public function setPoll( ?Poll $poll ): self {
$this->poll = $poll;
return $this;
}
} }

View File

@ -46,6 +46,12 @@ class Vote {
* @Serializer\Type("App\Entity\Poll") * @Serializer\Type("App\Entity\Poll")
*/ */
private $poll; private $poll;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\StackOfVotes", inversedBy="votes")
* @ORM\JoinColumn(nullable=false)
* @Serializer\Type("App\Entity\StackOfVotes")
*/
private $stackOfVotes;
public function __construct() { public function __construct() {
$this->setCreationDate( new \DateTime() ); $this->setCreationDate( new \DateTime() );
@ -104,4 +110,14 @@ class Vote {
return $this; return $this;
} }
public function getStackOfVotes(): ?StackOfVotes {
return $this->stackOfVotes;
}
public function setStackOfVotes( ?StackOfVotes $stackOfVotes ): self {
$this->stackOfVotes = $stackOfVotes;
return $this;
}
} }