diff options
Diffstat (limited to 'src/Model/Card.php')
-rw-r--r-- | src/Model/Card.php | 87 |
1 files changed, 87 insertions, 0 deletions
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 @@ +<?php + +namespace Elements\Model; + +use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\Common\Collections\Collection; +use Doctrine\DBAL\Schema\Column; +use Doctrine\DBAL\Schema\Table; +use Doctrine\ORM\Mapping\Entity; +use Doctrine\ORM\Mapping\GeneratedValue; +use Doctrine\ORM\Mapping\Id; +use Doctrine\ORM\Mapping\OneToMany; +use Doctrine\ORM\PersistentCollection; +use Elements\DB; + +#[Entity] +#[Table(name: 'cards')] +/** + * @Entity + * @Table(name="cards") + */ +class Card +{ + #[Id] + #[Column(type: 'integer')] + #[GeneratedValue] + /** + * @Id + * @Column(type="integer") + * @GeneratedValue + */ + public int $id; + + #[OneToMany(targetEntity: CardMeta::class, mappedBy: 'card')] + /** + * @OneToMany(targetEntity="CardMeta", mappedBy="card") + */ + public Collection|ArrayCollection|PersistentCollection $meta; + + /** + * Card constructor. + */ + public function __construct() + { + $this->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; + } +} + |