meta = new ArrayCollection(); $this->artworks = new ArrayCollection(); $this->votes = new ArrayCollection(); } /** * @param CardMeta $meta */ public function addMeta(CardMeta $meta) { $meta->card = $this; $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 * * @return string */ public function getMeta(string $key): ?string { // if meta is already hydrated if ($this->meta->isInitialized()) { $meta = $this->meta->unwrap() #->findFirst(fn (CardMeta $item) => $item->key === $key); ->filter(fn ($item) => $item->key === $key)->first(); return $meta->value ?? null; } // get directly from db otherwise $result = DB::$entityManager ->createQuery( 'SELECT cm.value FROM Elements\Model\CardMeta cm WHERE cm.key = :key AND cm.card = :card' ) ->setParameter('key', $key) ->setParameter('card', $this) ->getOneOrNullResult(); 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; } }