From a319f3a419790925bed539ba141038c72a83e70f Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Mon, 10 Jan 2022 20:29:04 +0100 Subject: Initial commit --- src/Model/Card.php | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 src/Model/Card.php (limited to 'src/Model/Card.php') diff --git a/src/Model/Card.php b/src/Model/Card.php new file mode 100644 index 0000000..ab421ab --- /dev/null +++ b/src/Model/Card.php @@ -0,0 +1,87 @@ +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; + } +} + -- cgit v1.2.3