mirror of
https://github.com/openvk/openvk
synced 2024-11-11 17:49:37 +03:00
39 lines
1 KiB
PHP
39 lines
1 KiB
PHP
|
<?php declare(strict_types=1);
|
||
|
namespace openvk\Web\Models\Entities;
|
||
|
use openvk\Web\Models\RowModel;
|
||
|
|
||
|
abstract class Attachable extends RowModel
|
||
|
{
|
||
|
function getId(): int
|
||
|
{
|
||
|
return $this->getRecord()->id;
|
||
|
}
|
||
|
|
||
|
function getParents(): \Traversable
|
||
|
{
|
||
|
$sel = $this->getRecord()
|
||
|
->related("attachments.attachable_id")
|
||
|
->where("attachments.attachable_type", get_class($this));
|
||
|
foreach($sel as $rel) {
|
||
|
$repoName = $rel->target_type . "s";
|
||
|
$repoName = str_replace("Entities", "Repositories", $repoName);
|
||
|
$repo = new $repoName;
|
||
|
|
||
|
yield $repo->get($rel->target_id);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Deletes together with all references.
|
||
|
*/
|
||
|
function delete(bool $softly = true): void
|
||
|
{
|
||
|
$this->getRecord()
|
||
|
->related("attachments.attachable_id")
|
||
|
->where("attachments.attachable_type", get_class($this))
|
||
|
->delete();
|
||
|
|
||
|
parent::delete();
|
||
|
}
|
||
|
}
|