meta = new ArrayCollection(); } /** * @param CardMeta $meta */ public function addMeta(CardMeta $meta) { $meta->card = $this; $this->meta[] = $meta; } /** * @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; } }