From 4cacc94240944ff316104bfd1b5e8e00fad14517 Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Fri, 14 Jan 2022 18:21:36 +0100 Subject: Add Artworks, Votes and better routing --- src/Model/Card.php | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'src/Model/Card.php') diff --git a/src/Model/Card.php b/src/Model/Card.php index ab421ab..9707e16 100644 --- a/src/Model/Card.php +++ b/src/Model/Card.php @@ -37,12 +37,26 @@ class Card */ public Collection|ArrayCollection|PersistentCollection $meta; + #[OneToMany(targetEntity: Artwork::class, mappedBy: 'card')] + /** + * @OneToMany(targetEntity="Artwork", mappedBy="card") + */ + public Collection|ArrayCollection|PersistentCollection $artworks; + + #[OneToMany(targetEntity: VoteCard::class, mappedBy: 'card')] + /** + * @OneToMany(targetEntity="VoteCard", mappedBy="card") + */ + public Collection|ArrayCollection|PersistentCollection $votes; + /** * Card constructor. */ public function __construct() { $this->meta = new ArrayCollection(); + $this->artworks = new ArrayCollection(); + $this->votes = new ArrayCollection(); } /** @@ -54,6 +68,24 @@ class Card $this->meta[] = $meta; } + /** + * @param Artwork $artwork + */ + public function addArtwork(Artwork $artwork) + { + $artwork->card = $this; + $this->artworks[] = $artwork; + } + + /** + * @param VoteCard $vote + */ + public function addVote(VoteCard $vote) + { + $vote->card = $this; + $this->votes[] = $vote; + } + /** * @param string $key * @@ -83,5 +115,22 @@ class Card return $result['value'] ?? null; } + + /** + * @return int + */ + public function getVotesTotal(): int + { + $result = DB::$entityManager + ->createQuery( + 'SELECT sum(v.value) as total + FROM Elements\Model\VoteCard v + WHERE v.card = :card' + ) + ->setParameter('card', $this) + ->getOneOrNullResult(); + + return $result['total'] ?? 0; + } } -- cgit v1.2.3