2025-01-31 18:20:13 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-06-07 19:04:43 +03:00
|
|
|
namespace openvk\Web\Models\Entities;
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2020-06-07 19:04:43 +03:00
|
|
|
use openvk\Web\Models\RowModel;
|
|
|
|
|
|
|
|
abstract class Attachable extends RowModel
|
|
|
|
{
|
2025-01-31 18:20:13 +03:00
|
|
|
public function getId(): int
|
2020-06-07 19:04:43 +03:00
|
|
|
{
|
|
|
|
return $this->getRecord()->id;
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
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));
|
2025-01-31 18:20:13 +03:00
|
|
|
foreach ($sel as $rel) {
|
2020-06-07 19:04:43 +03:00
|
|
|
$repoName = $rel->target_type . "s";
|
|
|
|
$repoName = str_replace("Entities", "Repositories", $repoName);
|
2025-01-31 18:20:13 +03:00
|
|
|
$repo = new $repoName();
|
|
|
|
|
2020-06-07 19:04:43 +03:00
|
|
|
yield $repo->get($rel->target_id);
|
|
|
|
}
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2020-06-07 19:04:43 +03:00
|
|
|
/**
|
|
|
|
* Deletes together with all references.
|
|
|
|
*/
|
2025-01-31 18:20:13 +03:00
|
|
|
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();
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2020-06-07 19:04:43 +03:00
|
|
|
parent::delete();
|
|
|
|
}
|
|
|
|
}
|