openvk/Web/Models/Entities/Attachable.php

43 lines
1 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
2020-06-07 19:04:43 +03:00
namespace openvk\Web\Models\Entities;
2020-06-07 19:04:43 +03:00
use openvk\Web\Models\RowModel;
abstract class Attachable extends RowModel
{
public function getId(): int
2020-06-07 19:04:43 +03:00
{
return $this->getRecord()->id;
}
public function getParents(): \Traversable
2020-06-07 19:04:43 +03:00
{
$sel = $this->getRecord()
->related("attachments.attachable_id")
->where("attachments.attachable_type", get_class($this));
foreach ($sel as $rel) {
2020-06-07 19:04:43 +03:00
$repoName = $rel->target_type . "s";
$repoName = str_replace("Entities", "Repositories", $repoName);
$repo = new $repoName();
2020-06-07 19:04:43 +03:00
yield $repo->get($rel->target_id);
}
}
2020-06-07 19:04:43 +03:00
/**
* Deletes together with all references.
*/
public function delete(bool $softly = true): void
2020-06-07 19:04:43 +03:00
{
$this->getRecord()
->related("attachments.attachable_id")
->where("attachments.attachable_type", get_class($this))
->delete();
2020-06-07 19:04:43 +03:00
parent::delete();
}
}